Menü schliessen
Created: January 24th 2025
Last updated: January 24th 2025
Categories: Common Web Development,  IT Development,  Wordpress
Author: Tim Fürer

WordPress: How to Create Custom Admin Page

Tags:  guide,  PHP,  wordpress
Donation Section: Background
Monero Badge: QR-Code
Monero Badge: Logo Icon Donate with Monero Badge: Logo Text
82uymVXLkvVbB4c4JpTd1tYm1yj1cKPKR2wqmw3XF8YXKTmY7JrTriP4pVwp2EJYBnCFdXhLq4zfFA6ic7VAWCFX5wfQbCC

WordPress offers functions for registering your own custom backend pages as well as sub-pages. Here's how to do it.


Page Render Function

Whether you want to create a top-level admin page or a sub-page, you must set up a render function that handles generating the HTML structure for the page.

Example:

<?php

function render_test_page() {
    ?>

    <div class="wrap">
        <h1>
            Test Admin Page
        </h1>

        <p>
            Hello World
        </p>
    </div>

    <?php
}

?>

Registering The Page

Now comes the time to register your new page.

For a top-level admin page, use the add_menu_page() function:

<?php

function add_test_page() {
    add_menu_page(
        'Page Title',
        'Menu Title',
        'manage_options', // capability
        'test-page', // slug
        'render_test_page' // render function
    );
}
add_action('admin_menu', 'add_test_page');

?>

For a sub-page, use the aptly named add_submenu_page() function:

<?php

function add_test_sub_page() {
    add_submenu_page(
        'test-page', // parent slug
        'Page Title',
        'Menu Title',
        'manage_options', // capability
        'test-sub-page', // slug
        'render_test_page' // render function
    );
}
add_action('admin_menu', 'add_test_sub_page', 100);

?>

Notice the priority when registering the sub-page. It's crucial that the action is executed after the parent page is registered.