1

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

0

2 Answers 2

1

define 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);
}
4
  • 2
    Please explain why your code should work. What actual question lacks. Don't just throw some random code. Commented Feb 10, 2021 at 9:31
  • What did you change in the code to solve the problem?
    – Tom J Nowell
    Commented Feb 10, 2021 at 12:00
  • I am still confuse. it didnt work
    – SamuelM
    Commented Feb 10, 2021 at 13:56
  • @TomJNowell Sorry. I added a little explanation Commented Feb 10, 2021 at 21:14
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.