0

So I've created a custom setting link on my plugin listing, that takes the user to the plugin settings. However, it doesn't seem to be working as I would expect it to and I'm not sure why. 🤨

To clarify, the link is added, and it does redirect. So the basic functionality is as I would expect. It's a visual error, I.e indication to the user which page they are on in the admin sidebar menu.


Expected Behaviour:

  • User clicks settings link.
  • User is taken to 'wp-admin/admin.php?page=apwb_settings' page.
  • Side bar menu <li> and <a> has class="current" appended.

Actual Behavior:

  • User clicks settings link.
  • User is taken to 'wp-admin/admin.php?page=apwb_settings' page.
  • Side bar menu <li> and <a> remains unchanged.

My Registered Setting...

/** 
 * Register the settings page.
 * 
 * @package     apwb/postal-pricing
 * @since       1.0.0
*/
function apwb_settings_page() {
    add_options_page(
        __( 'Postal Pricing Settings', 'postal-pricing' ),
        __( 'Postal Pricing Settings', 'postal-pricing' ),
        'manage_options',
        'apwb_settings',
        function() {
            ?>
            <div id="apwb-settings"></div>
            <?php
        },
    );
}
add_action( 'admin_menu', __NAMESPACE__ . '\apwb_settings_page' );

My plugin links amendment...

/** 
 * Add settings link to plugin listing.
 * 
 * @package     apwb/postal-pricing
 * @since       1.0.0
*/
function apwb_settings_link( $links ) : array {
    $label = esc_html__( 'Settings', 'postal-pricing' );
    $slug  = 'apwb_settings';
    $admin_url = admin_url('admin.php?page=' . $slug);

    array_unshift( 
        $links, 
        "<a href='$admin_url'>$label</a>" 
    );

    return $links;
}
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), __NAMESPACE__ . '\apwb_settings_link' );

My Question... How does WordPress typically handle this behavior? Is it something I can hook into? Or will I have to write a custom solution? (I really don't want to add more js if I can help it.)

1 Answer 1

4
+50

I noticed your apwb_settings_page() function is adding the "Postal Pricing Settings" submenu using add_options_page(), and the documentation stated that: (bold formatting added by me)

  • This function is a simple wrapper for a call to add_submenu_page(), passing the received arguments and specifying ‘options-general.php‘ as the $parent_slug argument. This means the new options page will be added as a sub menu to the Settings menu.

And that also means, in your apwb_settings_link() function, $admin_url (or your settings link) should actually link to or use options-general.php and not admin.php:

  • Incorrect: $admin_url = admin_url('admin.php?page=' . $slug);

  • Correct: $admin_url = admin_url('options-general.php?page=' . $slug);

  • Even better: Use menu_page_url(), e.g. $admin_url = menu_page_url( $slug, false );

The "Incorrect" above will actually load the correct "Postal Pricing Settings" admin page, but the "Postal Pricing Settings" submenu (under the "Settings" menu) will not be highlighted because the parent file is not options-general.php. So actually, you just needed to ensure your setting link uses the correct admin page URL. :)

1
  • 1
    Beautifully answered Sally, thanks for clearing that up. 👌👍
    – Lewis
    Commented Jun 15, 2022 at 20:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.