0

My PHP code below works great locally but as soon as I put this on the live server it takes the whole site down. After investigation error logs, it has an issue with the [] used in this line of code:

wp_enqueue_script( 'jquery', '//fake-jquery-script.js', [], null );

Here's the php in full, how do I get around this issue?

/**
 * @desc De-register WP jquery
 **/
 add_action( 'wp_print_scripts', 'de_script', 100 );

 function de_script() {
     wp_dequeue_script( 'jquery' );
     wp_deregister_script( 'jquery' );

 }

 /**
  * Inject jQuery early if there's a Gravity Form
  */
 function gc_gform_inject_jquery( $content = '' ) {
     global $gc_jquery_loaded_before_gform;

     if ( !isset( $gc_jquery_loaded_before_gform )) {

         // set global variable so jQuery isn't loaded twice
         $gc_jquery_loaded_before_gform = true;

         // inject jQuery code
         echo '<!-- loading jquery before Gravity Form inline scripts -->';
         gc_load_jquery_cdn_and_fallback();
     }
     return $content;
 }
 add_filter( 'gform_cdata_open', 'gc_gform_inject_jquery' );

 /**
  * Load jQuery in the footer or before the first Gravity Form.
  * Include a local fallback if the Google CDN fails (e.g. User is in China)
  */
     function gc_load_jquery_cdn_and_fallback() {
     // Google CDN
     echo '<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery'. (SCRIPT_DEBUG ? '.js' : '.min.js') .'"></script>';
     // Local fallback
     echo '<script>if (!window.jQuery) { document.write(\'<script src="'. get_stylesheet_directory_uri() .'/js/vendor/jquery-1.11.2'. (SCRIPT_DEBUG ? '.js' : '.min.js') .'"><\/script>\'); }</script>';
     }

 /**
  * Loading jQuery and jQuery-dependent scripts
  * If jQuery was not already loaded before a Gravity Form, load it
  * Also enqueue a fake version of it (for dependencies) and then
  * remove this fake script
  */
 function gc_load_javascript_in_footer() {
     global $gc_jquery_loaded_before_gform;

     // If jQuery has not been loaded already, load it
     if ($gc_jquery_loaded_before_gform !== true) {
         gc_load_jquery_cdn_and_fallback();
     }

     // Enqueue a fake script called "jquery" to for dependent enqueued scripts
     // HERE'S THE PROBLEM
     wp_enqueue_script( 'jquery', '//fake-jquery-script.js', [], null );

     // Remove the fake script
     function gc_remove_fake_jquery_script($tag) {
         $tag = ( strpos($tag, 'fake-jquery-script') !== false ) ? '' : $tag;
         return $tag;
     }
     add_filter( 'script_loader_tag', 'gc_remove_fake_jquery_script' );
 }
 add_action('wp_footer', 'gc_load_javascript_in_footer');
6
  • The old version of PHP that you're running doesn't support short array syntax; use array() instead, or upgrade the version of PHP that you're running Commented Oct 20, 2015 at 21:46
  • [] can on their own never be sent to a function. If it's a string, wrap it in quotes, if it's a array, wrap it in a variable. but as they are they're never going to be parsed by your current PHP. EDIT: maybe PHP 7 can? Commented Oct 20, 2015 at 21:46
  • @Martin - PHP >= 5.4.0, so it's been part of PHP since 1st March 2012 Commented Oct 20, 2015 at 21:48
  • ahhh, I never realised that, but then, what's the point in specifically passing an empty array? Commented Oct 20, 2015 at 21:52
  • 1
    @Martin - if it's a mandatory argument that requires an array, without a default, but you don't want to pass any values.... perfectly legitimate usage of an empty array argument Commented Oct 20, 2015 at 22:14

2 Answers 2

2

[] is PHP short hand for an empty array. http://php.net/manual/en/language.types.array.php

But you require PHP 5.4+ for it to work.

If it works locally but fails remotely, chances are your remote server is running < PHP 5.4

Sign up to request clarification or add additional context in comments.

1 Comment

No. Reference the documentation to see the acceptable arguments. In this case, the default is array(). Null should be fine, as the param is marked optional. But it really depends on how the function handles setting defaults and I'm not entirely sure in this case. Safe money says go with default value when in doubt. @erg103
-1
Please call javascript with thid code
wp_enqueue_script( 'jquery', '//fake-jquery-script.js', array('jquery'), '2015-10-26' );
and Please download fake-jquery-script.js .and put on the projects js template folder and call javascript in 
wp_enqueue_script( 'jquery', '//fake-jquery-script.js', array('jquery'), '2015-10-26' );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.