1

I developed a few nodes under my custom parent node on admin bar. While navigating on frontend and clicking on one node I'd like to enqueue some css file. The url should remain. While navigating on frontend and clicking on a different node I'd like to dequeue this css file. The url should also remain. Is that possible and how? Regards I. Mani

1 Answer 1

0

From what I understand from your question, your best course of action is to assign the node to a Wordpress callback function using built-in AJAX. If you don't know what I'm talking about, you should familiarize yourself with using AJAX in Wordpress in the Codex. However, here is a basic overview of steps:

  1. Add a function in a JS file that is attaches a click handler to the admin node element (should be #wp-admin-bar-node-name, check dev tools). Code not tested but for sample purposes.

    jQuery(document).ready(function($){
        $('#wp-admin-bar-node-name').on('click', 'a', function(e){
            var data = {
                'action': 'enqueue_my_style',
             };
            $.post(ajax_url, data, function(response) {
                console.log('Got this from the server: ' + response);
            });
            e.preventDefault();
        });
    });
    
  2. Create a PHP function (in functions.php, your plugin file or wherever you choose to keep and organize PHP functions) with a callback function (again, not tested)

    add_action('wp_ajax_enqueue_my_style', 'enqueue_my_style_callback');
    function enqueue_my_style_callback() {
        wp_enqueue_style('my_style'); //Assumes you have already registered the style
        wp_die();
    }
    

This should get you pointed in the right direction. If you're looking to dequeue on another node, just write another similar set of functions or write a more complex PHP callback and pass data from the click to the function using the data object. There are a lot of resources on how to use AJAX in Wordpress on the web and this example isnt's altogether that complicated.

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.