• Resolved rbroggero

    (@rbroggero)


    Hi Joshua, me again!
    Now i’m wondering how to exclude move jquery to footer only for one page.
    I have unchecked “Keep jQuery in the Header” and I tried to put this function on my
    function.php :

    add_filter( ‘stf_exclude_scripts’, ‘jdn_jquery_in_header’, 10, 1 );
    function jdn_jquery_in_header( $scripts ) {

    if( is_page( 114 ) ) { // Replace this number with the page id or page slug
    $scripts[] = ‘jquery’;
    }

    return $scripts;

    }

    But nothing happened
    Can u help me please?

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

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Joshua David Nelson

    (@joshuadnelson)

    Hey there!

    Happy to help. What you have here seems like it should work, but I’ll test it out and see if I can track down why it’s not working as expected and/or provide another method.

    I’m wrapping up a bunch of work today and heading into a holiday weekend, so bear with me if it takes me a week to turnaround a solution.

    Thanks,
    Joshua

    Plugin Author Joshua David Nelson

    (@joshuadnelson)

    Hello again,

    Apologies on the delayed response. I’m just getting out of the vacation backlog now.

    The issue here turns out to be that conditional tags (like is_page()) don’t work in that filter due to where it occurs within the plugin.

    I’ve issued an update and as of version 0.6.3 of the plugin you can use conditional tags correctly in this filter. Once you update, the following code will work.

    I couldn’t tell from your post if you were trying to “Keep jQuery in the Header” globally and push it into the footer for a specific page, OR if you were keeping it in the footer globally (the setting for “Keep in header” _not_ checked) and trying to place it in the header on a specific page.

    So, here are both options:

    
    /**
     * If the global option 'Keep jQuery in Footer' is NOT checked, this will
     *   keep jquery in the header on the specific page.
     */
    add_filter( 'stf_exclude_scripts', 'jdn_keep_jquery_in_header_scripts', 10, 1 );
    function jdn_keep_jquery_in_header_scripts( $scripts ) {
        
    	if( is_page( 2 ) )
    		$scripts[] = 'jquery'; // The script handle is used here
    
        return $scripts;
    
    }
    
    /**
     * If the global option 'Keep jQuery in Footer' is checked, this will
     *   keep jquery in the footer on the specific page.
     */
    add_filter( "stf_exclude_scripts", "jdn_keep_jquery_in_footer", 10, 1 );
    function jdn_keep_jquery_in_footer( $scripts ) {
    	
    	// Checks the page, confirms we have a valid array, and checks for 'jquery' in the array
    	// Then pulls the 'jquery' value out of the array
    	if( is_page( 2 ) && is_array( $scripts ) && in_array( 'jquery', $scripts ) ) {
    		$scripts = array_diff( $scripts, array( 'jquery' ) );
    	}
    	
    	return $scripts;
    
    }
    
    Plugin Author Joshua David Nelson

    (@joshuadnelson)

    Hey there, I hope I was able to help. Since I haven’t heard back in 2 months, I’m going to close this topic. Please reach out again if there are any further issues!

    Thanks,
    Joshua

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Keep Jquery in the header only for one page doesn’t work’ is closed to new replies.