php – Custom plugin settings link doesn’t append current class to menu item when visited? Why?

Question

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="https://wordpress.stackexchange.com/questions/406486/$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.)

0
Beaniie 2 months 2022-06-06T17:09:39-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse