Using WordPress ‘add_comments_page()’ PHP function

The add_comments_page() WordPress PHP function adds a submenu page to the Comments main menu.

Usage

To use the add_comments_page() function, you would typically call it inside a function hooked to the ‘admin_menu’ action, like this:

add_action('admin_menu', 'add_my_custom_comments_page');

function add_my_custom_comments_page() {
    add_comments_page(
        'My Custom Comments', 
        'Custom Comments', 
        'manage_options', 
        'custom-comments-page', 
        'display_custom_comments_page'
    );
}

In this example, the function add_my_custom_comments_page() is hooked to the ‘admin_menu’ action. When WordPress constructs the admin menu, it calls this function, which in turn calls add_comments_page() with the necessary parameters.

Parameters

  • $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).
  • $callback (callable) – The function to be called to output the content for this page. Default is ”.
  • $position (int) – The position in the menu order this item should appear. Default is null.

More information

See WordPress Developer Resources: add_comments_page()

Examples

Display a custom comments page

In this example, we’ll create a new submenu item in the Comments menu that displays a custom comments page.

function display_custom_comments_page() {
    echo 'Welcome to the custom comments page!';
}

add_action('admin_menu', 'add_my_custom_comments_page');

function add_my_custom_comments_page() {
    add_comments_page(
        'My Custom Comments', 
        'Custom Comments', 
        'manage_options', 
        'custom-comments-page', 
        'display_custom_comments_page'
    );
}

Add a comments page with a specific position

In this example, we’ll add a comments page with a specific position in the menu order.

add_action('admin_menu', 'add_my_custom_comments_page');

function add_my_custom_comments_page() {
    add_comments_page(
        'My Custom Comments', 
        'Custom Comments', 
        'manage_options', 
        'custom-comments-page', 
        'display_custom_comments_page',
        5 // Position
    );
}

Add a comments page for users with a specific capability

In this example, we’re adding a comments page that is only visible to users with the ‘publish_posts’ capability.

add_action('admin_menu', 'add_my_custom_comments_page');

function add_my_custom_comments_page() {
    add_comments_page(
        'My Custom Comments', 
        'Custom Comments', 
        'publish_posts', 
        'custom-comments-page', 
        'display_custom_comments_page'
    );
}