Skip to:
Content
Pages
Categories
Search
Top
Bottom

BP_init not working for themes Functions.php

  • @tmort

    Participant

    I’ve got a custom theme going, and I’d like to only add the CSS/JS/Ajax.php file’s required to run buddypress to load with the following perameters:

    1. Is buddypress activated? Yes? Good, load global.js, buddypress.css and require ajax.php

    2. Is the user on Buddypress Page X, Y, or Z? Yes? Good, load global.js, buddypress.css and require ajax.php

    I’ve got this in my functions.php so far, but `bp_init` doesn’t seem to be working. When I load just normal `init` everything loads. Well, except for the Ajax.php, I get a WSOD when I try to require that. Can anyone help with `bp_init` not working?

    `function bp_req() {

    define(“BP_PATH”, WP_CONTENT_URL . ‘/buddypress’);

    /* BuddyPress is installed and activated, finish initialization and go! */

    //require_once( BP_PATH . ‘/bp-themes/bp-default/_inc/ajax.php’ );

    wp_enqueue_script( ‘bp-js’, BP_PATH . ‘/bp-themes/bp-default/_inc/global.js’, array( ‘jquery’ ) );

    }
    add_action( ‘bp_init’, ‘bp_req’ );`

Viewing 4 replies - 1 through 4 (of 4 total)
  • @boonebgorges

    Keymaster

    It’s possible that bp_init is fired too early in the process for it to be detected with functions.php, which is only loaded with your theme (relatively late). Try hooking to something like ‘wp’ instead. You should be able to detect whether BP is activated by doing something like
    `if ( !function_exists( ‘bp_loaded’ ) )
    return;`

    In any case, you should be enqueuing scripts on the init action, which is unrelated to bp_init: https://codex.wordpress.org/Function_Reference/wp_enqueue_script

    @djpaul

    Keymaster

    Or after_theme_setup

    @tmort

    Participant

    Thanks to you both!

    @Boone Gorges The reason I’m doing it like this is because I’d like to only load the JS when Buddypress is using it, so if the user is on a blog post that has no BP component it won’t load unnecessary JS.

    @djpaul

    Keymaster

    if you do that, you’ll need to consider the impact on widgets and third-part BP plugins.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘BP_init not working for themes Functions.php’ is closed to new replies.
Skip to toolbar