• Resolved Josue Ardon

    (@josueardon)


    Hi guys,

    I need to add a PHP Snippet, and a CSS Snippet in two pages only. How do I do that?

    This is the PHP Snippet:

    add_filter('perfmatters_lazyload_youtube_thumbnail_resolution', function($resolution) {
    if(!wp_is_mobile()) {
    $resolution = 'maxresdefault';
    }
    return $resolution;
    });

    I found in your documentation that I could add this line of code at the beginning:
    if ( ! is_page( array( 'Homepage', 'Guatemala Tours' ) ) ) return;

    Is that correct?

    This is the code for the CSS Snippet:

    .hc-back {
    	overflow: hidden;
    }
    
    .hc-front {
    	transform: translatey(100%);
    	transition: 200ms ease-in-out;
    }
    
    .hc-back:hover .hc-front{
    	transform: translatey(0%);
    }

    and, what would I use for the CSS Snippet?

    Thank you in advance for your help!

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    You’re correct, you just need to add that line in each snippet just after the add_filter … function line.

    Here’s how that would look in the first snippet:

    add_filter( 'perfmatters_lazyload_youtube_thumbnail_resolution', function ( $resolution ) {
    	if ( ! is_page( array( 'Homepage', 'Guatemala Tours' ) ) ) {
    		return $resolution;
    	}
    
    	if ( ! wp_is_mobile() ) {
    		$resolution = 'maxresdefault';
    	}
    	return $resolution;
    } );

    Alternatively, because you’re already doing an if check in that snippet, you can combine them like so:

    add_filter( 'perfmatters_lazyload_youtube_thumbnail_resolution', function ( $resolution ) {
    	if ( ! wp_is_mobile() && is_page( array( 'Homepage', 'Guatemala Tours' ) ) ) {
    		$resolution = 'maxresdefault';
    	}
    	return $resolution;
    } );

    For the second snippet, you just need to wrap your CSS code in an add_action hook:

    add_action( 'wp_head', function () {
    	if ( ! is_page( array( 'Homepage', 'Guatemala Tours' ) ) ) {
    		return;
    	}
    
    	?>
    	<style>
            .hc-back {
                overflow: hidden;
            }
    
            .hc-front {
                transform: translatey(100%);
                transition: 200ms ease-in-out;
            }
    
            .hc-back:hover .hc-front {
                transform: translatey(0%);
            }
    	</style>
    	<?php
    } );
    Thread Starter Josue Ardon

    (@josueardon)

    Hi Shea,
    Thank you for your speedy relpy. I’ve implemented the codes, and they seem to work very well. This is really good for performance, because it’s possible to run the code only in certain pages. I appreciate your help.

    Plugin Author Shea Bunge

    (@bungeshea)

    No problem!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to add a code snippet only in two pages?’ is closed to new replies.