WordPress

WordPress TutorialRemove Version from WordPress and StylesheetsRun WordPress local with XAMPPSecurity in WordPress - EscapingSecurity in WordPress - SanitizationThe Loop (main WordPress loop)Update WordPress ManuallyWordPress Actions and FiltersWordPress add action()WordPress add editor style()WordPress add menu page()WordPress Add ShortcodeWordPress add submenu page()WordPress add theme support()WordPress Add/remove contact info for users with user contactmethods filter hookWordPress Admin Dashboard WidgetsWordPress AJAXWordPress Alternating main loop (pre get posts filter)WordPress Child Theme BasicsWordPress Create a Post ProgrammaticallyWordPress Create Template for Custom Post TypeWordPress Creating a custom templateWordPress Custom exerpts with excerpt length and excerpt moreWordPress Custom Post TypesWordPress Customizer Basics (Add Panel, Section, Setting, Control)WordPress Customizer Hello WorldWordPress DebuggingWordPress Enqueuing scriptsWordPress Enqueuing StylesWordPress Function : wp trim words()WordPress Function: add action()WordPress get bloginfo()WordPress get home path()WordPress get option()WordPress get permalink()WordPress get template part()WordPress get the category()WordPress get the title()WordPress home url()WordPress How Can I integrate Markdown editor with Advance Custom Fields repeater Add-on.WordPress initWordPress Installation and ConfigurationWordPress Making network requests with HTTP APIWordPress Meta BoxWordPress Options APIWordPress Plugin creationWordPress Plugin developmentWordPress Post FormatsWordPress Querying postsWordPress Remove Auto Line Breaks from Content and ExcerptWordPress REST APIWordPress Secure your installationWordPress ShortcodeWordPress Shortcode with attributeWordPress ShortcodesWordPress SidebarsWordPress Site MigrationWordPress TaxonomiesWordPress Template hierarchyWordPress template includeWordPress the $wpdb ObjectWordPress the Admin Bar (aka The Toolbar)WordPress the title()WordPress theme and child-theme developmentWordPress ThemesWordPress wp get current user()WordPress WP Query() LoopWordPress WP-CLIWordPress WP-Cron



WordPress add menu page()

From WikiOD

This function is to add an item in the admin panel's nav bar.

Syntax[edit | edit source]

  • add_menu_page( $page_title, $menu_title, $capability, $menu_slug, $function, $icon_url, $position)

Parameters[edit | edit source]

Parameter Details
$page_title (string) The text to be displayed in the title tags of the page when the menu is selected.
$menu_title (string) The text to be used for the menu.
$capability (string) The capability required for this menu to be displayed to the user.
$menu_slug (string) The slug name to refer to this menu by (should be unique for this menu).
$function (callable) (optional) The function to be called to output the content for this page.
$icon_url (string) (optional) The URL to the icon to be used for this menu.
$position (int) (optional) The position in the menu order this one should appear.

Remarks[edit | edit source]

Here is a list of the default positions (for $position)

  • 2 – Dashboard
  • 4 – Separator
  • 5 – Posts
  • 10 – Media
  • 15 – Links
  • 20 – Pages
  • 25 – Comments
  • 59 – Separator
  • 60 – Appearance
  • 65 – Plugins
  • 70 – Users
  • 75 – Tools
  • 80 – Settings
  • 99 – Separator

Adding the "Theme page title" item to the nav bar[edit | edit source]

Code

function add_the_theme_page(){
    add_menu_page('Theme page title', 'Theme menu label', 'manage_options', 'theme-options', 'page_content', 'dashicons-book-alt');
}
add_action('admin_menu', 'add_the_theme_page');
function page_content(){
    echo '<div class="wrap"><h2>Testing</h2></div>';
}

Output

add_menu_page()

Explanation

In the code, we created a function named add_the_theme_page and we used add_menu_page to add the item to the navbar. Please check the parameters part in this page to know about the arguments we passed in. Then we used add_action to run our add_the_theme_page function. Finally, we created the function page_content to display contents in the page.

OOP & how to load scripts/styles on menu page[edit | edit source]

<?php
/*
 *  Plugin Name: Custom Admin Menu
 */

class SO_WP_Menu {

    private $plugin_url;

    public function __construct() {
        $this->plugin_url = plugins_url( '/', __FILE__ );
        add_action( 'plugins_loaded', array( $this, 'init' ) );
    }

    public function init() {
        add_action( 'admin_menu', array( $this, 'add_menu' ) );
    }

    public function add_menu() {
        $hook = add_menu_page(
            'My Menu',                 // Title, html meta tag
            '<span style="color:#e57300;">My Menu</span>', // Menu title, hardcoded style
            'edit_pages',              // capability
            'dummy-page-slug',         // URL
            array( $this, 'content' ), // output
            null,                      // icon, uses default
            1                          // position, showing on top of all others
        );
        add_action( "admin_print_scripts-$hook", array( $this, 'scripts' ) );
        add_action( "admin_print_styles-$hook", array( $this, 'styles' ) );
    }

    public function content() {
        ?>
            <div id="icon-post" class="icon32"></div>
            <h2>Dummy Page</h2>
            <p> Lorem ipsum</p>
        <?php
    }

    # Printing directly, could be wp_enqueue_script
    public function scripts() {
        ?><script>alert('My page');</script><?php
    }

    # Enqueing from a CSS file on plugin directory
    public function styles() {
        wp_enqueue_style( 'my-menu', $this->plugin_url . 'my-menu.css' );
    }
}

new SO_WP_Menu();

What's important to note in this example is that, when using add_menu_page(), it returns a hook that can be used to target our exact page and load Styles and Scripts there.

A common mistake is to enqueue without targeting and that spills scripts and styles all over /wp-admin.

Using OOP we can store common variables to be used among internal methods.

This article is an extract of the original Stack Overflow Documentation created by contributors and released under CC BY-SA 3.0. This website is not affiliated with Stack Overflow