13

Is it possible to dynamically create Joomla categories, subcategories and articles using PHP scripts?

Is there any example link?

2
  • 2
    Yes it is possible but very hard and I would suggest against it unless you are extremely familiar with PHP as com_content is the most complex built-in extension in Joomla. You might want to approach this a different way, possible extending some of the current functionality and writing your own class
    – Lodder
    Nov 23, 2014 at 17:46
  • @Lodder thank you for your answer. To be honest I have a knowledge of php and I use Joomla using the backend interface. What I want to make is to have a site in which I added some features using Joomla admin console (menu, pages, etc.) and I want to make minor changes using php scripts. Is there any kind of tutorial to make it? Nov 23, 2014 at 17:51

3 Answers 3

17

It's a rather extensive process to do this, but here's a trimmed down version of the code that I generally use. I won't necessarily guarantee that it will run as shown since I ripped out a bunch of internal stuff that we do along the way, but these are the pieces you need to do it.

You'll need to look at tables #__categories and #__content to get all the fields you want to populate and add them to the given $category_data and $article_data arrays.

I should mention that you could accomplish it inserting the records with jDatabase or jTable, but I'd not generally recommend that because the important thing that you'd be missing is the built-in Joomla rules and logic that do things like checking the uniqueness of the alias, and managing the ACL for the new content items.

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;
    }
}
11
  • Joomla does have to be loaded of course, so if you're not doing in some existing code, you need to load it. I've updated my answer to accomplish that, so you're good to go.
    – GDP
    Nov 24, 2014 at 0:44
  • Not for this code, it uses the Joomla models to add them just like Joomla does itself. You should be able to put this code into a php file, complete the $data array with other fields that you want to update and successfully run it. Give me a few minutes, and I'll update my answer to include 100% functional code.
    – GDP
    Nov 26, 2014 at 18:00
  • The updated code now creates a category and an article within it when executed as is. FYI, if you specify a valid ID instead of 0 for either, it will update that category or article instead of adding it. Also, the ACL rules are optional...I copied this from one of my scripts that uses ACL, so that's why its there, but it's handy to know as well.
    – GDP
    Nov 26, 2014 at 18:12
  • 1
    I can only suggest that you formulate what you want to accomplish without assuming that templates or MVC are parts of it. Plugins are generally the best way to customize what Joomla does, but clarity of what you want to do is most important to determine what the best solution is.
    – GDP
    Nov 26, 2014 at 23:26
  • 2
    This solution is still working nicely in Joomla! 3.9.11 Aug 22, 2019 at 14:08
3

There is at least one extension, "OSContent" in the Joomla Extensions Directory that you could use as an example:

http://extensions.joomla.org/extensions/miscellaneous/mass-content/18000

OSContent allows you to create articles and categories in bulk.

0

To create a Category in Joomla 4:

$category_data['id'] = 0;
$category_data['parent_id'] = 0;
$category_data['title'] = "my title" ;
$category_data['alias'] = "my 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' => '');

$newapp = JFactory::getApplication();
$mvcFactory = $newapp->bootComponent('com_categories')->getMVCFactory();
$catModel = $mvcFactory->createModel('Category', 'Administrator', ['ignore_request' => true]);

$category_id = $catModel->save($category_data);

if (!$category_id ){
    throw new Exception('Can't create Category...');
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.