Search code examples
phpwordpressbuddypress

Buddypress groups_create_group undefined


I am trying to create a buddypress through a custom plugin with the following code

if(isset($_POST['update-groups'])){
    global $wpdb;

    require_once("https://dev.technologyforthefuture.org/wp-load.php");
    $csv_us_city_map = array_map('str_getcsv',file("https://dev.technologyforthefuture.org/wp-content/plugins/t4tf/Data_Files/uscountiesv1.0.0.csv"));

    /*$groups = BP_Groups_Group::get(array(
                            'type'=>'alphabetical',
                            'per_page'=> -1
                            ));*/

    for($i =0; $i < count($csv_us_city_map); $i++){
        $countyName = $csv_us_city_map[$i][0];
        $stateName = $csv_us_city_map[$i][2];

        $fullGroupName = $countyName . ", " . $stateName;
        $groupSlug = strtolower($countyName) . "-" . strtolower($stateName);
        $groupDescription = "<span style=\"font-family: Roboto, sans-serif;font-size: 13px;text-align: center\">Together we OPEN Doors to a brighter future for the students in $fullGroupName, $stateName.</span>";

        //$groups = $wpdb->get_results("SELECT * FROM wp_bp_groups");

        //$newGroup = $wpdb->insert("wp_bp_groups",array('creator_id' => 1, 'name' => $fullGroupName, 'slug' => $groupSlug, 'description' => $groupDescription, 'status' => 'public'));

        $args = array( 
                        'group_id' => 0, 
                        'creator_id' => 1, 
                        'name' => $fullGroupName, 
                        'description' => $groupDescription, 
                        'slug' => $groupSlug, 
                        'status' => 'public', 
                        'parent_id' => 0, 
                        'enable_forum' => 0 
                    ); 

        $newGroup = groups_create_group($args);
    }
}

I am getting this error when running this code

Fatal error: Uncaught Error: Call to undefined function groups_create_group()

I have tried as you can see to include the wp-load.php file and I have even required the file containing the function groups_create_group but it always shows this same error and I do not know why the function is undefined

SO How do I fix this error?

The path to the file that im working on is wp-content/plugins/t4tf/t4tf.php


Solution

  • Do not use http urls to include php files.

    require_once("https://dev.technologyforthefuture.org/wp-load.php");
    

    is plain wrong on so many levels.

    Use relative or absolute file system path.

    It is not clear where your code is used exactly, so I can't just give you a working code, but something like the following line should work:

    require_once(__DIR__ . '/../wp-load.php');