Skip to:
Content
Pages
Categories
Search
Top
Bottom

Custom Group Fields not storing if you go to previous step


  • colingdi
    Participant

    @colingdi

    So I found (though I can now track it down) a page with how to add custom fields to a group and it worked (yipee) however during testing we’ve realised that when you add the fields and select ‘Create Group and Continue’ and on the next page select ‘Previous’ your fields are blank. But when you just proceed straight through and don’t ever go back they store. I’m pasting the groups code below if someone knows a hook I can use or similar I’d greatly appreciate it.

    function bp_group_meta_init() {
            function custom_field($meta_key='') {
                //get current group id and load meta_key value if passed. If not pass it blank
                return groups_get_groupmeta( bp_get_group_id(), $meta_key) ;
            }
            //code if using seperate files require( dirname( __FILE__ ) . '/buddypress-group-meta.php' );
            // This function is our custom field's form that is called in create a group and when editing group details
            function group_header_fields_markup() {
                global $bp, $wpdb;?>
    <label for="group-guidelines">Group Guidelines</label>
    <textarea name="group-guidelines" id="group-guidelines" aria-required="true"></textarea>
    <br>
    <label for="group-aims">Group Aims</label>
    <textarea name="group-aims" id="group-aims" aria-required="true"></textarea>
    <?php }
        // This saves the custom group meta – props to Boone for the function
        // Where $plain_fields = array.. you may add additional fields, eg
        //  $plain_fields = array(
        //      'field-one',
        //      'field-two'
        //  );
        function group_header_fields_save( $group_id ) {
            global $bp, $wpdb;
            $plain_fields = array(
                                  'group-guidelines',
                                  'group-aims'
                                  );
            foreach( $plain_fields as $field ) {
                $key = $field;
                if ( isset( $_POST[$key] ) ) {
                    $value = $_POST[$key];
                    groups_update_groupmeta( $group_id, $field, $value );
                }
            }
        }
        add_filter( 'groups_custom_group_fields_editable', 'group_header_fields_markup' );
        add_action( 'groups_group_details_edited', 'group_header_fields_save' );
        add_action( 'groups_created_group',  'group_header_fields_save' );
Viewing 6 replies - 1 through 6 (of 6 total)

  • colingdi
    Participant

    @colingdi

    Hoping someone has a hint for this one as it’s one of the last outstanding issues on the site with my name beside it…


    yusufhm
    Participant

    @yusufhm

    Looks like we both followed the same article: https://codex.buddypress.org/plugindev/how-to-edit-group-meta-tutorial/

    I was able to resolve the issue you were having by modifying the custom_field function a bit, so it becomes like this:

    
    function custom_field($meta_key='') {
            $value = groups_get_groupmeta( bp_get_group_id(), $meta_key);
            if ($value) {
                return $value;
            }
    
            $bp   = buddypress();
            if ($bp->groups->current_group) {
                $group_id = $bp->groups->current_group->id;
                return groups_get_groupmeta($group_id, $meta_key, TRUE);
            }
            return FALSE;
    }
    

    It looks like the meta value’s already saved after the first step, and your group already has an ID; so it’s just a question of retrieving that. bp_get_group_id() somehow does not do the job for I don’t know what reason (maybe the group is not yet visible, or the cache has not been updated yet).


    Prashant Singh
    Participant

    @prashantvatsh

    Hi,

    It’s because you forgot to use this function https://prnt.sc/ohpc6z

    You have created it but have not used it. It returns the value of saved meta.

    So now you have to replace this line:

    
    <textarea name="group-guidelines" id="group-guidelines" aria-required="true"></textarea>
    

    with this

    
    <textarea name="group-guidelines" id="group-guidelines" aria-required="true"><?php echo custom_field('group-guidelines'); ?></textarea>
    

    and this line:

    <textarea name="group-aims" id="group-aims" aria-required="true"></textarea>

    with this

    <textarea name="group-aims" id="group-aims" aria-required="true"><?php echo custom_field('group-aims'); ?></textarea>

    Thanks


    yusufhm
    Participant

    @yusufhm

    Good catch Prashant, although mine was not working even though I was calling that function.


    colingdi
    Participant

    @colingdi

    Thanks both of you for that one, list items now ticked off.

    Prashant your change did the trick, I had that code in there before but I was troubleshooting a different issue and one of the steps had been to remove, never put it back which is poor show on my part!

    Thanks!


    rjudrok
    Participant

    @rjudrok

    Thanks to you all!! This plug is amazing! Just what I’ve been looking for in … months!
    I am not a programmer but I am building a pretty good community.
    The question is… considering both @colingdi and @Prashant codes, would it be possible to show different fields, for different roles?
    For example if role is “Teacher” make group-aims visible, if role is “Student” make group-guidelines visible…
    Any help or suggestion is highly appreciated.

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