最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

functions - Add_filter when value is no variable?

matteradmin8PV0评论

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
Add a comment  | 

1 Answer 1

Reset to default 2

I 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.

Post a comment

comment list (0)

  1. No comments so far