Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hi I tried to create a shortcode to show the join/leave button using the buddypress-functions in a Post but can’t make it to work… :( What am I doing wrong?

I would like a shortcode like this :

[groupbutton name="party1"]

I tried to make this code for a join button only at first:

add_shortcode( 'groupbutton', 'groupbutton_check_shortcode' );

function groupbutton_check_shortcode( $attr ) {

extract( shortcode_atts( array( 'name' => 'read' ), $attr ) );

$current_group_id = BP_Groups_Group::get_id_from_slug($name);
$group = groups_get_group( array( 'group_id' => $current_group_id ) );     
    if(bp_loggedin_user_id()) {
        if( 'public' == $group->status ) {
            echo '<a id="group-' . esc_attr( $current_group_id ) . '" class="join-group" rel="join" title="' . __( 'Join Group', 'buddypress' ) . '" href="' . wp_nonce_url( bp_get_group_permalink( $group ) . 'join', 'groups_join_group' ) . '">' . __( 'Join Group', 'buddypress' ) . '</a>';
        }
    }
}

I am not a developer at all so I dont understand everything but I am trying… :/ Cant figure what I am doing wrong… and why it does not display anything at all.

Thank you for your help and sorry for my bad english :)

PS: I would like to use all cases of buttons like here on the buddypress-functions.php:

function bp_legacy_theme_ajax_joinleave_group() {
// Bail if not a POST action
if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
    return;

// Cast gid as integer
$group_id = (int) $_POST['gid'];

if ( groups_is_user_banned( bp_loggedin_user_id(), $group_id ) )
    return;

if ( ! $group = groups_get_group( array( 'group_id' => $group_id ) ) )
    return;

if ( ! groups_is_user_member( bp_loggedin_user_id(), $group->id ) ) {
    if ( 'public' == $group->status ) {
        check_ajax_referer( 'groups_join_group' );

        if ( ! groups_join_group( $group->id ) ) {
            _e( 'Error joining group', 'buddypress' );
        } else {
            echo '<a id="group-' . esc_attr( $group->id ) . '" class="leave-group" rel="leave" title="' . __( 'Leave Group', 'buddypress' ) . '" href="' . wp_nonce_url( bp_get_group_permalink( $group ) . 'leave-group', 'groups_leave_group' ) . '">' . __( 'Leave Group', 'buddypress' ) . '</a>';
        }

    } elseif ( 'private' == $group->status ) {

        // If the user has already been invited, then this is
        // an Accept Invitation button
        if ( groups_check_user_has_invite( bp_loggedin_user_id(), $group->id ) ) {
            check_ajax_referer( 'groups_accept_invite' );

            if ( ! groups_accept_invite( bp_loggedin_user_id(), $group->id ) ) {
                _e( 'Error requesting membership', 'buddypress' );
            } else {
                echo '<a id="group-' . esc_attr( $group->id ) . '" class="leave-group" rel="leave" title="' . __( 'Leave Group', 'buddypress' ) . '" href="' . wp_nonce_url( bp_get_group_permalink( $group ) . 'leave-group', 'groups_leave_group' ) . '">' . __( 'Leave Group', 'buddypress' ) . '</a>';
            }

        // Otherwise, it's a Request Membership button
        } else {
            check_ajax_referer( 'groups_request_membership' );

            if ( ! groups_send_membership_request( bp_loggedin_user_id(), $group->id ) ) {
                _e( 'Error requesting membership', 'buddypress' );
            } else {
                echo '<a id="group-' . esc_attr( $group->id ) . '" class="membership-requested" rel="membership-requested" title="' . __( 'Membership Requested', 'buddypress' ) . '" href="' . bp_get_group_permalink( $group ) . '">' . __( 'Membership Requested', 'buddypress' ) . '</a>';
            }
        }
    }

} else {
    check_ajax_referer( 'groups_leave_group' );

    if ( ! groups_leave_group( $group->id ) ) {
        _e( 'Error leaving group', 'buddypress' );
    } elseif ( 'public' == $group->status ) {
        echo '<a id="group-' . esc_attr( $group->id ) . '" class="join-group" rel="join" title="' . __( 'Join Group', 'buddypress' ) . '" href="' . wp_nonce_url( bp_get_group_permalink( $group ) . 'join', 'groups_join_group' ) . '">' . __( 'Join Group', 'buddypress' ) . '</a>';
    } elseif ( 'private' == $group->status ) {
        echo '<a id="group-' . esc_attr( $group->id ) . '" class="request-membership" rel="join" title="' . __( 'Request Membership', 'buddypress' ) . '" href="' . wp_nonce_url( bp_get_group_permalink( $group ) . 'request-membership', 'groups_send_membership_request' ) . '">' . __( 'Request Membership', 'buddypress' ) . '</a>';
    }
}

exit;
}

/**
* Close and keep closed site wide notices from an admin in the sidebar, via a POST request.
*
* @return mixed String on error, void on success
* @since BuddyPress (1.2)
*/
function bp_legacy_theme_ajax_close_notice() {
// Bail if not a POST action
if ( 'POST' !== strtoupper( $_SERVER['REQUEST_METHOD'] ) )
    return;

if ( ! isset( $_POST['notice_id'] ) ) {
    echo "-1<div id='message' class='error'><p>" . __( 'There was a problem closing the notice.', 'buddypress' ) . '</p></div>';

} else {
    $user_id      = get_current_user_id();
    $notice_ids   = bp_get_user_meta( $user_id, 'closed_notices', true );
    $notice_ids[] = (int) $_POST['notice_id'];

    bp_update_user_meta( $user_id, 'closed_notices', $notice_ids );
}

exit;
}

I also would like to create a Group for each Wordpress Post I already have with the same name, How could I do that, any tips? thank you very much

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.