Skip to:
Content
Pages
Categories
Search
Top
Bottom

BP_init not working for themes Functions.php

  • Avatar of Tmort
    Tmort
    Participant

    @tmort

    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)
  • Avatar of Boone Gorges
    Boone Gorges
    Keymaster

    @boonebgorges

    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: http://codex.wordpress.org/Function_Reference/wp_enqueue_script

    Or after_theme_setup

    Avatar of Tmort
    Tmort
    Participant

    @tmort

    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.

    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)

You must be logged in to reply to this topic.