WordPress edit menu through code

Is there any way that you can edit a WordPress menu through code, when creating a plugin?

My goal is to show a menu-item at the end of the primary menu, where you can edit your profile information. I only want this behavior if the plugin is activated.

Read More

Any thoughts on this one? It should be used in the plugin and NOT in the theme!

Related posts

Leave a Reply

1 comment

  1. I would personally not recommend automatically adding the links when a plugin is initialized, especially if this plugin is going to be open and downloaded by many people, if its for a very specific theme then i would put it into the theme functions.php file to use.

    There are a number of ways to do it with jQuery or PHP but there is a very simple solution to it within PHP which i think suits your needs best.

    Use the snippet below:

    add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
    function add_loginout_link( $items, $args ) {
        $items .= '<li><a href="<!-- LINK TO PAGE -->">Edit Profile</a></li>';
        return $items;
    }
    

    This appends a new item onto the current nav menu items and then returns it back.

    You may want to add in some code that does verification on the user being logged in etc.