Buddypress - 从自定义表单中发布组中的活动更新?

时间:2014-07-21 16:46:53

标签: php html5 wordpress buddypress

我将Buddypress用于我的一个项目。我已经构建了一个自定义表单,您可以通过下拉菜单将活动更新发布到主流或其中一个当前用户组中。

除了发布到小组中的一些棘手的部分之外,一切都很好。

以下是代码:

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();                                   
                }                              
            });                                                                 
        }                              
    }); 
});

1 个答案:

答案 0 :(得分:1)

由于某些原因, bp_activity_add 无法与群组活动一起正常运行。

我改用它:

$act_ID = groups_record_activity($args);

希望这有帮助!