Skip to main content
deleted 1 character in body
Source Link
GDP
  • 7.7k
  • 2
  • 30
  • 72
if (!defined('_JEXEC')) {
    define( '_JEXEC', 1 );
    define('JPATH_BASE', realpath(dirname(__FILE__)));
    require_once ( JPATH_BASE .'/includes/defines.php' );
    require_once ( JPATH_BASE .'/includes/framework.php' );
    defined('DS') or define('DS', DIRECTORY_SEPARATOR);
}
$app = JFactory::getApplication('site');

$category_data['id'] = 0;
$category_data['parent_id'] = 0;
$category_data['title'] = 'My Category Title';
$category_data['alias'] = 'my-categegory-title-alias';
$category_data['extension'] = 'com_content';
$category_data['published'] = 1;
$category_data['language'] = '*';
$category_data['params'] = array('category_layout' => '','image' => '');
$category_data['metadata'] = array('author' => '','robots' => '');

$category_id = createCategory($category_data);
if(!$category_id){
    echo "Category create failed!";
}else{
    $article_data = array(
        'id' => 0,
        'catid' => $category_id,
        'title' => 'My article title',
        'alias' => 'my-article-alias',
        'introtext' => 'My intro text',
        'fulltext' => '<p>My full text</p>',
        'state' => 1,
        'language' => '*'
    );
    $article_id = createArticle($article_data);
    if(!$article_id){
        echo "Category"Article create failed!";
    }
}

