admin管理员组文章数量:1301534
I am trying to run a function only on one page or other pages in the future if I so desire. But I dont want to run it site wide. I need help on how to put the if is_page or if is_single.
// disable auto-formatting
function my_formatter($content) {
$new_content = '';
$pattern_full = '{(\[raw\].*?\[/raw\])}is';
$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($pieces as $piece) {
if (preg_match($pattern_contents, $piece, $matches)) {
$new_content .= $matches[1];
} else {
$new_content .= wptexturize(wpautop($piece));
}
}
return $new_content;
}
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
add_filter('the_content', 'my_formatter', 99);
Much appreciate the help.
Thanks
I am trying to run a function only on one page or other pages in the future if I so desire. But I dont want to run it site wide. I need help on how to put the if is_page or if is_single.
// disable auto-formatting
function my_formatter($content) {
$new_content = '';
$pattern_full = '{(\[raw\].*?\[/raw\])}is';
$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($pieces as $piece) {
if (preg_match($pattern_contents, $piece, $matches)) {
$new_content .= $matches[1];
} else {
$new_content .= wptexturize(wpautop($piece));
}
}
return $new_content;
}
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
add_filter('the_content', 'my_formatter', 99);
Much appreciate the help.
Thanks
Share Improve this question edited Feb 9, 2021 at 23:05 Tom J Nowell♦ 61k7 gold badges79 silver badges148 bronze badges asked Feb 9, 2021 at 21:08 SamuelMSamuelM 112 bronze badges 02 Answers
Reset to default 1define the function:
// my_formatter - register custom text formatter for specified post or page
function my_formatter($content) {
$new_content = '';
$pattern_full = '{(\[raw\].*?\[/raw\])}is';
$pattern_contents = '{\[raw\](.*?)\[/raw\]}is';
$pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);
foreach ($pieces as $piece) {
if (preg_match($pattern_contents, $piece, $matches)) {
$new_content .= $matches[1];
} else {
$new_content .= wptexturize(wpautop($piece));
}
}
return $new_content;
}
Simply add the following code. you should change 100 with your desired page ID.
if ( is_page( 100 ) ){ // you should change this line
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
add_filter('the_content', 'my_formatter', 99);
}
You may have already solved this, but here's one example how to conditionally add filter to single page or post.
// Check, if custom formatter needs to be added
add_action(
'template_redirect',
function() {
// don't do anything, if not page or post
if ( ! is_singular( array('page', 'post') ) ) {
// some other type
return;
}
// targetted page or post
// add other post or page slugs or ID to array
// to use custom formatter on them
$targets = array(
'some-post-name', // names/slugs
12, // or IDs
);
// check, that current post/page is one of the targets
global $post;
$current = array($post->ID, $post->post_name);
if ( ! array_intersect( $current, $targets ) ) {
// current ID or slug was not in the targets array
return;
}
// We're on a targeted post or page, so let's do stuff!
// remove default formatters
remove_filter('the_content', 'wpautop');
remove_filter('the_content', 'wptexturize');
// add custom formatter
add_filter('the_content', 'my_formatter', 99);
}
);
function my_formatter($content) {
// code...
}
本文标签: Function only on a specific page
版权声明:本文标题:Function only on a specific page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741669616a2391523.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论