Support » Plugin: Rotating Tweets (Twitter widget and shortcode) » Javascript js error, script is above mine in footer

  • Resolved Patrick Whitty-Clarke

    (@zerodegreeburn)


    Hi,

    I’ve used grunt to concatenate and minify all of my js files, including jquery into a single file that I enqueue manually in functions.php. I’ve been able to get gravity forms to enqueue files in the footer following advice from this link https://bjornjohansen.no/load-gravity-forms-js-in-footer

    However, rotating tweets seems to insist on placing its scripts above my script file. Is there a hook or something I can do to force it to load itself after my script? I’ve looked around the code but I can’t work it out without modifying the plugin, which I don’t really want to do…

    Thanks

    https://wordpress.org/plugins/rotatingtweets/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Plugin Author Martin Tod

    (@mpntod)

    RT doesn’t do any of the things that Gravity Forms does. It only uses wp_enqueue_script() to put its JavaScript on the page and doesn’t output any JavaScript directly in HTML (except for small bit of code on the admin pages).

    The obvious thing to do is to make RT dependent on your script when it is enqueued using wp_enqueue_script().

    What name do you use to enqueue your script? And what is the name of a unique function or plug-in that only your site uses? Then I can modify the code around line 1846 to be something like:

    $dependence = array('jquery');
    	if (is_plugin_active('wsi/wp-splash-image.php')) {
    		//plugin is activated
    		$dependence[] = 'jquery.tools.front';
    	};
    	if(function_exists('unusual_function_name')||is_plugin_active('unusual_plugin_name')) {
    		$dependence[] = 'unique_name_used_for_your_script_with_wp_enqueue_script';
    	}

    and then it will automatically appear on the page after your script.

    Plugin Author Martin Tod

    (@mpntod)

    PS: On the Rotating Tweets settings page, there’s also a Load in footer option for Where to load Rotating Tweets JavaScript – but I guess you’ve already done that!

    Thread Starter Patrick Whitty-Clarke

    (@zerodegreeburn)

    Hi Martin,

    Yes I’d found the load in footer option! 🙂

    I call my script global-js :

    function pwc_theme_name_scripts() {
    wp_enqueue_script( ‘global-js’, get_template_directory_uri() . ‘/assets/js/build/production.min.js’, array(), ‘1.0.0’, true );
    }
    add_action( ‘wp_enqueue_scripts’, ‘pwc_theme_name_scripts’, 0 );

    When I load a page with rotating tweets on, I get the following error:

    Uncaught TypeError: undefined is not a function rotating_tweet.js:48(anonymous function) rotating_tweet.js:48eb.extend.each production.min.js:1eb.fn.eb.each production.min.js:1(anonymous function) rotating_tweet.js:5j jquery.js:2k.fireWith jquery.js:2m.extend.ready jquery.js:2J

    This must be because of jquery being loaded in the header, along with jquery-migrate.min.js:

    <script type=’text/javascript’ src=’http://dpa.dev/wp-includes/js/jquery/jquery.js’></script&gt;
    <script type=’text/javascript’ src=’http://dpa.dev/wp-includes/js/jquery/jquery-migrate.min.js’></script&gt;

    I can’t quite work out where to apply your code, this is 1839 to 1853 for me:

    foreach($option as $stringname => $contents) {

    $ageindays = (time()-$contents[‘datetime’])/60/60/24;

    if($ageindays > $targetageindays) unset($option[$stringname]);

    };

    $numberidentities = count($option);

    if(WP_DEBUG) echo “<!– There are now “.$numberidentities.” identities cached –>”;

    update_option($optionname,$option);

    }

    Thanks for the help.

    Plugin Author Martin Tod

    (@mpntod)

    What happens if you try version 2 of the JavaScript? Currently you’re using version 1. Sometimes it’s a problem that people have a copy of jQuery.cycle in their JavaScript which can cause problems and changing versions can solve it.

    Does the Rotating Tweets javascript appear before or after your global-js script? If so, otherwise I’ve attempted a fix in the Development Version (lines 1851-1853) which forces it to go after global-js which may, hopefully, also solve the problem.

    Thread Starter Patrick Whitty-Clarke

    (@zerodegreeburn)

    I just tried Version 2, but I still get the js error.

    I get jquery and jquery.migrate loaded into the header, which I’d like to avoid whether its version 1 or 2 and whether load scripts in footer is checked or not. Jquery cycle and rotating tweets appear above my script, I can’t figure out how to get mine above them.

    <script type='text/javascript' src='http://dpa.dev/wp-content/plugins/rotatingtweets/js/jquery.cycle2.renamed.js'></script>
    <script type='text/javascript' src='http://dpa.dev/wp-content/plugins/rotatingtweets/js/jquery.cycle2.scrollVert.renamed.js'></script>
    <script type='text/javascript' src='http://dpa.dev/wp-content/plugins/rotatingtweets/js/jquery.cycle2.carousel.renamed.js'></script>
    <script type='text/javascript' src='http://dpa.dev/wp-content/plugins/rotatingtweets/js/rotatingtweets_v2.js'></script>
    <script type='text/javascript' src='http://dpa.dev/wp-includes/js/admin-bar.min.js'></script>
    <script type='text/javascript' src='http://dpa.dev/wp-content/themes/dpa/assets/js/build/production.min.js'></script>

    However, I just tried your development version and it fixed the issue. Thanks!

    I managed to get jquery to stop loading by using this answer.

    I’d suggest adding in a minified version of your script in your plugin, and maybe trying to get all of the js into one file. At the moment it’s loading 4 js files which is 4 (unminified) HTTP requests:

    <script type='text/javascript' src='http://dpa.dev/wp-content/plugins/rotatingtweets/js/jquery.cycle2.renamed.js'></script>
    <script type='text/javascript' src='http://dpa.dev/wp-content/plugins/rotatingtweets/js/jquery.cycle2.scrollVert.renamed.js'></script>
    <script type='text/javascript' src='http://dpa.dev/wp-content/plugins/rotatingtweets/js/jquery.cycle2.carousel.renamed.js'></script>
    <script type='text/javascript' src='http://dpa.dev/wp-content/plugins/rotatingtweets/js/rotatingtweets_v2.js'></script>

    Thanks for the help!

    Plugin Author Martin Tod

    (@mpntod)

    Glad to hear it.

    Good point re: the JavaScript. I’ll check I can find a solution that doesn’t break other people’s minify plug-ins – and copes with the flexibility of what scripts need to load.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Javascript js error, script is above mine in footer’ is closed to new replies.