• Resolved caraya

    (@caraya)


    I’m trying to use the scripts_to_footer plugin to move scripts to the bottom of pages. This breaks Cloudinary since it expects the script to be in the head.

    Normally I would run something like this to exclude the script from head

    add_filter( 'stf_exclude_scripts', 'stf_custom_header_scripts', 10, 1 );
    function stf_custom_header_scripts( $scripts ) {
    	$scripts[] = 'backbone'; // replace 'backbone' with the script slug
    	return $scripts;
    }

    What slug would I use to keep the script in the head? Is there an alternative to use scripts _to_footer?

    • This topic was modified 1 year, 5 months ago by caraya.
Viewing 9 replies - 1 through 9 (of 9 total)
  • Hey there,

    Looks like there is a good stack overflow thread on this issue:
    https://wordpress.stackexchange.com/questions/205938/move-scripts-to-footer-but-exclude-one-script

    Thread Starter caraya

    (@caraya)

    The SO thread is not applicable. wp_dequeue_script only works with script files and this is not the case here.

    The Cloudinary script is inlined on the page’s head and there is no ID that I can use with the script to footer plugin to keep it in the head.

    Plugin Support loic @ cloudinary

    (@loiccloudinary)

    Hi @caraya,

    We are looking into this plugin as it does some drastic things like removing WP Core actions instead of filtering out the scripts.

    I’ll get back to you asap.

    Loic

    Thread Starter caraya

    (@caraya)

    Interesting observation. The plugin doesn’t remove anything you don’t tell it to, but I would love to hear more about it.

    I’ll wait for your feedback.

    Plugin Support Wissam @ Cloudinary

    (@wissamcloudinary)

    Hi there,

    This 3rd party plugin does remove all the actions from the?wp_head.
    You could check their class-stf-public.php file.

    Regards,

    Wissam

    Thread Starter caraya

    (@caraya)

    For most scripts that are files, either part of WP core or part of a plugin, STF provides a way to keep it in the head.

    Since Cloudinary writes the script on the head and doesn’t provide an ID or any other way to identify the script for STF to keep in the head, I thought I would ask here. This is why I asked if you provided a way to keep the generated script on the head.

    This is a performance optimization and not being able to keep Cloudinary in the head of the page and Cloudinary breaking if you move it to the footer prevents me from using it

    Plugin Support loic @ cloudinary

    (@loiccloudinary)

    Hi @caraya,

    You should be able to use this code either in your functions.php or as part of a mu-plugin

    <?php
    
    /**
     * Ensure that the lazy load assets are loaded in the head.
     */
    add_action(
    	'cloudinary_ready',
    	static function() {
    
    		// Is Cloudinary lazy load bypassed.
    		$bypass = apply_filters(
    			'cloudinary_bypass_lazy_load',
    			false,
    			array(
    				'atts' => array(
    					'class' => '',
    				),
    			)
    		);
    
    		// Check if the default core actions were removed, and Cloudinary lazy load is not bypassed.
    		if ( ! $bypass && ! has_action( 'wp_head', 'wp_print_scripts' ) ) {
    			$plugin    = Cloudinary\get_plugin_instance();
    			$lazy_load = $plugin->get_component( 'lazy_load' );
    
    			// Add back Cloudinary lazy load assets to the head.
    			add_action( 'wp_head', array( $lazy_load, 'enqueue_assets' ) );
    		}
    	}
    );

    Please let me know how it goes.

    Loic

    Thread Starter caraya

    (@caraya)

    Loic,

    It fixed the problem. The scripts have been moved to the footer and the images loaded from Cloudinary still work.

    Thanks!

    Plugin Support loic @ cloudinary

    (@loiccloudinary)

    Hi @caraya,

    Glad it works!

    Feel free to reach out here or via [email protected] if you need anything else.

    Best,

    Loic

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Keeping cloudinary script on the head’ is closed to new replies.