Support » Plugin: WP Logo Showcase Responsive Slider » PHP Notice: wp_enqueue_script was called incorrectly

  • Resolved stevemullen

    (@stevemullen)


    This plugin appears to call wp_enqueue_script directly. This is not supported and generates PHP warnings. This code from wpls_logo_slider( $atts, $content ):

    `wp_enqueue_script( ‘wpos-slick-jquery’ );
    wp_enqueue_script( ‘wpls-public-js’ );`

    The correct way to load js is via an action:
    add_action( 'admin_enqueue_scripts', 'wpls_load_scripts' );

    function wpls_load_scripts() {

    `wp_enqueue_script( ‘wpos-slick-jquery’ );
    wp_enqueue_script( ‘wpls-public-js’ );`

    }

Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author Anoop Ranawat

    (@anoopranawat)

    Hello,

    Thanks for reaching here.
    We have checked our end its working fine it’s look like your side permission related issue or any other issue please share worning message, page url or screenshot

    We have aleady registered these js in Wpls_Script class and then used(enqueue) in shortcode where its need this is proper way.

    And your code snippet admin_enqueue_scripts hook is only use for admin side enqueue script not front side it should be wp_enqueue_scripts

    Thanks

    Yes, it’s working. But no, it’s not working correctly.

    Enable WordPress DEBUG, and you will get “PHP Notice: wp_enqueue_script was called incorrectly”.

    You are not loading wp_enqueue_script via an action in the function wpls_logo_slider(). You must do this to avoid this php warning.

    Please refer to https://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts

    Thank you

    Plugin Author Anoop Ranawat

    (@anoopranawat)

    Hi,

    It looks like you have some misunderstanding. I have double check and everything is OK. Let me clear,

    In wpls_logo_slider() function we have just enqueue the script. We are not registering over here.

    The script is registered via proper action hook wp_enqueue_scripts in this file
    /includes/class-wpls-script.php

    So the script is registered via proper action hook and in function we just enqueue it. It means what the script loads only on that page in which the shortcode is placed. It will not load all over regarding speed concern.

    Can you please confirm which version of plugin you are using and can you please provide us the full screenshot of that error? so I can take a look into it.

    Please read the linked page I’ve already provided:

    https://codex.wordpress.org/Plugin_API/Action_Reference/wp_enqueue_scripts

    I am not mistaken.

    Plugin Author Anoop Ranawat

    (@anoopranawat)

    Hi,

    As we are plugin developer and we are aware about the action and hooks. We have double check everything and all looks good.

    We have also WP_DEBUG on and we are not getting any error like this or before this none of our client has reported this type of error.

    I have also checked server error log file and there is no error like this.

    Just for your reference, Let me explain with other popular plugin – WooCommerce
    1) woocommerce/includes/gateways/class-wc-payment-gateway-cc.php – Line no 56 – Function form()

    In that you can see wp_enqueue_script( 'wc-credit-card-form' ) is called.

    2) woocommerce/includes/class-wc-shortcodes.php – Line no 555 – Function product_page()

    In that you can see wp_enqueue_script( 'wc-single-product' ) is called. It means it is not called incorrectly. They have registered script in a proper hook and then they can use where they want.

    Can you please provide us the details which I have asked in my previous response?

    I’m trying to help you – but you are ignoring what I’m saying.

    At one time, you used to be allowed to load a script directly via wp_enqueue_script(). And while that still works:
    – it is no longer the proper way. You must load the script (or style) via the action hook wp_enqueue_scripts or admin_enqueue_scripts or login_enqueue_scripts. Please read this important link.

    Your code is obviously not doing that. It’s directly calling wp_enqueue_script() within a shortcode.

    I cannot explain why are aren’t seeing the php warnings on your server. Any number of settings (or php versions) might get in the way of that.

    The php warning:
    [12-Apr-2019 12:57:45 UTC] PHP Notice: wp_enqueue_script was called incorrectly. Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. Please see Debugging in WordPress for more information. (This message was added in version 3.3.0.) in /home/redacted/public_html/wp-includes/functions.php on line 4667

    Plugin Author Anoop Ranawat

    (@anoopranawat)

    Hi,

    I am not ignoring you. I have gone through your each and every response, I have checked in code, read codex as well. Everything looks OK.

    As I have told you, there are two functions for script and style,
    1) wp_register_style() and wp_register_script()
    2) wp_enqueue_style() and wp_enqueue_script()

    Now when we use wp_register_style() OR wp_register_script() then it is highly recommended to use within proper hook wp_enqueue_scripts() OR admin_enqueue_scripts() based on your requirement.

    Without this hook you can not register your script or style. If you do then it will be a bad practice.

    Now in our code we have registered our script with wp_enqueue_scripts() hook now we can use that script handle where we want so that’s why we just enqueue our script with handler in shortcode. It will only be used when page has a shortcode. On other pages it will not come due to speed concern, code optimization and to avoid conflict.

    As per HTML standard style should be load in HEAD and script should be in footer. Our plugin also follows this.

    Now let me show you some WordPress core example,
    1) File Location : /wordpress/wp-admin/about.php – Line no 12. They have enqueued wp_enqueue_script( ‘underscore’ );

    2) File Location : /wordpress/wp-admin/comment.php – Line no 60. They have enqueued wp_enqueue_script( ‘comment’ );

    My environment is as below,
    1) WordPress – 5.1.1
    2) WP_DEBUG set to true in config.php file
    3) PHP – 7.2.4

    I have checked my PHP error log also but not found this type of error.

    Can you please tell me what is your environment and please provide me the full screenshot of that error.

    Please check by disabling the other plugins also.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘PHP Notice: wp_enqueue_script was called incorrectly’ is closed to new replies.