• Resolved Ibby

    (@ibby)


    Sorry if this has been asked before; I have been looking for a while, but couldn’t find anything directly applicable. I am building my first plugin and I am trying to follow the WP best practices as detailed here.

    My current plugin structure looks like this: – my-plugin — index.php — admin (folder — sub-menu-page.php

    I am trying to keep all of my plugin pages separate as part of my organisation. However, I can’t seem to point my plugin’s submenu link to the sub-menu-page.php file. Here’s my menu code:

    add_action('admin_menu', 'my_menu_pages');
    function my_menu_pages(){
        add_menu_page('WISE Admin', 'WISE Admin', 'manage_options', 'wise-menu', 'my_menu_output', 'dashicons-tickets', 2 );
        add_submenu_page('wise-menu', 'General Settings', 'General', 'manage_options', 'general-settings', 'general_settings' );
        add_submenu_page('wise-menu', 'Submenu Page', 'Submenu Page', 'manage_options', 'admin/sub-menu-page' );
        remove_submenu_page('wise-menu', 'wise-menu');
    }

    The submenu link takes me to “mydomain.com/wp-admin/admin/sub-menu-page” which is obviously not where I want to go.

    Can I please have some advice for what I am doing wrong?

    • This topic was modified 5 years, 1 month ago by Ibby.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Moderator bcworkz

    (@bcworkz)

    The recommendation is to not use the file reference form at all. Use the callback function form instead, like you did for general settings. Your callback function could then simply do an include statement that references your stand alone file.

    BTW, “general_settings” is a terrible name choice. The name could easily collide with other code that uses bad function naming. Prefix all global names with your plugin’s initials. For example, if your plugin name was “Super Awesome Plugin”, prefix function names with “sap_”, as in “sap_general_settings”.

    Thread Starter Ibby

    (@ibby)

    @bcworkz Thank you very much, this has helped immensely. I will follow your advice.

    Moderator bcworkz

    (@bcworkz)

    Happy to help, you’re welcome.

    Thread Starter Ibby

    (@ibby)

    I do have one small question which is sort of related: I want to do some subpage to subpage linking in my plugin and pass some parameters. However, every time I try do this within the plugin itself, I receive the “Sorry, you are not allowed to access this page.” message.

    I have a query in my SQL that adds a row from the plugin. This row includes an ‘ID’ field with a number. I want to pass this number through my subpage link like so:
    <a href=\"admin.php?page=wise-calendar-update?action=edit&item=$rs[0]\">edit</a>

    My ‘edit’ function is defined in wise-calendar-update.php and it works perfectly when I link directly to it. However, if I link directly to it, it doesn’t maintain the WP backend UI…

    Moderator bcworkz

    (@bcworkz)

    Does your user have the capability you’ve specified in the add_submenu_page() call? (“manage_options” in your OP) The lack should be the only reason you would see that message. It’s not related to passing extra query vars. I tried adding a link on my test plugin’s page that passes &item=123 to another plugin page and it came through fine. Same capability specified, I accessed as an admin user. My pages do use the callback method and not the file reference type.

    One explanation for your trouble could be if your theme or another plugin is hooked into the ‘user_has_cap’ filter and is messing with the capabilities check. Another possibility is if there is some sort of custom checking of something that spits out the same message. The capability check is the only use of that message in core, unless you are on multisite.

    Thread Starter Ibby

    (@ibby)

    I’ve raised the priority of the function to 11:
    add_action('admin_menu', 'my_menu_pages', 11);

    And the database user that is passing through the SQL and retrieving information has full privileges. I am of course, logged into wordpress as an Administrator so I am a bit confused. I’ve also tried deactivating all plugins except my custom plugin but I’m still getting that message.

    Hmm, not sure how to even go about debugging this…

    Moderator bcworkz

    (@bcworkz)

    The first step is to be sure you’re seeing the message due to the cause I think is the cause. Hook the ‘admin_page_access_denied’ action and use error_log() to add a distinct message to the log file. Make the error causing request, then verify the distinct message is in the error log.

    The only way that action can fire is if user_can_access_admin_page() returns false. Locate the source code for the function in /wp-admin/includes/plugin.php. Make a backup of the file so you can hack away at the function’s source code and still be able to easily restore the original.

    Add code to the function to send debug data to the error log that would tell you the reason why your menu page permission is failing. Every return false; statement is a different potential reason. Once you identify the right statement, the associated if() conditionals will tell you the reason. Log the related variable values. One (or more) of them is not as expected.

    Thread Starter Ibby

    (@ibby)

    Sorry, as I’ve never done this before, I’m not sure how to use that hook. Would it be in my functions.php? Is it a general function that would log errors, or does it have to go into my link somehow?
    I ‘think’ everything else you advised is straightforward, but I’d have to give it a crack!

    Thread Starter Ibby

    (@ibby)

    After a short time off this to clear my head, I worked out the issues. Firstly, I had two question marks in my URLs which was causing the system to treat the second question markas a character. I changed the second one as per PHP guidelines to and ampersand:
    <a href=\"admin.php?page=wise-calendar-update&action=edit&item=$rs[0]\">edit</a>
    The second issue, which was kindly pointed out by a user in the php-code slack channel, was that all pages had the same prefix to avoid conflicts. This seemed to rectify all problems.

    Thank you very much @bcworkz, you were a great help and I learnt much from you.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Plugin sub-menu pages recommended structure and links’ is closed to new replies.