note: updated the code with suggested code
I am creating a menu and submenu in the wordpress admin for a plugin. The menu item of the main menu works fine and goes to the right php file(page). but the submenu shows the text of the menu but when clicked on it it redirects to the 404 page and in my case it will go to the homepage. I have spend some hours over the code and it looks like I am overseeing something. Here is the code:
class BbtbPlugin{
public function __construct(){
add_action( 'init', array( $this,'bbtb_admin_menu' ));
}
public function bbtb_admin_menu() {
add_menu_page(
'Bricks by the Bay conventions',
'BBTB conventions',
'manage_options',
'bbtb_conventions',
'all_bbtb_conventions',
'dashicons-media-spreadsheet',
11
);
add_submenu_page(
'bbtb_conventions',
'Settings',
'Settings',
'manage_options',
'bbtb_settings',
function () { // anonymous callback function
include "includes/setting_page.php";
}
);
function all_bbtb_conventions() {
include "includes/add_convention.php";
}
}
}
So I checked if the function of the submenu works by replacing in the add_menu_page the 'all_bbtb_convention' with 'bbtb_settings'. Then the function works.
So there must be something in the add_submenu_page settings I am missing
It may be because you are using the same name of the slug in the name of the callback function, try changing it.
You can also do the following:
add_submenu_page(
'bbtb_conventions',
'Settings',
'Settings',
'manage_options',
'bbtb_settings',
'bbtb_settings_callback'
);
function bbtb_settings_callback() {
include "includes/setting_page.php";
}
You can even omit the callback function with an anonymous one.
add_submenu_page(
'bbtb_conventions',
'Settings',
'Settings',
'manage_options',
'bbtb_settings',
function () { // anonymous callback function
include "includes/setting_page.php";
}
);
There is also no need to add a conditional to verify the capacity of manage_options, because for something the capacity is set in parameter 3 inadd_menu_page()
and in parameter 4 inadd_submenu_page()
I hope I have helped you, greetings!
Figured out I need to change theadd_action( 'init', array( $this,'bbtb_admin_menu' ));
replace theinit
withadmin_menu
.
Now it works good
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.