Skip to:
Content
Pages
Categories
Search
Top
Bottom

Merging all BP JS files into one


  • mrgiblets
    Participant

    @mrgiblets

    Hey guys,

    I’ve put the content of all of the following separate JS files….

    confirm.min.js
    widget-members.min.js
    jquery-query.min.js
    jquery-cookie.min.js
    jquery-scroll-to.min.js
    buddypress.js

    …into one singular file called buddypress.js in mytheme/js/ to help minimize http requests.

    But how do I stop Buddypress from still loading all of the original separate files?

    I’m assuming it’s something to do with :

    remove_action('bp_enqueue_scripts');

    But i’m not sure how to use this in my functions.php, and also, will this stop loading the bp css files? Because I still want them to load as normal.

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

  • mcpeanut
    Participant

    @mcpeanut

    It can be a real pain when combining js scripts so i feel you, i have not yet tried to combine the js files used within buddypress, although i have done this many times with other js files with mixed results, its a case of trial and error manually combining js files, i will probably be trying to do all this myself within my latest buddypress install when the time comes, i would recommend trying a plugin called minqueue to help you combine files as i have used this myself on previous built sites with great results but i have found an issue recently that i posted in the support forum for the plugin which you can read here https://wordpress.org/support/topic/works-at-first-then-logging-out-and-back-in-stops-it-working?replies=1

    maybe you could try it and see if it works for you without the problems i described then i know it wasn’t just my install and config causing it.

    The beauty of this plugin is that it lets you see exactly what scripts are loaded on a per page basis via the front end of your site and if you hover over the said scripts it will let you also know the dependencies of each script, then you can add different scripts to be combined in a queue on the backend, the dev has not responded to my support yet there so im still unsure if its my install that is causing the problem, try it and follow the steps i raised in the support and see if it does the same for you.


    mcpeanut
    Participant

    @mcpeanut


    mrgiblets
    Participant

    @mrgiblets

    Thanks bro, had a look at that plug in but it hasn’t been updated since 2013 and isn’t compatible with my version of WordPress.

    Sounds like a great plug-in though, if it works (haha).

    Hope you get it fixed.

    Still gonna try to do with without a plug-in though as i’ve done all the legwork manually already, just need to stop buddypress from enqueuing the original files (or at very least dequeue them in my functions.php). Just can’t find the functions in BP that are loading them originally.


    mcpeanut
    Participant

    @mcpeanut

    yes i agree it needs an update but its always worked good for me in the past, did you not check the second link i posted?

    it gives you examples on how to dequeue scripts

    /**
     * Dequeue the jQuery UI script.
     *
     * Hooked to the wp_print_scripts action, with a late priority (100),
     * so that it is after the script was enqueued.
     */
    function wpdocs_dequeue_script() {
       wp_dequeue_script( 'jquery-ui-core' );
    }
    add_action( 'wp_print_scripts', 'wpdocs_dequeue_script', 100 );

    mrgiblets
    Participant

    @mrgiblets

    Yes I did see it but I don’t know the names of the functions that are loading the scripts so I can dequeue them.

    I tried dequeing ‘bp_enqueue_scripts’ but it didn’t remove anything.

    I really wish one of the BP devs would just throw a basic editors roadmap together which clearly explains little things like this. I know probably 99% of the people using BP will never want to mess with things like this, but there are plenty of others like us out there that will only want to use BP as long as we can customise it to our requirements.

    The way it all ties together is just so confusing.

    I’ve wasted hours already just trying to find where simple things are located in the file structure before I’ve even managed to start editing and changing what I was looking for.

    Oh well, onwards and upwards 😉


    mrgiblets
    Participant

    @mrgiblets

    Ok managed to do it, it’s a dirty way but it works…

    1) Compile all of the above scripts into a single file “mynewjsfile.js” then minify it to keep it even more compact if you want (forget the buddypress.js thing as what I’ve done stops the script from looking for it completely).

    2) Upload mynewjsfile.js wherever you want to put it (for the sake of this I’ve put it into my theme root /js/mynewjsfile.js

    3) Make a copy of buddypress-functions.php (inside bp-legacy/)

    4) In your new buddypress-functions.php around line 89 comment out the following :

    add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_scripts' ) );

    Like this :

    /* add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); */

    5) Create a new folder called “buddypress” in your theme’s root and upload buddypress-functions.php into it. Now buddypress will use this file instead of the original.

    6) In your wordpress functions.php add the following :

    function mynewjsfile() {
    	wp_enqueue_script( 'mynewjsfile', get_template_directory_uri() . '/js/mynewjsfile.js', 'jquery', false );
    }
    add_action( 'wp_enqueue_scripts', 'mynewjsfile' );

    The ‘jquery’ is there because our new script requires it to function properly. This ensures that Jquery loads before it. False is telling wordpress to load it in the header.

    7) Enjoy your new single minified JS file 😉


    mrgiblets
    Participant

    @mrgiblets

    Only thing i’ve noticed that is telling me that this could have been done better is that when the page first loads the text entry boxes are fully open, then animate to their “normal” state.

    It’s probably just load order though, i’m sure it could be tweaked to work 100% as it should.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Merging all BP JS files into one’ is closed to new replies.
Skip to toolbar