I am not very familiar with Wordpress terminology yet, so this may be why my Google results come back empty but I guess this is a question that troubles a lot of developers with Drupal background that move to Wordpress.

The short version:

"Is there a way to programmatically add a menu item like mywordpresssite.com/my/custom/url/%/% (where % are wildcards) and define what this page will return in my code?"

The extended version:

In Drupal there is a hook called hook_menu where you can define a menu path like the one mentioned above and a callback function that will take the wildcards as arguments.

What I'm trying to do here is simple: create a button on every post of the site; when this button is clicked in a specific post, it will do a simple GET request via AJAX to a menu item like: mywordpresssite.com/ajax-calls/%current_post_id which will allow me to do some further processing of the specific post in the background.

I hope that the above makes sense. If this is not the way that things work in Wordpress, please provide any resources as to how I should go about approaching the task at hand.


This code can be used after the new post was saved. Otherwise there is no post object at the time.

Please pay attention that the code was not tested.

// get post ID from the URL query
$post_id = $_GET['post'];

// get post object
$new_item_to_add = get_post($post_id, OBJECT);

// get menu to add the item into (change to the relevant slug, ID, or name)
$existing_menu = wp_get_nav_menu_object('test-nav-menu-slug');
$existing_menu_id = $existing_menu->term_id;

$new_menu_item_data =  array(
    'menu-item-db-id' => $menu_item_db_id, // default 0, creates a new one
    'menu-item-object-id' => $post_id,
    'menu-item-object' => '', //default
    'menu-item-parent-id' => 0, //default
    'menu-item-position' => 0, //default
    'menu-item-type' => $new_item_to_add->post_type,
    'menu-item-title' => $new_item_to_add->post_title,
    'menu-item-url' => get_permalink($post_id),
    'menu-item-description' => '', //default
    'menu-item-attr-title' => '', //default
    'menu-item-target' => '', //default
    'menu-item-classes' => '', //default
    'menu-item-xfn' => '', //default
    'menu-item-status' => 'publish',

wp_update_nav_menu_item($existing_menu_id, $menu_item_db_id = 0, $new_menu_item_data);

See wp_update_nav_menu_item code reference.

Also, there is a worse way. You can alter the menu on-the-fly. This will not update the menu:

function wpse267737_add_nav_menu_items($nav_menu_items) {

    // get post ID from URL
    $post_id = $_GET['post'];

    // get current post data
    $link_url = get_permalink( $post_id );
    $link_anchor =  get_the_title( $post_id );

    // build new menu item
    $new_menu_item = '<li><a href="' . $link_url . '">' . $link_anchor . '</a></li>';

    // concatenate existing items and new
    $nav_menu_items .= $new_menu_item;

    return $nav_menu_items;
// change TEST-MENU-SLUG to the relevant menu slug
add_filter( 'wp_nav_menu_TEST-MENU-SLUG_items', 'wpse267737_add_nav_menu_items' );

See wp_nav_menu_items reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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