Skip to:
Content
Pages
Categories
Search
Top
Bottom

Leave and Join a Group button via shortcode to use in pages

Viewing 11 replies - 1 through 11 (of 11 total)
  • @doremdou

    Participant

    Hello again… ummm someone? an idea? 🙂
    Should I just copy paste the button function to make a url work…? thank you

    @doremdou

    Participant

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

    I would like a shortcode like this :

    [groupbutton name=”party1″]

    I tried with 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;
    }

    @doremdou

    Participant

    Hi again 🙂
    Can someone just tell me if it is not possible at all? :/
    Trying things for 6 days already… can someone just tell me if I am on the right path or if I am totally wrong ^^
    Thank you 🙂

    @hnla

    Participant

    BP provides all the buttons you need, if you are not seeing them then you have larger issues likely in the use of a custom theme, but you haven’t mentioned the theme you’re using (see the forum stickies for detail to provide in posts) Have you changed theme to a known working one – any of the default WP ones – this will tell you if this is a theme issue.

    Once the cause is established then you can proceed with the best approach to correcting the problem but throwing functions in at this stage isn’t wise.

    @doremdou

    Participant

    Hello @hnla thank you for your answer 🙂
    But I can see the button already in the Group page it is working well, I just want to be able to put the join/leave button for a specific group on a WordPress Post or a Page for example 🙂
    (That is why at first I wanted to put a simple button with a Link to the Group URL but it is not working, I should have changed my topic title but I can’t edit anything)
    That is why I thought it would be easier to create a shortcode for it 🙂

    @hnla

    Participant

    Oh I see, hmm looks like it will be trickier than one would like, seems the extract $atts is causing issue running too late for bp to work with and ob_buffering doesn’t seem to help, works without user atts ok just not when they’re added to shortcode, there’s probably another solution but personally haven’t time to investigate it, maybe someone else knows of an approach that works.

    @doremdou

    Participant

    @hnla ok no problem thank you for your answer 🙂
    I see you are a moderator, maybe you could just change my topic title to be more explicit for people to help, for example “Leave/Join Group Button shortcode on any Posts/Pages” please? 🙂
    Thank you very much

    @doremdou

    Participant

    Hello… ^^
    Been on this for a week now and can’t figure how to do it… 🙁
    If someone has an idea for this I would be really grateful, or then… think I will just abandon the idea :/
    Thanks

    @doremdou

    Participant

    Hello 🙂
    I am still looking to make this work, does anyone have an idea? 🙂
    Thank you very much 🙂

    @sbrajesh

    Participant

    Do you want the join and leave button depending on the currently logged in user ina single shortcode or 2 different shortcode. I am not sure, so here is the code that will give you join button for any group

    
    add_shortcode( 'group-join', 'devb_group_join_btn' );
    function devb_group_join_btn( $atts = '', $content = null ){
        if( !is_user_logged_in() || !bp_is_active( 'groups' ) )
            return '';
        $atts = shortcode_atts( array(
                'group_id'=> false,
                'name'    => ''  
        ), $atts );
        
        extract( $atts );
    
        if( !$group_id && !$name )
          return '';
    
        if( $name ){
            
            $group_id = groups_get_id( $name );
        }
    
        if( groups_is_user_member(get_current_user_id(), $group_id ) )
                return '';
        
        //now let us put the button
        $btn = bp_get_group_join_button( groups_get_group(array('group_id'=> $group_id ) ) );
        
        return $btn;
    }
    
    

    please make sure to pass the slug of group as name. If you want it to be extendede and leave button added, please do reply.

    @doremdou

    Participant

    Hello Brajesh Singh ! 🙂
    Thank you for your answer 🙂

    I will test your code in a few hours when I am back home ! ^^

    And yes a join and leave button depending on the currently logged in user in a single shortcode would be awesome! 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Leave and Join a Group button via shortcode to use in pages’ is closed to new replies.
Skip to toolbar