
In this tutorial I will be teaching you on how to create a page for your WordPress plugin or WordPress theme in the Appearance Main Menu on WordPress dashboard using add_theme_page() function.
add_theme_page() function is one of the WordPress functions used to add submenu page to Appearance main menu and this can achieved with the help of the the parameters defined below;
add_theme_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = ”, int $position = null )
Parameters
$page_title – (string) (Required) The text to be displayed in the title tags of your page.
$menu_title – (string) (Required) The text that will be displayed in appearance submenu. Usually this can be your plugin or theme name.
$capability – (string) (Required) The capability required for this menu to be displayed to the user.
$menu_slug – (string) (Required) The slug name to refer to this menu by and should be unique. This slug show up in the browser URL of your page.
$function – (callable) (Optional) The function to be called to output the content for this page. In our example below our function is show_my_theme_page().
$position – (int) (Optional) The position in the menu order this item should appear. Default value: null. In our example the position is set to 5.
Now we have to create a function called add_test_theme_page() function and inside this function we have to define add_theme_page functions with its parameters as specified below;
function add_test_theme_page() {
add_theme_page( 'My Theme Name in Title Tag', 'Theme Name Here', 'edit_theme_options', 'my-theme-page-name-in-browser-url', 'show_my_theme_page' , 5);
}
add_action( 'admin_menu', 'add_test_theme_page' );
show_my_theme_page() function will output the HTML content that will be displayed on the page.
function show_my_theme_page() {
echo "<br>";
echo '<h1>My Theme Name Here</h1>';
echo "<br>";
echo '<p>This is my Theme page on WordPress Dashboard.</p>';
}