I'm trying to create a menu item in joomla 3.x using php

I've been trying to get this code to work:

$menuItem = array(
    'menutype' => 'client-pages',
    'title' => "new Menu",
    'type' => 'component',
    'component_id' => 22,
    'link' => 'index.php?option=com_content&view=article&id='.$articleid,
    'language' => '*',
    'published' => 1,
    'parent_id' => $parent_id,
    'level' => 1,

$menuTable = JTable::getInstance('Menu', 'JTable', array());

$menuTable->setLocation($parent_id, 'last-child');

if (!$menuTable->save($menuItem)) {
    throw new Exception($menuTable->getError());
    echo "exception thrown";
    return false;

echo "<pre>";
echo "</pre>";

It doesn't throw errors and the print_r menu table makes it look like it's going to create the menu item

Joomla\CMS\Table\Menu Object
    [parent_id] => 5536
    [level] => 1
    [lft] => 327
    [rgt] => 328
    [alias] => new-menu
    [_location:protected] => last-child
    [_location_id:protected] => 
    [_cache:protected] => Array
            [rebuild.sql] => 
SELECT id, alias
FROM #__menu
WHERE parent_id = %d
ORDER BY parent_id, lft
        ) ....

However no menu item is created - can someone suggest what I am doing wrong?


1 Answer 1


For starters, perhaps just an oversight on bringing your code to the JSE platform here, but...

$menuTable = JTable::getInstance('Menu', 'JTable', array());

You are passing in an empty array.

$newMenu = new stdClass();

Then you can do:

$menuTable        = JTable::getInstance('menu', 'JTable');
$menuData         = (array)$newMenu;

$menuTable->setLocation($parent_id, 'last-child');

You are missing the following required properties in your $menuItem array.

  • alias
  • path
  • template_style_id
  • params

I'd also suggest doing this in a try/catch, which will be easier to diagnose.

    if ($menuTable->save($menuItem))
        echo "Success";
        echo "Failed";
catch (Exception $e)
    throw new Exception('You might want to substitute this for another function, but $e will be your error exception', 500);
return $menuTable->id;
  • thanks for the suggestion. I couldn't get it to work - I don't think I understand how $menuData = (array)$jMenus; fits into the code Jun 22, 2020 at 11:42
  • Apologies, updated code, it should read $menuData = (array)$newMenu
    – Grant G
    Jun 22, 2020 at 11:47
  • thanks got it to work - Jun 22, 2020 at 14:22
  • I don't know that the empty $config array as the 3rd param of getInstance() is a problem. docs.joomla.org/Using_the_JTable_class , api.joomla.org/cms-3/classes/… and save() will accept an object or an array as the first param: api.joomla.org/cms-3/classes/… It is important that we do not misinform the OP and researchers. Can you show me the docs page that shows the required data for the $menuData @Grant? Jun 22, 2020 at 22:58
  • @mickmackusa both the $prefix and $config are optional as per menu.php model. The reason I know about those missing properties is through my own trial and error. The code in my OP is the code I am currently using - never did find the requirements per se, but I do suspect the lack of alias and params was the root cause of the issue here as these are always set in both the model and menu helpers.
    – Grant G
    Jun 23, 2020 at 0:28

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.