function createCategory( $data )
{
    $data['rules'] = array(
        'core.edit.state' => array(),
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
        'core.edit.own' => array(1=>true)
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_categories';
    require_once $basePath.'/models/category.php';
    $config  = array('table_path' => $basePath.'/tables');
    $category_model = new CategoriesModelCategory($config);
    if(!$category_model->save($data)){
        $err_msg = $category_model->getError();
        return false;
    }else{
        $id = $category_model->getItem()->id;
        return $id;
    }
}
function createArticle($data)
{
    $data['rules'] = array(
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_content';
    require_once $basePath.'/models/article.php';
    $config = array();
    $article_model = new ContentModelArticle($config);
    if(!$article_model->save($data)){
        $err_msg = $article_model->getError();
        return false;
    }else{
        $id = $article_model->getItem()->id;
        return $id;
    }
}
if (!defined('_JEXEC')) {
    define( '_JEXEC', 1 );
    define('JPATH_BASE', realpath(dirname(__FILE__)));
    require_once ( JPATH_BASE .'/includes/defines.php' );
    require_once ( JPATH_BASE .'/includes/framework.php' );
    defined('DS') or define('DS', DIRECTORY_SEPARATOR);
}
$app = JFactory::getApplication('site');

$category_data['id'] = 0;
$category_data['parent_id'] = 0;
$category_data['title'] = 'My Category Title';
$category_data['alias'] = 'my-categegory-title-alias';
$category_data['extension'] = 'com_content';
$category_data['published'] = 1;
$category_data['language'] = '*';
$category_data['params'] = array('category_layout' => '','image' => '');
$category_data['metadata'] = array('author' => '','robots' => '');

$category_id = createCategory($category_data);
if(!$category_id){
    echo "Category create failed!";
}else{
    $article_data = array(
        'id' => 0,
        'catid' => $category_id,
        'title' => 'My article title',
        'alias' => 'my-article-alias',
        'introtext' => 'My intro text',
        'fulltext' => '<p>My full text</p>',
        'state' => 1,
        'language' => '*'
    );
    $article_id = createArticle($article_data);
    if(!$article_id){
        echo "Category create failed!";
    }
}

function createCategory( $data )
{
    $data['rules'] = array(
        'core.edit.state' => array(),
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
        'core.edit.own' => array(1=>true)
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_categories';
    require_once $basePath.'/models/category.php';
    $config  = array('table_path' => $basePath.'/tables');
    $category_model = new CategoriesModelCategory($config);
    if(!$category_model->save($data)){
        $err_msg = $category_model->getError();
        return false;
    }else{
        $id = $category_model->getItem()->id;
        return $id;
    }
}
function createArticle($data)
{
    $data['rules'] = array(
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_content';
    require_once $basePath.'/models/article.php';
    $config = array();
    $article_model = new ContentModelArticle($config);
    if(!$article_model->save($data)){
        $err_msg = $article_model->getError();
        return false;
    }else{
        $id = $article_model->getItem()->id;
        return $id;
    }
}
if (!defined('_JEXEC')) {
    define( '_JEXEC', 1 );
    define('JPATH_BASE', realpath(dirname(__FILE__)));
    require_once ( JPATH_BASE .'/includes/defines.php' );
    require_once ( JPATH_BASE .'/includes/framework.php' );
    defined('DS') or define('DS', DIRECTORY_SEPARATOR);
}
$app = JFactory::getApplication('site');

$category_data['id'] = 0;
$category_data['parent_id'] = 0;
$category_data['title'] = 'My Category Title';
$category_data['alias'] = 'my-categegory-title-alias';
$category_data['extension'] = 'com_content';
$category_data['published'] = 1;
$category_data['language'] = '*';
$category_data['params'] = array('category_layout' => '','image' => '');
$category_data['metadata'] = array('author' => '','robots' => '');

$category_id = createCategory($category_data);
if(!$category_id){
    echo "Category create failed!";
}else{
    $article_data = array(
        'id' => 0,
        'catid' => $category_id,
        'title' => 'My article title',
        'alias' => 'my-article-alias',
        'introtext' => 'My intro text',
        'fulltext' => '<p>My full text</p>',
        'state' => 1,
        'language' => '*'
    );
    $article_id = createArticle($article_data);
    if(!$article_id){
        echo "Article create failed!";
    }
}

function createCategory( $data )
{
    $data['rules'] = array(
        'core.edit.state' => array(),
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
        'core.edit.own' => array(1=>true)
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_categories';
    require_once $basePath.'/models/category.php';
    $config  = array('table_path' => $basePath.'/tables');
    $category_model = new CategoriesModelCategory($config);
    if(!$category_model->save($data)){
        $err_msg = $category_model->getError();
        return false;
    }else{
        $id = $category_model->getItem()->id;
        return $id;
    }
}
function createArticle($data)
{
    $data['rules'] = array(
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_content';
    require_once $basePath.'/models/article.php';
    $config = array();
    $article_model = new ContentModelArticle($config);
    if(!$article_model->save($data)){
        $err_msg = $article_model->getError();
        return false;
    }else{
        $id = $article_model->getItem()->id;
        return $id;
    }
}
Improved sample code
Source Link
GDP
  • 7.7k
  • 2
  • 30
  • 72
if (!defined('_JEXEC')) {
    define( '_JEXEC', 1 );
    define('JPATH_BASE', realpath(dirname(__FILE__)));
    require_once ( JPATH_BASE .'/includes/defines.php' );
    require_once ( JPATH_BASE .'/includes/framework.php' );
}  
   defined('DS') or define('DS', DIRECTORY_SEPARATOR);
}
$app = JFactory::getApplication('site');

$category_data['id'] = 0;
$category_data['parent_id'] = 0;
$category_data['title'] = 'My Category Title';
$category_data['alias'] = 'my-categegory-title-alias';
$category_data['extension'] = 'com_content';
$category_data['published'] = 1;
$category_data['language'] = '*';
$category_data['params'] = array('category_layout' => '','image' => '');
$category_data['metadata'] = array('author' => '','robots' => 'Index, No Follow''');

$category_id = createCategory($category_data);
if(!$category_id){
    echo "Category create failed!";
}else{
    $article_data = array(
        'id' => 0,
        'catid' => $category_id,
        'title' => 'My article title',
        'alias' => 'my-article-alias',
        'introtext' => 'My intro text',
        'fulltext' => '<p>My full text</p>',
        'state' => 1,
        'language' => '*'
    );
    $article_id = createArticle($article_data);
    if(!$article_id){
        echo "Category create failed!";
    }
}

function createCategory( $data )
{
    $data['rules'] = array(
        'core.edit.state' => array(),
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
        'core.edit.own' => array(1=>true)
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_categories';
    require_once $basePath.'/models/category.php';
    $config  = array('table_path' => $basePath.'/tables');
    $category_model = new CategoriesModelCategory($config);
    if(!$category_model->save($data)){
        $err_msg = $category_model->getError();
        return false;
    }else{
        $id = $category_model->getItem()->id;
        return $id;
    }
}
function createArticle($data)
{
    $data['rules'] = array(
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_content';
    require_once $basePath.'/models/article.php';
    $config = array();
    $article_model = new ContentModelArticle($config);
    if(!$article_model->save($data)){
        $err_msg = $article_model->getError();
        return false;
    }else{
        $id = $article_model->getItem()->id;
        return $id;
    }
}
if (!defined('_JEXEC')) {
    define( '_JEXEC', 1 );
    define('JPATH_BASE', realpath(dirname(__FILE__)));
    require_once ( JPATH_BASE .'/includes/defines.php' );
    require_once ( JPATH_BASE .'/includes/framework.php' );
}  
 defined('DS') or define('DS', DIRECTORY_SEPARATOR);

$category_data['id'] = 0;
$category_data['parent_id'] = 0;
$category_data['title'] = 'My Title';
$category_data['alias'] = 'my-title-alias';
$category_data['params'] = array('category_layout' => '','image' => '');
$category_data['metadata'] = array('author' => '','robots' => 'Index, No Follow');

$category_id = createCategory($category_data);
if(!$category_id){
    echo "Category create failed!";
}else{
    $article_data = array(
        'id' => 0,
        'catid' => $category_id,
        'title' => 'My article title',
        'alias' => 'my-article-alias',
        'introtext' => 'My intro text',
        'fulltext' => '<p>My full text</p>',
        'state' => 1
    );
    $article_id = createArticle($article_data);
    if(!$article_id){
        echo "Category create failed!";
    }
}

function createCategory( $data )
{
    $data['rules'] = array(
        'core.edit.state' => array(),
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
        'core.edit.own' => array(1=>true)
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_categories';
    require_once $basePath.'/models/category.php';
    $config  = array('table_path' => $basePath.'/tables');
    $category_model = new CategoriesModelCategory($config);
    if(!$category_model->save($data)){
        $err_msg = $category_model->getError();
        return false;
    }else{
        $id = $category_model->getItem()->id;
        return $id;
    }
}
function createArticle($data)
{
    $data['rules'] = array(
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_content';
    require_once $basePath.'/models/article.php';
    $config = array();
    $article_model = new ContentModelArticle($config);
    if(!$article_model->save($data)){
        $err_msg = $article_model->getError();
        return false;
    }else{
        $id = $article_model->getItem()->id;
        return $id;
    }
}
if (!defined('_JEXEC')) {
    define( '_JEXEC', 1 );
    define('JPATH_BASE', realpath(dirname(__FILE__)));
    require_once ( JPATH_BASE .'/includes/defines.php' );
    require_once ( JPATH_BASE .'/includes/framework.php' );
    defined('DS') or define('DS', DIRECTORY_SEPARATOR);
}
$app = JFactory::getApplication('site');

$category_data['id'] = 0;
$category_data['parent_id'] = 0;
$category_data['title'] = 'My Category Title';
$category_data['alias'] = 'my-categegory-title-alias';
$category_data['extension'] = 'com_content';
$category_data['published'] = 1;
$category_data['language'] = '*';
$category_data['params'] = array('category_layout' => '','image' => '');
$category_data['metadata'] = array('author' => '','robots' => '');

$category_id = createCategory($category_data);
if(!$category_id){
    echo "Category create failed!";
}else{
    $article_data = array(
        'id' => 0,
        'catid' => $category_id,
        'title' => 'My article title',
        'alias' => 'my-article-alias',
        'introtext' => 'My intro text',
        'fulltext' => '<p>My full text</p>',
        'state' => 1,
        'language' => '*'
    );
    $article_id = createArticle($article_data);
    if(!$article_id){
        echo "Category create failed!";
    }
}

function createCategory( $data )
{
    $data['rules'] = array(
        'core.edit.state' => array(),
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
        'core.edit.own' => array(1=>true)
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_categories';
    require_once $basePath.'/models/category.php';
    $config  = array('table_path' => $basePath.'/tables');
    $category_model = new CategoriesModelCategory($config);
    if(!$category_model->save($data)){
        $err_msg = $category_model->getError();
        return false;
    }else{
        $id = $category_model->getItem()->id;
        return $id;
    }
}
function createArticle($data)
{
    $data['rules'] = array(
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_content';
    require_once $basePath.'/models/article.php';
    $config = array();
    $article_model = new ContentModelArticle($config);
    if(!$article_model->save($data)){
        $err_msg = $article_model->getError();
        return false;
    }else{
        $id = $article_model->getItem()->id;
        return $id;
    }
}
added 319 characters in body
Source Link
GDP
  • 7.7k
  • 2
  • 30
  • 72
if (!defined('_JEXEC')) {
    define( '_JEXEC', 1 );
    define('JPATH_BASE', realpath(dirname(__FILE__)));
    require_once ( JPATH_BASE .'/includes/defines.php' );
    require_once ( JPATH_BASE .'/includes/framework.php' );
}  
defined('DS') or define('DS', DIRECTORY_SEPARATOR);

$category_data['id'] = 0;
$category_data['parent_id'] = 0;
$category_data['title'] = 'My Title';
$category_data['alias'] = 'my-title-alias';
$category_data['params'] = array('category_layout' => '','image' => '');
$category_data['metadata'] = array('author' => '','robots' => 'Index, No Follow');

$category_id = createCategory($category_data);
if(!$category_id){
    echo "Category create failed!";
}else{
    $article_data = array(
        'id' => 0,
        'catid' => $category_id,
        'title' => 'My article title',
        'alias' => 'my-article-alias',
        'introtext' => 'My intro text',
        'fulltext' => '<p>My full text</p>',
        'state' => 1
    );
    $article_id = createArticle($article_data);
    if(!$article_id){
        echo "Category create failed!";
    }
}

function createCategory( $data )
{
    $data['rules'] = array(
        'core.edit.state' => array(),
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
        'core.edit.own' => array(1=>true)
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_categories';
    require_once $basePath.'/models/category.php';
    $config  = array('table_path' => $basePath.'/tables');
    $category_model = new CategoriesModelCategory($config);
    if(!$category_model->save($data)){
        $err_msg = $category_model->getError();
        return false;
    }else{
        $id = $category_model->getItem()->id;
        return $id;
    }
}
function createArticle($data)
{
    $data['rules'] = array(
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_content';
    require_once $basePath.'/models/article.php';
    $config = array();
    $article_model = new ContentModelArticle($config);
    if(!$article_model->save($data)){
        $err_msg = $article_model->getError();
        return false;
    }else{
        $id = $article_model->getItem()->id;
        return $id;
    }
}
$category_data['id'] = 0;
$category_data['parent_id'] = 0;
$category_data['title'] = 'My Title';
$category_data['alias'] = 'my-title-alias';
$category_data['params'] = array('category_layout' => '','image' => '');
$category_data['metadata'] = array('author' => '','robots' => 'Index, No Follow');

$category_id = createCategory($category_data);
if(!$category_id){
    echo "Category create failed!";
}else{
    $article_data = array(
        'id' => 0,
        'catid' => $category_id,
        'title' => 'My article title',
        'alias' => 'my-article-alias',
        'introtext' => 'My intro text',
        'fulltext' => '<p>My full text</p>',
        'state' => 1
    );
    $article_id = createArticle($article_data);
    if(!$article_id){
        echo "Category create failed!";
    }
}

function createCategory( $data )
{
    $data['rules'] = array(
        'core.edit.state' => array(),
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
        'core.edit.own' => array(1=>true)
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_categories';
    require_once $basePath.'/models/category.php';
    $config  = array('table_path' => $basePath.'/tables');
    $category_model = new CategoriesModelCategory($config);
    if(!$category_model->save($data)){
        $err_msg = $category_model->getError();
        return false;
    }else{
        $id = $category_model->getItem()->id;
        return $id;
    }
}
function createArticle($data)
{
    $data['rules'] = array(
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_content';
    require_once $basePath.'/models/article.php';
    $config = array();
    $article_model = new ContentModelArticle($config);
    if(!$article_model->save($data)){
        $err_msg = $article_model->getError();
        return false;
    }else{
        $id = $article_model->getItem()->id;
        return $id;
    }
}
if (!defined('_JEXEC')) {
    define( '_JEXEC', 1 );
    define('JPATH_BASE', realpath(dirname(__FILE__)));
    require_once ( JPATH_BASE .'/includes/defines.php' );
    require_once ( JPATH_BASE .'/includes/framework.php' );
}  
defined('DS') or define('DS', DIRECTORY_SEPARATOR);

$category_data['id'] = 0;
$category_data['parent_id'] = 0;
$category_data['title'] = 'My Title';
$category_data['alias'] = 'my-title-alias';
$category_data['params'] = array('category_layout' => '','image' => '');
$category_data['metadata'] = array('author' => '','robots' => 'Index, No Follow');

$category_id = createCategory($category_data);
if(!$category_id){
    echo "Category create failed!";
}else{
    $article_data = array(
        'id' => 0,
        'catid' => $category_id,
        'title' => 'My article title',
        'alias' => 'my-article-alias',
        'introtext' => 'My intro text',
        'fulltext' => '<p>My full text</p>',
        'state' => 1
    );
    $article_id = createArticle($article_data);
    if(!$article_id){
        echo "Category create failed!";
    }
}

function createCategory( $data )
{
    $data['rules'] = array(
        'core.edit.state' => array(),
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
        'core.edit.own' => array(1=>true)
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_categories';
    require_once $basePath.'/models/category.php';
    $config  = array('table_path' => $basePath.'/tables');
    $category_model = new CategoriesModelCategory($config);
    if(!$category_model->save($data)){
        $err_msg = $category_model->getError();
        return false;
    }else{
        $id = $category_model->getItem()->id;
        return $id;
    }
}
function createArticle($data)
{
    $data['rules'] = array(
        'core.edit.delete' => array(),
        'core.edit.edit' => array(),
        'core.edit.state' => array(),
    );

    $basePath = JPATH_ADMINISTRATOR.'/components/com_content';
    require_once $basePath.'/models/article.php';
    $config = array();
    $article_model = new ContentModelArticle($config);
    if(!$article_model->save($data)){
        $err_msg = $article_model->getError();
        return false;
    }else{
        $id = $article_model->getItem()->id;
        return $id;
    }
}
added 331 characters in body
Source Link
GDP
  • 7.7k
  • 2
  • 30
  • 72
Loading
Source Link
GDP
  • 7.7k
  • 2
  • 30
  • 72
Loading