admin管理员组文章数量:1130349
I was wondering if it is possible to filter out a part of a variable, if it is no array. Or can I only replace the whole code, or add code to it?
Let's say I have this code:
$output = '<select>';
$output .= '<option value="option 1">Option 1</option>';
$output .= '<option value="Filter out">Don't show this option</option>';
$output .= '<option value="option 2">Option 2</option>';
$output .= '</select>';
$output = apply_filters( 'my_filter', $output );
return $output;
Is there any way to filter out the second option, without rewriting the whole code in the filter?
Thanks
SOLUTION
My solution was to use str_replace.
I was wondering if it is possible to filter out a part of a variable, if it is no array. Or can I only replace the whole code, or add code to it?
Let's say I have this code:
$output = '<select>';
$output .= '<option value="option 1">Option 1</option>';
$output .= '<option value="Filter out">Don't show this option</option>';
$output .= '<option value="option 2">Option 2</option>';
$output .= '</select>';
$output = apply_filters( 'my_filter', $output );
return $output;
Is there any way to filter out the second option, without rewriting the whole code in the filter?
Thanks
SOLUTION
My solution was to use str_replace.
1 Answer
Reset to default -1The documentation is well explicable of the topic, let's have a look here https://developer.wordpress/reference/functions/apply_filters/.
In your specific case my_filter is applied no matter what, even if there is no value it. To apply the filter only if is_array() you could insert a specific instruction directly into your filter like:
function example_callback( $output ) {
if(is_array($output)){
//do something and replace part of the string with str_replace()
}
return $output;
}
add_filter( 'my_filter', 'example_callback', 10, 1 );
About the str_replace() php function read more here http://php/manual/en/function.str-replace.php.
I was wondering if it is possible to filter out a part of a variable, if it is no array. Or can I only replace the whole code, or add code to it?
Let's say I have this code:
$output = '<select>';
$output .= '<option value="option 1">Option 1</option>';
$output .= '<option value="Filter out">Don't show this option</option>';
$output .= '<option value="option 2">Option 2</option>';
$output .= '</select>';
$output = apply_filters( 'my_filter', $output );
return $output;
Is there any way to filter out the second option, without rewriting the whole code in the filter?
Thanks
SOLUTION
My solution was to use str_replace.
I was wondering if it is possible to filter out a part of a variable, if it is no array. Or can I only replace the whole code, or add code to it?
Let's say I have this code:
$output = '<select>';
$output .= '<option value="option 1">Option 1</option>';
$output .= '<option value="Filter out">Don't show this option</option>';
$output .= '<option value="option 2">Option 2</option>';
$output .= '</select>';
$output = apply_filters( 'my_filter', $output );
return $output;
Is there any way to filter out the second option, without rewriting the whole code in the filter?
Thanks
SOLUTION
My solution was to use str_replace.
-
yes you can. you can use
regexto search for a specific match in a string or useDOMdocumentto query HTML code in php – honk31 Commented Nov 22, 2018 at 16:18 - Is that really the best/only way to go? – RobbTe Commented Nov 23, 2018 at 9:27
- 1 Yes, welcome. @RobbTe – middlelady Commented Nov 23, 2018 at 11:37
1 Answer
Reset to default -1The documentation is well explicable of the topic, let's have a look here https://developer.wordpress/reference/functions/apply_filters/.
In your specific case my_filter is applied no matter what, even if there is no value it. To apply the filter only if is_array() you could insert a specific instruction directly into your filter like:
function example_callback( $output ) {
if(is_array($output)){
//do something and replace part of the string with str_replace()
}
return $output;
}
add_filter( 'my_filter', 'example_callback', 10, 1 );
About the str_replace() php function read more here http://php/manual/en/function.str-replace.php.
本文标签: hooksHow to filter part of a variable if it is no array
版权声明:本文标题:hooks - How to filter part of a variable if it is no array? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749157372a2324822.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


regexto search for a specific match in a string or useDOMdocumentto query HTML code in php – honk31 Commented Nov 22, 2018 at 16:18