• I am learning WordPress Plugin Development. I would like to add a Menu in Admin Panel to insert new News item. How can I do that ? My code is like below.

    public function admin_menu() {
        add_menu_page( 'News Information', 'News Info', 'manage_options', 'news_info', [$this, 'api_key_page'], 'dashicons-info-outline', 30 );
        add_submenu_page( 'news_info', 'News Information', 'API Key', 'manage_options', 'news_info_page', [$this, 'api_key_page'], 0 );
        add_submenu_page( 'news_info', 'News Information', 'Add News', 'manage_options', 'news_info', 'dsfdasfasf', 1 );
    }

    • This topic was modified 1 year, 10 months ago by mabufoysal.
    • This topic was modified 1 year, 10 months ago by mabufoysal.
Viewing 14 replies - 1 through 14 (of 14 total)
  • I am not able to create a professional menu.

    Moderator bcworkz

    (@bcworkz)

    @pawankashyap22 — please start your own topic and do not tag onto other’s topics. You may subscribe to other topics you have interest in, but expecting other members to respond to your specific question here is a form of thread hijacking that is seriously frowned upon in these forums.

    @mabufoysal — did you hook your function into the “admin_menu” action? See the first User Contributed Note example on the add_menu_page() doc. Your api_key_page() function is responsible for managing the menu page’s content, as well as processing form data. That is, unless you utilize the Settings API to generate page content.

    Thread Starter mabufoysal

    (@mabufoysal)

    Thanks @bcworkz . I would like to create sub menu like below Add New of Tutorials.
    https://i.stack.imgur.com/X8XgB.png
    How can I do that ?

    Moderator bcworkz

    (@bcworkz)

    Use add_submenu_page() like you have for news information, except you’ll need the menu page slug for the top level menu item of Tutorials. Its equivalent of the ‘news_info’ value for your News Information menu item. To find it, look at the URL of the Tutorials menu page. It’s the “page” query string value.

    When you hook “admin_menu” action, use a large priority arg to help ensure you add submenus after the “Add New” item had already been added.
    add_action('admin_menu', 'admin_menu', 200 );

    BTW, admin_menu() is not a very good callback name, it’s too generic and could cause a name collision. Ideally, you’d prefix the callback name with a short unique string, perhaps “mab”. Also, the name should briefly describe what it’s doing. So mab_add_menu_pages() might be a better name ??

    Thread Starter mabufoysal

    (@mabufoysal)

    Thanks @bcworkz .

    My issue is in callback function of this code add_submenu_page( 'news_info', 'News Information', 'Add News', 'manage_options', 'news_info', 'dsfdasfasf', 1 );.

    What should I put in callback function if I would like to add New News item ?

    Moderator bcworkz

    (@bcworkz)

    You can pass a path/filename relative to wp-admin/ as the callback arg. For example:
    'post-new.php?post_type=news' instead of 'dsfdasfasf'

    It’s usually not necessary to Add New since WP will normally provide an Add New item for any custom post types you might add, but it’s a useful method if you want to utilize a different menu structure other than default.

    Thread Starter mabufoysal

    (@mabufoysal)


    Thanks @bcworkz . What could be ideal code for this ? Could you please share some sample code ? Thanks.

    Moderator bcworkz

    (@bcworkz)

    Add the following to your admin_menu() callback function, which I assume is hooked to “admin_menu” action:
    add_submenu_page( 'news_info', 'News Information', 'Add News', 'manage_options', 'news_info', 'post-new.php?post_type=news', 1 );

    Thread Starter mabufoysal

    (@mabufoysal)

    Thanks @bcworkz . I used your code and got this error.

    Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'post-new.php?post_type=weather' not found or invalid function name in /home/foysal/www/html/wordpressbosta/wordpresspluginweather/wp-includes/class-wp-hook.php on line 308

    • This reply was modified 1 year, 10 months ago by mabufoysal.
    Moderator bcworkz

    (@bcworkz)

    OK then, looks like that trick doesn’t work any more. It’s based on an old example that I’ve not tested in a long time. Thanks for testing it ??

    What we can do now is go back to providing a valid callback and have the callback do a JS redirect. It’s a little crude and users see a brief blank page during the redirect, but it does get them to the right page. So back to:
    add_submenu_page( 'news_info', 'News Information', 'Add News', 'manage_options', 'news_info', 'dsfdasfasf', 1 );
    Then for the callback do this:

    function dsfdasfasf() {
    	$location = admin_url('post-new.php?post_type=news');
    	echo "<script>window.location.href = \"$location\"</script>";
    	exit;
    }
    Thread Starter mabufoysal

    (@mabufoysal)

    Thanks @bcworkz . Is it the right way ? What other plugin developers do in their plugin ?

    Moderator bcworkz

    (@bcworkz)

    If “right way” == “it works” ?? It’s not ideal. I’ve not seen any plugins whose menu item goes to a default admin screen, I don’t know what they would do. Usually when you add a post type that should be user editable, you expose it to the UI and WP handles the menu items itself.

    Probably the way to handle it would be to implement your plugin’s settings screen as usual, and include a Add New button on the screen. Instead of having a menu item that goes there directly.

    Thread Starter mabufoysal

    (@mabufoysal)

    Thanks @bcworkz . Could you please show me some sample code ?

    Is it possible to get Menu Name using below code ?

    <?php // Use the init hook. 
    add_action( ‘init’, wpse_412823_register_tutorials’ ); 
    function wpse_412823_register_tutorials() { // Set up arguments for the post type.
    $args = array( // There are many arguments, but this will make a bare-minimum post type.
     ‘public’ => true, 
     ‘label’ => ‘News’, 
    ); 
    // Finally, register the post type.
    register_post_type( ‘tutorial’, $args ); } 
    ?>

    • This reply was modified 1 year, 10 months ago by bcworkz. Reason: code format fixed
    Moderator bcworkz

    (@bcworkz)

    function dsfdasfasf() {
            // assume there is admin CSS that styles a.button text to look like a button.
    	$location = admin_url('post-new.php?post_type=news');
    	echo "<a href=\"$location\" class=\"button\">Add New News Item</a><br>";
            echo '<p>Other plugin settings can go here</p>';
    }

    Front end (for wp_nav_menu()) menu names are “nav_menu” taxonomy terms for “nav_menu_item” post type. You could query for a specific name, but you need some kind of criteria to differentiate it from others. For example, if you had a menu item ID, you could use it to learn what taxonomy terms are assigned to it, thus learning the menu name.

    Or do you mean admin menu name? The admin menu doesn’t have a name per se, as in something saved in the DB. Its structure is saved in the globals $menu and $submenu.

Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘Add Menu’ is closed to new replies.