Skip to:
Content
Pages
Categories
Search
Top
Bottom

Plugin Fatal error: Call to undefined function bp_is_active()


  • hiverizecaro
    Participant

    @hiverizecaro

    Hi,

    I followed the Developer Resources → Group Extension API Example trying to create a custom group tab.

    Unfortunately, I get a Fatal error: Call to undefined function bp_is_active() when trying to activate my plugin. (WordPress 4.9.4 buddypress Version 2.9.3)

    What am I missing? I placed the myPlugin.php in a folder …/plugins/myPlugin

    <?php
    /*
    Plugin Name: myPlugin
    Description: lalala.
    Tags: buddypress
    Version: 0.1
    Author: Caro Zett
    License: GPL2
    */
    
    // Exit if accessed directly.
    defined( 'ABSPATH' ) || exit;
    ?>
    <?php
    /* Copyright 2018 Caro Z
    
    This program is free software; ...
    */
    ?>
    
    <?php
    
    /* Only load code that needs BuddyPress to run once BP is loaded and initialized. */
    function my_plugin_init() {
        require( dirname( __FILE__ ) . '/myPlugin.php' );
    }
    add_action( 'bp_include', 'my_plugin_init' );
    
    /**
     * The bp_is_active( 'groups' ) check is recommended, to prevent problems
     * during upgrade or when the Groups component is disabled
     */
    
    if ( bp_is_active( 'groups' ) ) :
    ...
    

    Please feel free to advise on “obvious” stuff as well, this is my first plugin 🙂
    Thanks

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

  • Venutius
    Moderator

    @venutius

    I don’t see a problem with it, are you sure BP is activated?


    hiverizecaro
    Participant

    @hiverizecaro

    yes, it is activated. Everything I tested with it works like a charm (new member sign-up from frontend/backend, create new group, add friends …)

    I am totally lost where this error comes from.

    If I comment this line out I get the nexrt error: Class ‘BP_Group_Extension’ not found


    hiverizecaro
    Participant

    @hiverizecaro

    Update: I split this into two files: the plugin header, the function my_plugin_init() and the add_action into an initthisplugin.php, and the rest into myPlugin.php

    Now myPlugin.php gets loaded, since buddypress is active, but stills throws an Fatal error: Call to undefined function bp_is_active().


    Venutius
    Moderator

    @venutius

    I can replicate your issue, yet I use the same line in my own plugins without error. The only difference is that I look for bp from within a function and I’m thinking this may be the issue, basically wp thinks bp is not active when you are in the plugins page.

    Try wrapping your query in a function tied into your buddypress task.


    Henry Wright
    Moderator

    @henrywright

    Try wrapping your call to bp_is_active() in a function hooked to something like wp.

    For example:

    add_action( 'wp', function() {
        // Call bp_is_active() here
    } );

    You need to do this because it’s likely your plugin is loaded before BuddyPress, hence the bp_is_active() function isn’t defined at that point.


    hiverizecaro
    Participant

    @hiverizecaro

    thanks for the advice.
    I am still confused why the “official” buddypress codex advises to hook plugins to bp_include if this is too early.

    I changed my code to `
    add_action( ‘bp_loaded’, ‘my_plugin_init’ );`

    This solved the fatal error, yet I don’t see any changes in group behaviour…but I’ll look into that.


    Henry Wright
    Moderator

    @henrywright

    “bp_loaded” will also get the job done. You just need to find a hook that’s appropriate. A function needs to be defined before you call it so it’s always worth keeping track of the load order in both WordPress and BuddyPress.

    Feel free to edit the documentation if you find an error. BuddyPress welcomes contributions

Viewing 7 replies - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.
Skip to toolbar