Using WordPress ‘add_posts_page()’ PHP function

The add_posts_page() WordPress PHP function adds a submenu page to the Posts main menu in your WordPress admin area.

Usage

To use this function, you can include it inside a function that’s registered with the admin_menu hook. Here’s a custom example:

function add_my_custom_posts_page() {
    add_posts_page( 
        'My Custom Posts Page', 
        'My Custom Page', 
        'manage_options', 
        'my-custom-page', 
        'display_my_custom_page_content' 
    );
}
add_action( 'admin_menu', 'add_my_custom_posts_page');

In this case, ‘My Custom Posts Page’ will appear as a submenu under ‘Posts’ in the WordPress admin menu.

Parameters

  • $page_title (string) – The text displayed in the title tags of the page when the menu is selected.
  • $menu_title (string) – The text 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).
  • $callback (callable – optional) – The function to be called to output the content for this page.
  • $position (int – optional) – The position in the menu order this item should appear.

More information

See WordPress Developer Resources: add_posts_page()

Examples

Add a Custom Posts Page

This example adds a ‘My Custom Page’ submenu to the ‘Posts’ menu.

function add_my_custom_posts_page() {
    add_posts_page( 
        'My Custom Posts Page', 
        'My Custom Page', 
        'manage_options', 
        'my-custom-page', 
        'display_my_custom_page_content' 
    );
}
add_action( 'admin_menu', 'add_my_custom_posts_page');

Add a Posts Page with Custom Position

You can specify the position of your submenu in the menu order. Let’s add a page at position 5:

function add_my_custom_posts_page() {
    add_posts_page( 
        'My Custom Posts Page', 
        'My Custom Page', 
        'manage_options', 
        'my-custom-page', 
        'display_my_custom_page_content',
        5
    );
}
add_action( 'admin_menu', 'add_my_custom_posts_page');

Display Content on the Custom Page

You can also display custom content on your new page. Let’s create a callback function that will display a message:

function display_my_custom_page_content(){
    echo 'Welcome to My Custom Page!';
}

function add_my_custom_posts_page() {
    add_posts_page( 
        'My Custom Posts Page', 
        'My Custom Page', 
        'manage_options', 
        'my-custom-page', 
        'display_my_custom_page_content' 
    );
}
add_action( 'admin_menu', 'add_my_custom_posts_page');

Restrict Access to the Page

You can restrict who can see your new page by setting a user capability. Here, we set it to ‘edit_posts’ so only users who can edit posts can see the page:

function add_my_custom_posts_page() {
    add_posts_page( 
        'My Custom Posts Page', 
        'My Custom Page', 
        'edit_posts', 
        'my-custom-page', 
        'display_my_custom_page_content' 
    );
}
add_action( 'admin_menu', 'add_my_custom_posts_page');