-
-
Save brnl/9dfc85d541c9f774ec7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* | |
* Procedure for creating categories programmatically in Joomla 2.5 and Joomla 3.x | |
* | |
* @version 1.0 | |
* - Removed unused database instance | |
* - Added variables for main fields with description for usage | |
* | |
* Original by Michael Babker (mbabker) | |
* https://gist.github.com/mbabker/3211464 | |
* | |
* Edit by brnl (https://gist.github.com/brnl) | |
* | |
*/ | |
// Set the extension. For content categories, use 'com_content' | |
$extension = 'com_content'; | |
// Set the title for the category | |
$title = 'My Category'; | |
// Type the description, this is also the 'body'. HTML allowed here. | |
$desc = 'A category for my extension'; | |
// Set the parent category. 1 is the root item. | |
$parent_id = 1; | |
// JTableCategory is autoloaded in J! 3.0, so... | |
if (version_compare(JVERSION, '3.0', 'lt')) | |
{ | |
JTable::addIncludePath(JPATH_PLATFORM . 'joomla/database/table'); | |
} | |
// Initialize a new category | |
$category = JTable::getInstance('Category'); | |
$category->extension = $extension; | |
$category->title = $title; | |
$category->description = $desc; | |
$category->published = 1; | |
$category->access = 1; | |
$category->params = '{"target":"","image":""}'; | |
$category->metadata = '{"page_title":"","author":"","robots":""}'; | |
$category->language = '*'; | |
// Set the location in the tree | |
$category->setLocation($parent_id, 'last-child'); | |
// Check to make sure our data is valid | |
if (!$category->check()) | |
{ | |
JError::raiseNotice(500, $category->getError()); | |
return false; | |
} | |
// Now store the category | |
if (!$category->store(true)) | |
{ | |
JError::raiseNotice(500, $category->getError()); | |
return false; | |
} | |
// Build the path for our category | |
$category->rebuildPath($category->id); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment