Adding the custom page with add_menu_page function on Wordpress - php

I am using the add_menu_page function on WordPress; this is the code;
function my_admin_menu() {
add_menu_page( 'My Top Level Menu Example', 'Top Level Menu',
'manage_options', 'example.php', 'myplguin_admin_page', 'dashicons-tickets', 6 );
}
example.php
function display_text() {
echo 'Welcome to my page';
}
I get the menu in the dashboard, but the issue has content on the page. I can click the top-level option page from the dashboard, but once I do that, I get an empty page where it should say 'Welcome To My page'. Any ideas on how to get my content to show?

You were using a wrong function name that is why it was showing a blank page.
function my_admin_menu() {
add_menu_page( 'My Top Level Menu Example', 'Top Level Menu', 'manage_options', 'example.php', 'myplguin_admin_page', 'dashicons-tickets', 6 );
}
function myplguin_admin_page(){
echo 'Welcome to admin page';
}

You can try this code, because i use this and work
add_menu_page(
'Import Resi', // Page Title
'Import Resi', // Menu Title
'manage_options', // Capabiliy
'import_php/index.php', // Menu_slug
'', // function
'', // icon_url
6 // position
);
add_menu_page(
'Admin Cek', // Page Title
'Admin Cek', // Menu Title
'manage_options', // Capabiliy
'admin_cek/index.php', // Menu_slug
'', // function
'', // icon_url
7 // position
);

you need to use the correct function name and a more appropriate identifier than example.php. you can require your file from the called function
function my_admin_menu() {
add_menu_page( 'My Top Level Menu Example', 'Top Level Menu', 'manage_options', 'example', 'display_text', 'need a uri to your image here!!', 6 );
}
function display_text(){
require_once 'pathtofile.php'; //--> make sure you read up on paths and require to find your file.
}

Related

Wordpress submenu is blank

I've been willing to add a submenu to an existing plugin (school project), except that i can see it in the menu, but anytime i open it the page is empty.
Anyone know why it might does this?
static function admin_menu() {
add_menu_page(
_x( 'Foyer', 'plugin name in admin menu', 'foyer' ),
_x( 'Foyer', 'plugin name in admin menu', 'foyer' ),
'edit_posts',
'foyer',
array(),
'dashicons-welcome-view-site',
31
);
add_submenu_page(
'tools.php',
'Foyer Settings', //page title
'Settings', //menu title
'edit_plugins', //capability,
'foyer-settings',//menu slug
'my_custom_submenu_page_content'//callback function
);
}
function my_custom_submenu_page_content() {
echo '<div class="wrap">';
echo '<h2>Page Title</h2>';
echo '</div>';
}

Directing to filtered pages from Wordpress Admin Sub Menus

I want to add two additional sub-menu items to my Wordpress Admin menu. The top-level menu I want to hook into is the 'Products' menu created by WooCommerce.
edit.php?post_type=product
The content I want the menu items to show can be accessed by filtering the products by Product Category. E.g.
http://dev3.benefacto.org/wp-admin/edit.php?s&post_type=product&product_cat=manchester
I've come up with a working solution (below) to do this - but it is inelegant because it requires calling a function when I feel like I should be able to simply add something into the 'menu slug' variable perhaps.
Any thoughts much appreciated.
// Hook into the Admin Menu
add_action( 'admin_menu', 'lnz_wp_adminmenu_addproductpages' );
// Add Product Categories
function lnz_wp_adminmenu_addproductpages() {
add_submenu_page( 'edit.php?post_type=product', 'Manchester Charities - Page', 'Manchester Charities- Menu', 'manage_options', 'product_cat_manchester', 'lnz_wp_adminmenu_redirectmanchester' );
add_submenu_page( 'edit.php?post_type=product', 'London Charities - Page', 'London Charities- Menu', 'manage_options', 'product_cat_london', 'lnz_wp_adminmenu_redirectlondon' );
}
// Create Redirects for relevant links
function lnz_wp_adminmenu_redirectmanchester() {
header('Location: http://dev3.benefacto.org/wp-admin/edit.php?s&post_type=product&product_cat=manchester');
exit();
}
function lnz_wp_adminmenu_redirectlondon() {
header('Location: http://dev3.benefacto.org/wp-admin/edit.php?s&post_type=product&product_cat=london');
exit();
}
As far as I know, there's no direct way to achieve this using WP hooks or modifying something like global $submenu...
It can be done with jQuery modifying the href attribute of the submenu items:
add_action( 'admin_menu', function() {
add_submenu_page( 'edit.php?post_type=product', 'Manchester', 'Manchester', 'manage_options', 'cat_manchester', '__return_null' );
add_submenu_page( 'edit.php?post_type=product', 'London', 'London', 'manage_options', 'cat_london', '__return_null' );
});
add_action('admin_footer', function(){
?>
<script>
jQuery(document).ready( function($) {
var admin_url = 'http://dev3.benefacto.org/wp-admin/edit.php';
$('#menu-posts-product').find('a[href*="cat_manchester"]').attr('href',admin_url+'?s&post_type=product&product_cat=manchester');
$('#menu-posts-product').find('a[href*="cat_london"]').attr('href',admin_url+'?s&post_type=product&product_cat=london');
});
</script>
<?php
});

