bsod99
bsod99

Reputation: 1297

Wordpress apply filter / add filter from child theme

I've looked over the docs and examples of how this should be done, but can't see what the issue is with the code below. The function in my child theme just isn't being called. It's probably glaringly obvious but I just can't see it, see any pointers most welcome...

parent theme's functions.php

add_action('init', 'st_header_scripts');
    function st_header_scripts() {
        $javascripts  = wp_enqueue_script('jquery');
        $javascripts .= wp_enqueue_script('custom',get_bloginfo('template_url') ."/javascripts/app.js",array('jquery'),'1.2.3',true);
        $javascripts .= wp_enqueue_script('superfish',get_bloginfo('template_url') ."/javascripts/superfish.js",array('jquery'),'1.2.3',true);
        $javascripts .= wp_enqueue_script('formalize',get_bloginfo('template_url') ."/javascripts/jquery.formalize.min.js",array('jquery'),'1.2.3',true);

        echo apply_filters ('child_add_javascripts',$javascripts);
    }

in child theme...

function child_add_javascripts($javascripts) {
        $javascripts  = "test";
        echo "test"; die;
        return $javascripts;
    }

    add_filter('st_header_scripts','child_add_javascripts');

Upvotes: 1

Views: 1448

Answers (2)

Ivan Hanák
Ivan Hanák

Reputation: 2306

So, you want in parent theme have a function, that hooks your JS files and in a child theme only add JS files?

Your code is sort of messy. I explain

  1. scripts should be hooked into wp_enqueue_scripts, not init
  2. wp_enqueue_script does not return any value (not even NULL :-D), so assigning it to a variable is useless
  3. Pay attention to your naming of filter, functions add_filter and apply_filters "work" together, so their first parameter should be same

And here is code, that I assume, you wanted, in the parent theme, there is created a function, that do the enqueueing, and through child theme, you only set an array of javascript files to be hooked and enqueued.

function st_header_scripts() {

    /**
     * 'jquery' has FALSE value, since it is registered in wordpress and you do not need an url
     */
    $js_files = array(
        'jquery' => false,
        'custom' => get_bloginfo('template_url') ."/javascripts/app.js",
        'superfish' => get_bloginfo('template_url') ."/javascripts/superfish.js",
        'formalize' =>get_bloginfo('template_url') ."/javascripts/jquery.formalize.min.js"
    );

    /**
     * Here you create a variable with a possibility to be filtered
     * The first argument is your custom name of your filter
     * The second argument is a variable which might be modified with filter and assigned to the $javascripts variable
     */
    $javascripts = apply_filters('header_javascripts', $js_files);

    /**
     * Here you just enqueue your scripts
     */
    if(false != $javascripts)
        foreach($javascripts as $name => $uri)
            if(!$uri)
                wp_enqueue_script($name);
            else
                wp_enqueue_script($name, $uri, array('jquery'), '1.2.3', true );
}
add_action('wp_enqueue_scripts', 'st_header_scripts');

/**
 * A callback for a filter `header_javascripts`
 * @param array $original_js array of JS files from parent theme
 * You can either add JS into array or create and return new array
 */
function children_theme_js($original_js) {

    //adding to original array
    $original_js[] = 'jquery-ui-core';

    //not enqueueing JS from parent theme
    //$original_js = array();
    //$original_js['script'] = 'I-am-the-url';

    //you need to return an array into the filter
    return $original_js;
}
add_filter('header_javascripts','children_theme_js');

Upvotes: 0

maiorano84
maiorano84

Reputation: 11951

There are a few things wrong here. wp_enqueue_script isn't a return function, so there's no reason for you to be setting it to a variable. What it does is generate all script tags necessary once wp_head() is called within header.php

Secondly, the problem stems from your use of add_filter and apply_filter. But I think we should go over what the actual differences are between actions and filters (which you probably might know, but others might not):

Actions do stuff depending on the data it receives
Filters do stuff to the data it receives and returns it

do_action() and apply_filter() are your trigger functions that take in the trigger name as its first parameter, and arguments you would like to pass to the callback as it's 2nd-nth arguments.

add_action() and add_filter() are your listeners that look for the defined name in its first argument, and then executes a callback function defined in its second argument.

Given your case here, you would be better off prioritizing your action hooks using the 3rd parameter of your action hooks.

Parent Theme:

add_action('wp_enqueue_scripts', 'st_header_scripts');
function st_header_scripts() {
    wp_enqueue_script('jquery');
    wp_enqueue_script('custom',get_bloginfo('template_url') ."/javascripts/app.js",array('jquery'),'1.2.3',true);
    wp_enqueue_script('superfish',get_bloginfo('template_url') ."/javascripts/superfish.js",array('jquery'),'1.2.3',true);
    wp_enqueue_script('formalize',get_bloginfo('template_url') ."/javascripts/jquery.formalize.min.js",array('jquery'),'1.2.3',true);
}

Child Theme:

add_action('wp_enqueue_scripts','child_add_javascripts',20); //This will execute the child_add_javascripts callback after the st_header_scripts callback
function child_add_javascripts(){
    wp_enqueue_script('child_javascript',get_bloginfo('stylesheet_directory') ."/javascripts/child_js.js",array('jquery'),'1.2.3',true); //This looks in the CHLID theme's directory while template_url looks in the parent theme's directory
}

It took me a bit to get a solid grasp on all the different core actions and filters, but once you get used to it and leverage it to all of your theme's needs, they become a very powerful tool.

Let me know if this helps

Upvotes: 2

Related Questions