admin管理员组文章数量:1391964
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=":$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 ?
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/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
Reset to default 1I'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.
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
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";}
本文标签: Passing variable from child theme function to parent theme filter
版权声明:本文标题:Passing variable from child theme function to parent theme filter 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744673516a2618971.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论