admin管理员组文章数量:1130349
Let's say I have an function like:
function cc_get_content_preview() {
return apply_filters( 'cc_get_content_preview', get_the_excerpt(), 55 );
}
Here get_the_excerpt() is the $value that I can edit in an add_filter correct?
Question: As you can see the second parameter, in apply_filters (get_the_excerpt()), is not a variable. Is it correct that I can call this parameter through a $variable name to my liking in the add_filter function? Such as below ($cctoreturn):
//Set custom content preview for Aff posts
function cc_custom_content_preview($cctoreturn){
$post_id = get_the_ID();
if (is_sticky($post_id)) :
$cctoreturn = $post_id;
endif;
return $cctoreturn;
}
add_filter( 'cc_get_content_preview', 'cc_custom_content_preview', 20, 1);
Let's say I have an function like:
function cc_get_content_preview() {
return apply_filters( 'cc_get_content_preview', get_the_excerpt(), 55 );
}
Here get_the_excerpt() is the $value that I can edit in an add_filter correct?
Question: As you can see the second parameter, in apply_filters (get_the_excerpt()), is not a variable. Is it correct that I can call this parameter through a $variable name to my liking in the add_filter function? Such as below ($cctoreturn):
//Set custom content preview for Aff posts
function cc_custom_content_preview($cctoreturn){
$post_id = get_the_ID();
if (is_sticky($post_id)) :
$cctoreturn = $post_id;
endif;
return $cctoreturn;
}
add_filter( 'cc_get_content_preview', 'cc_custom_content_preview', 20, 1);
Share
Improve this question
edited Nov 8, 2018 at 11:51
RobbTe
asked Nov 8, 2018 at 11:35
RobbTeRobbTe
2622 silver badges16 bronze badges
1 Answer
Reset to default 2I can't really make sense of what you're trying to do. This code:
apply_filters( 'cc_get_content_preview', get_the_excerpt(), 55 );
Will let developers hook into the cc_get_content_preview hook to change the value that the cc_get_content_preview() function returns. When developers hook into an action or filter, they do so using a callback function. This function will receive any additional arguments passed to apply_filters() as parameters on the callback function.
In you specific function, that would look like this:
function my_callback_function( $a, $b ) {
echo $a; // This will be the value of get_the_excerpt().
echo $b; // This will be the number 55.
}
add_filter( 'cc_get_content_preview', 'my_callback_function', 10, 2 );
But it looks like you're trying to do something like JavaScript and pass the full function through? That's not going to work. You can't put functions into variables and execute them later like you can in JavaScript.
Let's say I have an function like:
function cc_get_content_preview() {
return apply_filters( 'cc_get_content_preview', get_the_excerpt(), 55 );
}
Here get_the_excerpt() is the $value that I can edit in an add_filter correct?
Question: As you can see the second parameter, in apply_filters (get_the_excerpt()), is not a variable. Is it correct that I can call this parameter through a $variable name to my liking in the add_filter function? Such as below ($cctoreturn):
//Set custom content preview for Aff posts
function cc_custom_content_preview($cctoreturn){
$post_id = get_the_ID();
if (is_sticky($post_id)) :
$cctoreturn = $post_id;
endif;
return $cctoreturn;
}
add_filter( 'cc_get_content_preview', 'cc_custom_content_preview', 20, 1);
Let's say I have an function like:
function cc_get_content_preview() {
return apply_filters( 'cc_get_content_preview', get_the_excerpt(), 55 );
}
Here get_the_excerpt() is the $value that I can edit in an add_filter correct?
Question: As you can see the second parameter, in apply_filters (get_the_excerpt()), is not a variable. Is it correct that I can call this parameter through a $variable name to my liking in the add_filter function? Such as below ($cctoreturn):
//Set custom content preview for Aff posts
function cc_custom_content_preview($cctoreturn){
$post_id = get_the_ID();
if (is_sticky($post_id)) :
$cctoreturn = $post_id;
endif;
return $cctoreturn;
}
add_filter( 'cc_get_content_preview', 'cc_custom_content_preview', 20, 1);
Share
Improve this question
edited Nov 8, 2018 at 11:51
RobbTe
asked Nov 8, 2018 at 11:35
RobbTeRobbTe
2622 silver badges16 bronze badges
1 Answer
Reset to default 2I can't really make sense of what you're trying to do. This code:
apply_filters( 'cc_get_content_preview', get_the_excerpt(), 55 );
Will let developers hook into the cc_get_content_preview hook to change the value that the cc_get_content_preview() function returns. When developers hook into an action or filter, they do so using a callback function. This function will receive any additional arguments passed to apply_filters() as parameters on the callback function.
In you specific function, that would look like this:
function my_callback_function( $a, $b ) {
echo $a; // This will be the value of get_the_excerpt().
echo $b; // This will be the number 55.
}
add_filter( 'cc_get_content_preview', 'my_callback_function', 10, 2 );
But it looks like you're trying to do something like JavaScript and pass the full function through? That's not going to work. You can't put functions into variables and execute them later like you can in JavaScript.
本文标签: functionsAddfilter when value is no variable
版权声明:本文标题:functions - Add_filter when value is no variable? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749197569a2331243.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论