0

I want to pass a variable from my child theme function to a filter which resides in my parent theme. Please let me know whether below code will work or not ? Is it the right way ?

Code in Parent Theme's functions.php

add_filter( 'post_thumbnail_html', "map_thumbnail" );
function map_thumbnail($html,$color) {
        $my_post = get_post($post->ID);
        $my_color = $color;
        if($my_post->post_name == "contact") {
              $html = '<img src="http://maps.googleapis.com/maps/api/staticmap?color:$my_color">';
        }
        return $html;
}

Code in Child Theme's functions.php

<?php map_thumbnail('#0099dd'); ?>

Please tell me, will above code work? Can I pass the variable $color from my child theme to parent theme like this ?

3 Answers 3

1

I'm not exactly sure what you're trying to accomplish but this won't work because your trying to use or call the function that is declared in the parent theme instead of replacing it with a new function. I'm not sure if replacing a function in the child theme would work either. Maybe someone else can help answer that.

The function map_thumbnail() that you're trying to call in the child theme, is actually being used by the add_filter() function right above it to modify the existing function post_thumbnail_html().

Additionally, even if it would work, you only passed one paramater ('#0099dd') in your attempt to call the function, and it appears to be out of order.

0

apply_filter('post_thumbnail_html', $color). I guess you need this one no?

Call the functions added to a filter hook.

The callback functions attached to filter hook $tag are invoked by calling this function. This function can be used to create a new filter hook by simply calling this function with the name of the new hook specified using the $tag parameter.

The function allows for additional arguments to be added and passed to hooks.

Here is the doc apply_filters

4
  • Q.1) apply_filter('post_thumbnail_html', $color) ...I should write it to my child theme's functions.php right ? Q.2) Can I also do this : apply_filter('post_thumbnail_html', $color1, $color2) ?? Will it work too ?
    – Saif Islam
    Commented Mar 8, 2016 at 20:01
  • somewhere you need it. Check where is parent calling it... maybe it is used in post only (for example single.php), maybe functions.php....
    – Apeiron
    Commented Mar 8, 2016 at 20:03
  • ok, this seems to be the solution for me..I will try it tomorrow and get back to this thread..thanks for the suggestion anyway...will update you tomorrow :) ..thanks a lot
    – Saif Islam
    Commented Mar 8, 2016 at 20:06
  • yes. Additional Filter Arguments.... $myvar = apply_filters( $tag, $value, $param, $otherparam ); $tag should be 'post_thumbnail_html'
    – Apeiron
    Commented Mar 8, 2016 at 20:07
0

Before we get to a solution, let's take a look at how functions work.

Yes, you can call functions in the parent theme from the child theme (though in your example you'll get an error because you pass one variable where two are expected). So, you can add it as a filter in one place and call it independently elsewhere.

When WordPress is loaded it is making an inventory of all available functions. It first loads the child theme's functions.php, then the parent theme's. A function that is redefined in the child, is skipped in the parent. Execution of code only starts after the inventory has been made. Then you can call parent theme functions from the child. You can call plugin functions from your theme, or the other way around. It simply doesn't matter where the function is defined, as long as it is available.

What your function call from the child theme won't do is change the behaviour of the function when it is called as a filter post_thumbnail_html from the get_the_post_thumbnail function. That's two separate calls that are unaware of eachother. So if you want the color from the child function to be applied in the filter, that won't work. To do that, you would have to turn it around. In the parent function call a function from the child (if it exists) and in the child define a function that passes the color.

// parent
if (function_exists (wpse220111_get_child_color))
  $my_color = wpse220111_get_child_color;
else
  $my_color = "#000000";

// child
function wpse220111_get_child_color () {return "#0099dd";}

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.