WordPress add_submenu_page won’t accept parameters

I’ve got this problem while developing a WordPress plugin. Here’s the code snippet of the area I’m working with:

add_submenu_page( 'admin_manager',
                  'Add Event',
                  'Add Event',
                  'manage_options',
                  'add_event&action=modify',
                  'add_event_handler' );

As you can see, on my menu slug I want to add the ‘&action=modify’

Read More
'add_event&action=modify'

But when I do that, WordPress denies me access and says I don’t have the proper privileges. It works fine without the extra parameters, is there something built into wordpress that doesn’t allow for such things?

Any thoughts or workarounds?

Related posts

Leave a Reply

1 comment

  1. The slug argument is not intended to take in query parameters. If you’re intending to set up some kind of default action (which, I would also imagine the “action” query is more than likely a reserved keyword used by WordPress itself), why don’t you set up the page to populate as if the parameter were there by default?

    My guess is that you want other parameters in place as well, which you can then use a Form to send data (via POST or GET) to populate your page using those parameters. For instance:

    <!-- SUBMENU PAGE -->
    <form action="" method="GET">
        <label>Select Action: </label>
        <select name="jKern_action">
            <option value="modify">Modify</option>
            <option value="add">Add Event</option>
            <option value="delete">Delete Event</option>
        </select>
    </form>
    <?php
    $action = isset($_GET['jKern_action']) ? $_GET['jKern_action'] : 'modify';
    switch($action)
    {
        case 'add' :
        ?>
            <!-- YOUR EVENT ADDING HTML HERE -->
        <?php
        break;
        case 'delete' :
        ?>
            <!-- DELETE HTML HERE -->
        <?php
        break;
        default :
        ?>
            <!-- DEFAULT MODIFICATION HTML -->
    <?php
    }
    ?>
    

    This isn’t a perfect example, but I’m sure you can gather what has been done here. As mentioned, the slug argument is NOT intended to populate queries, and you should also be naming your GET and POST keys (as well as option and meta keys) something unique to your code so the likelihood of any naming conflicts is minimal.

    Another example of the proper use of submenu navigation without relying on a form to navigate would be something like this:

    <a href="?page=add_event&jKern_action=add">Add Event</a>
    <a href="?page=add_event&jKern_action=modify">Edit Event</a>
    <a href="?page=add_event&jKern_action=delete">Delete Event</a>
    <?php
    ...
    ?>
    

    Your best bet would be to look up some examples of how plugin authors have effectively used the add_submenu_page() function, but mostly, you should play around with it and get used to how the normal WordPress flow works.

    Good luck, and let me know if this helps.