Creating custom admin menu page

To create custom menu page in admin panel we simply need to execute add_menu_page function on “admin_menu” action.

We will follow 2 steps in functions.php file:

Step 1:

  • Create function which will generate page content.

Step 2:

  • Create our custom function which execute add_menu_page function.
  • Hook our custom function on “admin_menu” action.

STEP 1. Create function which outputs HTML content. In my example i created get_custom_page_content function.

function get_custom_page_content() {

    echo '<h1>'. get_admin_page_title() .'</h1>';

    echo'<tableclass="data">';
    echo'<tr>';
    echo'<th>EntryHeader1</th>';
    echo'<th>EntryHeader2</th>';
    echo'<th>EntryHeader3</th>';
    echo'<th>EntryHeader4</th>';
    echo'</tr>';
    echo'<tr>';
    echo'<td>EntryFirstLine1</td>';
    echo'<td>EntryFirstLine2</td>';
    echo'<td>EntryFirstLine3</td>';
    echo'<td>EntryFirstLine4</td>';
    echo'</tr>';
    echo'<tr>';
    echo'<td>EntryLine1</td>';
    echo'<td>EntryLine2</td>';
    echo'<td>EntryLine3</td>';
    echo'<td>EntryLine4</td>';
    echo'</tr>';
    echo'<tr>';
    echo'<td>EntryLastLine1</td>';
    echo'<td>EntryLastLine2</td>';
    echo'<td>EntryLastLine3</td>';
    echo'<td>EntryLastLine4</td>';
    echo'</tr>';
    echo'</table>';

}

If you want your page to look more like wordpress default. You can find more information here: WP List Table


STEP 2. Create function which will call add_menu_page function.

add_menu_page function has 7 parameters:

Required:
$page_title – Text which will be displayed in title tags.
$menu_title – Text which will be displayed in menu.
$capability – determine which users can access this page. More about wordpress user roles and capabilities. I chose “manage_options” because it refers to page administrators.
$menu_slug – slug which refers to this page. Must be unique.

Optional:
$function – function which will be called to output content.
$icon_url – icon to display near menu title. To display wordpress default dashicons you need provide only class name.
$position – position in menu order.

For our page page will use only $page_title, $menu_title, $capability, $menu_slug, $function parameters.

function register_my_custom_menu_page() {

    add_menu_page( 'Custom Menu Page Title', 'Custom Page', 'manage_options', 'custom.php', 'get_custom_page_content' );

}

add_action( 'admin_menu', 'register_my_custom_menu_page' );

Final code:

function get_custom_page_content() {

    echo '<h1>'. get_admin_page_title() .'</h1>';

    echo'<tableclass="data">';
    echo'<tr>';
    echo'<th>EntryHeader1</th>';
    echo'<th>EntryHeader2</th>';
    echo'<th>EntryHeader3</th>';
    echo'<th>EntryHeader4</th>';
    echo'</tr>';
    echo'<tr>';
    echo'<td>EntryFirstLine1</td>';
    echo'<td>EntryFirstLine2</td>';
    echo'<td>EntryFirstLine3</td>';
    echo'<td>EntryFirstLine4</td>';
    echo'</tr>';
    echo'<tr>';
    echo'<td>EntryLine1</td>';
    echo'<td>EntryLine2</td>';
    echo'<td>EntryLine3</td>';
    echo'<td>EntryLine4</td>';
    echo'</tr>';
    echo'<tr>';
    echo'<td>EntryLastLine1</td>';
    echo'<td>EntryLastLine2</td>';
    echo'<td>EntryLastLine3</td>';
    echo'<td>EntryLastLine4</td>';
    echo'</tr>';
    echo'</table>';

}

function register_my_custom_menu_page() {

    add_menu_page( 'Custom Menu Page Title', 'Custom Page', 'manage_options', 'custom.php', 'get_custom_page_content' );

}

add_action( 'admin_menu', 'register_my_custom_menu_page' );