Wordpress Issue Create Sub Menu

I just making sure that I'm creating the submenu on dashboard correctly.
I'm watching a tutorial on how to create a Setting API on Youtube
The problem is when every I hover on ALLB Option Sitting does pop up with the submenu.
Can anyone help with this quick issue having on WordPress, please explain with detail?
<?php
/*
====================
Admin Page
====================
*/
function allb_admin_page() {
//Generate A Legacy Left Behind Admin Page
add_menu_page( ' ALLB Theme Options', 'ALLB Options', 'manage_options', 'a_legacy_left_behind', 'allb_theme_create_page', 'dashicons-admin-customizer', 110);
//Generate A Legacy Left Behind Admin Sub Page
add_submenu_page('allb_theme', 'ALLB Theme Options', 'Setting', 'manage_options', 'a_legacy_left_behind', 'allb_theme_settings_page');
}
add_action('admin_menu', 'allb_admin_page');
function allb_theme_create_page() {
// genration of our admin page
}
function allb_theme_settings_page() {
// genration of our admin page
}
Here is the answer..
Just copy and past this code in your theme's functions.php file..
Would you please try it once ?
add_action('admin_menu', 'my_menu_pages');
function my_menu_pages(){
add_menu_page('My Page Title', 'My Menu Title', 'manage_options', 'my-menu', 'my_menu_output' );
add_submenu_page('my-menu', 'Submenu Page Title', 'Whatever You Want', 'manage_options', 'my-menu' );
add_submenu_page('my-menu', 'Submenu Page Title2', 'Whatever You Want2', 'manage_options', 'my-menu2' );
}
I hope its helpful for you..
Hope this will solve your issue
function allb_admin_page() {
//Generate A Legacy Left Behind Admin Page
add_menu_page( 'ALLB Theme Options', 'ALLB Options', 'manage_options', 'a_legacy_left_behind', 'allb_theme_create_page');
//Generate A Legacy Left Behind Admin Sub Page
add_submenu_page('a_legacy_left_behind', 'ALLB Theme Options', 'Setting', 'manage_options', 'a_legacy_left_behind_sub', 'allb_theme_settings_page');
}

Add menu page is creating submenu with same title name

I am using this function to create menu option in wp dashboard
add_menu_page(
__('Advertisement Pages'),// the page title
__('Advertisement'),//menu title
'edit_themes',//capability
'a-advertise',//menu slug/handle this is what you need!!!
'display_all_ad',//callback function
'',//icon_url,
'15'//position
);
it is creating menu the name of Advertisement but also creating submenu with the same name.
try this
add_action( 'admin_menu', 'advertisement_menus' );
function advertisement_menus() {
add_menu_page( 'Advertisement Pages', 'Advertisement', 'edit_themes', 'advertisement_slug', 'display_all_ad', '', 15 );
}
//and your call back function
function display_all_ad(){
echo "this is call back";
}

Creating Custom Options page in wordpress and adding sub_menu_page

Below is my code, the code generates a user custom options menu page, after adding the sub menu page, when I click the sub menu option, the page refreshes but the sub menu page content is not visible, only the main page content sticks. Please help me to where I am wrong. Do I have to register_setting or any thing else....
<?php
// create custom plugin settings menu
add_action('admin_menu', 'omr_create_menu');
add_action('admin_menu', 'omr_create_submenu');
function omr_create_menu() {
//create new top-level menu
add_menu_page('My Menu Page', 'Main Menu', 'administrator', __FILE__, 'main_menu_page', 'favicon.ico');
}
?>
<?php
function main_menu_page() {
global $title;
?>
<h2><?php echo $title;?></h2>
My New Menu Page!!
<?php
}
function omr_create_submenu(){
add_submenu_page(__FILE__, 'My SubMenu Page', 'My Submenu', 'administrator', 'my_new_submenu', 'my_submenu_page');
}
function my_submenu_page() {
global $title;
?>
<h2><?php echo $title;?></h2>
My New Submenu Page!!
<?php
}
?>
I think you are using the same content page for both menu and submenu with _file_, so you don't see the sbu muen page.
works both:
add_menu_page('My Menu Page', 'Main Menu', 'administrator', __FILE__,
'main_menu_page', 'favicon.ico');
add_submenu_page(__FILE__, 'My SubMenu Page', 'My Submenu', 'administrator',
'my_new_submenu', 'my_submenu_page');
and
add_menu_page('My Menu Page', 'Main Menu', 'administrator', 8, 'main_menu_page',
'favicon.ico');
add_submenu_page(8, 'My SubMenu Page', 'My Submenu', 'administrator', 1,
'my_submenu_page');

Categories