• Resolved ImageAndrzej

    (@ukandrzej)


    Is there any code I could add to a php snippet to stop it running on specific pages or posts?

Viewing 7 replies - 1 through 7 (of 7 total)
  • concerning a page you can use

    $exclude=stristr(‘https://’ . $_SERVER[‘HTTP_HOST’]. $_SERVER[‘REQUEST_URI’],’/some-page-name/’);
    if(!$exclude {…}

    The advance of this solution is that it concerns also sub-pages like /some-page-name/sub-page-name/

    apparenty WordPress has also some functions for getting the page id or post id. Look at function reference

    https://codex.wordpress.org/Function_Reference

    $exclude1 = some_function_name(‘here page/post id’);
    $exclude2 = some_function_name(‘here page/post id’);
    if (!($exclude1 || $exclude2)) {…}

    Thread Starter ImageAndrzej

    (@ukandrzej)

    Thank you very much.

    I understand where I need to substitute my page URL but don’t understand what I need to put in the curly brackets: {…}.

    Am I able to add this code above or below into the same code snippet that I want to work on the rest of the site?

    Inside curly brackets just the code, which you want to be executed with other pages.

    Plugin Author ImageShea Bunge

    (@bungeshea)

    Hello @ukandrzej,

    I would recommend adding this to the top of snippets that you want to exclude from a specific page:

    if ( is_page( 'page-slug' ) ) return;

    You can also pass in page IDs or titles, or multiple pages in an array. See more here: https://developer.wordpress.org/reference/functions/is_page/

    Thread Starter ImageAndrzej

    (@ukandrzej)

    Hi Shea,

    I’m adding your code like this:

    if ( is_page( '/categories/hotels/' ) ) return;
    function c3_display_dbl_spaces($content){
    	return preg_replace("/(.|\?|!)( {2})/", "\\1  ", $content);
    }
    add_filter( 'the_content', 'c3_display_dbl_spaces', 20 );

    The php snippet is to represent a non breaking space which I add with the space bar after a sentence full stop. It works fine but not on the page path mydomain.com/categories/hotels/ (page ID is 17493), where it breaks my external affiliate JavaScript code which is supposed to pull in up to date information from the business.

    Plugin Author ImageShea Bunge

    (@bungeshea)

    Hi Andrej,

    You need to pass just the slug to is_page() – not the full path.

    It might be easier to use the ID in this situation, like this:

    add_filter( 'the_content', function ( $content ) {
    	if ( is_page( 17493 ) ) {
    		return $content;
    	}
    
    	return preg_replace( '/(.|\?|!)( {2})/', '\\1  ', $content );
    }, 20 );
    Thread Starter ImageAndrzej

    (@ukandrzej)

    Hi Shea,

    I very much appreciate the help from you both. I used your code above and it works perfectly.

    Best regards,
    Andrzej

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Exclude php snippet on specific page’ is closed to new replies.