Skip to:
Content
Pages
Categories
Search
Top
Bottom

Insert a join group button in a post


  • mattheoh
    Participant

    @mattheoh

    Hi there,
    I m trying for days now, to do something which shouldn’t be very difficult for someone who knows well buddypress and php (that’s unfortunately not my case).

    I just want to add in the bottom of several articles a button to join a group.
    I thought about two solutions :
    – insert the code in single php : in this case I would give the same slug to the article and the group (cause it’s easy to get the slug of article in single.php)
    – create a shortcode that I ll insert in each article I need, with the slug group name, like that : [join name=”slug-group”]

    I made a lot of tests, but I dont manage to do it. these discussions seems to be closed to find a solution, but Brajsh code doesn’t work (maybe cause it’s 2 years ago) : https://buddypress.org/support/topic/leave-and-join-a-group-url-link/
    I just to need to join the group (not leave)
    I m on the last version of BP and WP.

    Thanks a lot for your help !

Viewing 8 replies - 1 through 8 (of 8 total)

  • mattheoh
    Participant

    @mattheoh

    Unfortunately for me , this kind of problem doesn’t seem very popular ! :-/
    So just an up message to wish you a good easter week-end :-p


    danbp
    Moderator

    @danbp

    Hi,

    you tried the complicated way. 😉 Let’s do it simple & stupid (it’s easter).

    Assuming you want to link only to one group, whatever the post.

    1) simplest way
    Pick up the join group url by hovering over the button on the group directory.
    Something like your-site.com/groups/group_name/join?_wpnonce=5b8355ee81d
    Above the post, write “Join group X” as text and add the url. You’re done !

    2) lazy way
    add a shortcode button on the text editor (not visual), with button text and url.

    Add both snippet to bp-custom.php or child-theme’s functions.php
    In this example, my group page is named groupes and the group name is Horseshoe
    Change that to your credentials for $href var (see 1) to get the url).

    function my_join_button($atts, $content='') {		
    
    	extract(shortcode_atts(array(
    	'class'=>'post-join-group' // css class name
    	), $atts));
    
    	$href = get_home_url().'/groupes/horseshoe/join?_wpnonce=6b826ee77d';
    
    	$output = '<span><a href=" '. $href .' ">Join the Horse group</a></span>';
    	if ( $content != '' ) {
    		$output = "$class $content";
    	}
    	return $output;
    }
    add_shortcode('join_my_group','my_join_button');
    
    /* Add a shortcode button to html (text) editor */
    
    function add_quicktags() {  ?>
    <script type="text/javascript" charset="utf-8">
    	QTags.addButton( 'aq_jmg', 'Join my group', '[join_my_group]', '', '', 'Join my group' );
    </script>
    <?php
    }
    add_action( 'admin_print_footer_scripts','add_quicktags');

    mattheoh
    Participant

    @mattheoh

    Hi thanks for your answer ! I appreciate that.

    I have two questions / remarks

    1/ I need the name of the group to be dynamic , it won’t be always the same in the different posts- I mean : it can be a button for joining “group horses” in article 1, “group dogs” in article 2 …
    that’s why I wanted to use the slug of article (and put the same slug for the group) – But if we use a shortcode (that I think is the best) , I ll just have to add the variable as you said…

    2/ the Nonce is not dynamic ??!! It’s always the same for each link ?!
    cause when I tried that in single PHP :
    <?php echo '<a href=" '.wp_nonce_url( bp_loggedin_user_domain() . '/groupes/' . get_the_slug() '/join') .' ">join group</a>'; ?>
    I had a security alert “Are you sure you want to do that ?”

    Thanks again for your time.


    danbp
    Moderator

    @danbp

    it’s always the same for each link ? YES

    Groupes is french. If you use your mentionned code, you have to enter the slug used on your site, in your language. If it’s english, try ‘groups‘ instead.

    Also this will never work like you expect.
    bp_loggedin_user_domain() output gives your-site.com/members/username/groups/

    And i suppose there is no utility to invite from within a post, a user to join a group of which he’s already member of ?!!!

    Can you better explain the relation between post, author and join group ?
    Do you mean, an author can invite readers to join (selectively) a group he’s member of ?


    mattheoh
    Participant

    @mattheoh

    it’s always the same for each link ? YES

    OK , I didn’t know ! thanks

    Groupes is french. If you use your mentionned code, you have to enter the slug used on your site, in your language. If it’s english, try ‘groups‘ instead.

    Of course, I would have correct it myself.
    with your code I managed to insert the slug article

    $href = get_home_url().'/groupes/'.get_the_slug($id).'/join?_wpnonce=6b826ee77d';
    But I will be stuck with the nonce cause it’s proper for each group :-/

    Also this will never work like you expect.
    bp_loggedin_user_domain() output gives your-site.com/members/username/groups/

    OK thanks for the precision

    And i suppose there is no utility to invite from within a post, a user to join a group of which he’s already member of ?!!!
    Can you better explain the relation between post, author and join group ?
    Do you mean, an author can invite readers to join (selectively) a group he’s member of ?

    You’re right, I m going to clarify my needs by the begining ! 😉
    In fact, I want to post some articles to talk about some contests / tests.
    Only identified buddypress members will be able to see this articles (I make a restriction on them)
    At the end of the articles, I wanted to add a button , that give them the possibility to participate to the test.
    In fact, I don’t car using groups… I just need to register people who wants to participate (and then be able to do an export with export user data plugin that I m using actually)
    I thought using groups to qualify this people, cause I thought it was the easier way… (When I write an article about a new test, I create a new group… So people when they click on participate button, update automatically their profile by joining the group related to the article – that’s why I wanted to put the same slug for group and article)
    My final aim is just to export these infos… (complete profile informations and groups particpations)
    So you’re right if someone is already member of a group (so has already clicked on this button), he shouldn’t be able to see the button (or ideally, a different message “you have always sent your participation”)

    I hope it’s a little more comprehensible !
    If you think group is not the good solution, I m listening 😉
    My final purpose is just that user can be tagged as participant and that I can export this information)

    Thanks a lot to have read this cause it was a bit long 😉


    shanebp
    Moderator

    @shanebp

    In fact, I want to post some articles to talk about some contests / tests.
    Only identified buddypress members will be able to see this articles (I make a restriction on them)

    In fact, I don’t car using groups… I just need to register people who wants to participate (and then be able to do an export with export user data plugin that I m using actually)

    Don’t use groups.
    Use user_meta: https://codex.wordpress.org/Function_Reference/add_user_meta
    And the field should be picked up by your export plugin.


    mattheoh
    Participant

    @mattheoh

    Don’t use groups.
    Use user_meta: https://codex.wordpress.org/Function_Reference/add_user_meta
    And the field should be picked up by your export plugin.

    I thought about it … but how to add a button / checkbox in an article which fulfill the user_meta ?
    Didn’t find anything about it…


    mattheoh
    Participant

    @mattheoh

    I made some tests… seems to work with this plugin : https://fr.wordpress.org/plugins/user-meta/
    Maybe User Meta is the good solution (cause I will never use all the possibilities of Groups- So it’s to complicate the solution using them)
    I will go further tomorrow, but anyway thanks a lot for the idea of user meta.

Viewing 8 replies - 1 through 8 (of 8 total)
  • You must be logged in to reply to this topic.
Skip to toolbar