Skip to:
Content
Pages
Categories
Search
Top
Bottom

wp_enqueue_script issue on activity page


  • albert101
    Participant

    @albert101

    Hi,
    wondering if someone can help me here.
    After having tried hard not to bother any one I think found the problem( a custom js file) in my theme that is causing issues with my activities page.
    If i take the line off BP works like it is supposed to. but then other functions like sliders etc don’t.
    The following is the line of code causing the problem.
    wp_enqueue_script( ‘xyz-custom-script’, $template_dir . ‘/js/custom.js’, array( ‘jquery’ ), $theme_version, true );

    How should i go about in a correct fashion disable the script from queuing on the activity page please.
    Thanks

Viewing 24 replies - 1 through 24 (of 24 total)

  • Henry Wright
    Moderator

    @henrywright

    Hello @albert101

    You can use wp_dequeue_script() to remove an enqueued script. For example:

    function my_dequeue_script() {
        if ( bp_is_activity() )
            wp_dequeue_script( 'xyz-custom-script' );
    }
    add_action( 'wp_print_scripts', 'my_dequeue_script', 100 );

    Ref: https://codex.wordpress.org/Function_Reference/wp_dequeue_script

    Should be hooked on ‘wp_enqueue_scripts’ though; the print_scripts and related hooks were deprecated back in 3.0 or thereabouts.


    Henry Wright
    Moderator

    @henrywright

    @hnla good spot. I took the example from the WP Codex article I referenced in my post. Do you want to update or should I?

    Thanks but I’ve done my share of updating docs today 🙂


    Henry Wright
    Moderator

    @henrywright

    Done! 🙂


    albert101
    Participant

    @albert101

    Hi Thanks for the help.
    Henry and Hugo.
    I Just read the message.

    If I understand correctly should I change the statement:
    function my_dequeue_script() {
    if ( bp_is_activity() )
    wp_dequeue_script( ‘xyz-custom-script’ );
    }
    add_action( ‘wp_print_scripts’, ‘my_dequeue_script’, 100 );
    to something else and if so What would it be in my case.
    Sorry for my ignorance and thanks for you patience.


    Henry Wright
    Moderator

    @henrywright

    Here is the updated code:

    function my_dequeue_script() {
        if ( bp_is_activity() )
            wp_dequeue_script( 'xyz-custom-script' );
    }
    add_action( 'wp_enqueue_scripts', 'my_dequeue_script', 100 );

    shanebp
    Moderator

    @shanebp

    @albert101

    Did henry’s original function it work for you?
    If so, don’t bother changing it.


    @henrywright

    afaik – The reason the codex page uses the wp_print_scripts hook is to insure that it runs after the wp_enqueue_scripts hook, regardless of priority.

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

    The deprecation Hugo mentions is re not using print_scripts to enqueue a script, which the OP is not trying to do.
    So I’m not convinced that your change to the codex page is appropriate. If you decide to stick with your change, at least update the comments above the example function to reflect that change.


    albert101
    Participant

    @albert101

    Thanks a TON
    Not only have you helped me out with the code.But But You gentlemen, have also helped me grasp the concept and better understand the process.
    Regards,
    Albert


    albert101
    Participant

    @albert101

    I barely checked my mail and got the first Mess that Henry posted using the
    ‘wp_print_scripts’ method.
    I will apply one after the other and will post my results here.
    That is is the least I can do.
    Thanks

    @@shanebp @henrywright ah ok if that is the case then it perhaps ought to be mentioned, did wonder about the hook priority but seen other examples of ? de-registering in that manner but perhaps that would run on that hook – Sorry Henry might have got that wrong then.

    @albert101 try henrywrights code but you may want to change:

    add_action( 'wp_enqueue_scripts', 'my_dequeue_script', 100 );

    to:

    add_action( 'wp_print_scripts', 'my_dequeue_script', 100 );

    The latter should work.


    Henry Wright
    Moderator

    @henrywright

    Probably best if I revert my change! I’ll see to that now…

    @henrywright yep just blame me if asked 🙂


    Henry Wright
    Moderator

    @henrywright

    @hnla we’ll blame the Red Arrows for interrupting our concentration 🙂

    Thanks @shanebp for pointing out.


    albert101
    Participant

    @albert101

    Hi again.
    sorry guys you are dealing with a total amateur here.
    I should be adding the lines in the theme functions file or in the appropriate bp functions.
    I get a blank page.
    Would the code chunk go after the enqueue line I mentioned?
    If you give up on me I would understand.


    Henry Wright
    Moderator

    @henrywright

    @albert101

    If you copy and paste this into your theme’s functions.php file, it should work.

    function my_dequeue_script() {
        if ( bp_is_activity() )
            wp_dequeue_script( 'xyz-custom-script' );
    }
    add_action( 'wp_print_scripts', 'my_dequeue_script', 100 );

    Your functions.php file will be located at: /wp-content/themes/your-theme/functions.php


    shanebp
    Moderator

    @shanebp

    Either hook will work for the OP’s needs.
    But the wp_enqueue_scripts hook would require a priority greater than the function used to enqueue the script in question. Hard to imagine that 100 wouldn’t suffice.

    My input was more about editing the WP codex and inadvertently introducing some confusion.
    [I love that codex.]


    albert101
    Participant

    @albert101

    Hello again cut and pasted the code exactly on the last line of the theme functions php and
    my debug log output is as follows.
    I must be doing something wrong.

    [10-Sep-2014 19:54:06 UTC] PHP Fatal error: Call to undefined function bp_is_activity() in /Applications/MAMP/htdocs/wordpress/wp-content/themes/mytheme/functions.php on line 4618
    [10-Sep-2014 19:54:06 UTC] PHP Stack trace:
    [10-Sep-2014 19:54:06 UTC] PHP 1. {main}() /Applications/MAMP/htdocs/wordpress/wp-admin/index.php:0
    [10-Sep-2014 19:54:06 UTC] PHP 2. require_once() /Applications/MAMP/htdocs/wordpress/wp-admin/index.php:10
    [10-Sep-2014 19:54:06 UTC] PHP 3. require_once() /Applications/MAMP/htdocs/wordpress/wp-admin/admin.php:204
    [10-Sep-2014 19:54:06 UTC] PHP 4. do_action() /Applications/MAMP/htdocs/wordpress/wp-admin/admin-header.php:108
    [10-Sep-2014 19:54:06 UTC] PHP 5. call_user_func_array:{/Applications/MAMP/htdocs/wordpress/wp-includes/plugin.php:505}() /Applications/MAMP/htdocs/wordpress/wp-includes/plugin.php:505
    [10-Sep-2014 19:54:06 UTC] PHP 6. print_head_scripts() /Applications/AMP/htdocs/wordpress/wp-includes/plugin.php:505
    [10-Sep-2014 19:54:06 UTC] PHP 7. do_action() /Applications/MAMP/htdocs/wordpress/wp-includes/script-loader.php:773
    [10-Sep-2014 19:54:06 UTC] PHP 8. call_user_func_array:{/Applications/MAMP/htdocs/wordpress/wp-includes/plugin.php:505}() /Applications/MAMP/htdocs/wordpress/wp-includes/plugin.php:505
    [10-Sep-2014 19:54:06 UTC] PHP 9. my_dequeue_script() /Applications/MAMP/htdocs/wordpress/wp-includes/plugMin.php:505


    Henry Wright
    Moderator

    @henrywright

    @shanebp even if I provided some explanation of the priority requirement, I think my edit to change the hook to wp_enqueue_scripts may have introduced confusion to some people not as familiar with the priority param. I think it’s safer all round just to use wp_print_scripts, especially as the hook isn’t being deprecated as first thought.


    shanebp
    Moderator

    @shanebp

    @albert101

    Try putting the function in bp-custom.php


    albert101
    Participant

    @albert101

    Thanks i was just in the process of trying that will keep you posted.
    i am reading up on buddypress customization.
    Thanks


    albert101
    Participant

    @albert101

    Hello.
    Thanks for your time I think it is an issue I will have to take up with the theme creator
    since the problem is certainly theme related I wish to not waste any precious time of yours
    Regards


    danbp
    Participant

    @danbp

    Closing this topic and changed the title, as help please is not a support question !

Viewing 24 replies - 1 through 24 (of 24 total)
  • The topic ‘wp_enqueue_script issue on activity page’ is closed to new replies.
Skip to toolbar