Search code examples
phphtmlwordpressbuddypress

Buddypress - Post an activity update in a group from a custom form?


I use Buddypress for one of my project. I've built a custom form where you can post an activity update into the main stream OR in one of the current user groups via a dropdown menu.

Everything is working fine except that little tricky part of posting into a group.

Here is the code :

PHP

add_action( 'wp_ajax_post_babifun_submit', 'post_babifun_submit' );
add_action( 'wp_ajax_nopriv_post_babifun_submit', 'post_babifun_submit' );
function post_babifun_submit() {

    $component = 'activity'; //Default value
    $type = 'activity_update'; //Default value

    $args = array('action'=>'','component'=>$component, 'type'=>$type, 'content'=>$_POST['m'], 'item_id'=>'');


    if(isset($_POST['component'])){
        if($_POST['component'] != '' && $_POST['component'] != 'activity'){
            $args['component'] = $_POST['component'];       //"groups"
            $args['item_id'] = intval($_POST['groupid']);   // group ID
            $args['type'] = $_POST['type'];                 //"activity_update"
        }
        $act_ID = bp_activity_add($args);
    }else{
        $act_ID = bp_activity_add($args);
    }


     // send some information back to the javascipt handler
    if(isset($_POST['m'])){
        $response = array(
            'status' => '200',
            'message' => 'OK',
            'new_post' => $act_ID
        );

        // normally, the script expects a json respone
        header( 'Content-Type: application/json; charset=utf-8' );
        echo json_encode( $response );
    }

    exit; // important
}

JS

$P("#babifun").submit(function( event ) {
    event.preventDefault();

    var message = $P('#quoi_9').val();
    var submitData = {
        action:'post_babifun_submit',
        m:message
    }


    /***************
    //  POST TARGET
    ***************/
    if($P('#wiki-post-in').val() != ''){
        submitData.groupid = $P('#wiki-post-in').val();
        submitData.component = 'groups';
        submitData.type = 'activity_update';
    }



    /***************
    //  SUBMIT & SHOW
    ***************/
    $P.ajax({
        type: "POST",
        url: "http://wiki.mhweb.ca/wp-admin/admin-ajax.php",
        data: submitData,
        error: function(jqXHR, textStatus, errorThrown){                                        
            console.error("The following error occured: " + textStatus, errorThrown);                                                       
        },
        success: function(data) {
            var id = data.new_post;
            console.log(data);
            $P.ajax({
                type: "POST",
                url: "http://wiki.mhweb.ca/wp-admin/admin-ajax.php",
                data: {action:'get_last_formated_post',id:id},
                error: function(jqXHR, textStatus, errorThrown){                                        
                    console.error("The following error occured: " + textStatus, errorThrown);                                                       
                },
                success: function(data) {
                    $P("#activity-stream").prepend(data);
                    $P(".fancybox").fancybox();                                   
                }                              
            });                                                                 
        }                              
    }); 
});

Solution

  • For some reasons, bp_activity_add doesn't work properly with groups activity.

    I use this instead :

    $act_ID = groups_record_activity($args);
    

    Hope this help !