• Resolved tarundeology

    (@tarundeology)


    Hello,

    I added some custom user meta and trying to show it in user edit page and showing only certain usermeta in users list.

    I managed to get it done using the snippet below, but the Membership Level column is empty (not showing anything. Before I added the snippet, it shows the Membersip Level properly in the column.

    
    // Enable custom user meta display & editing
    // Firstly setup/declare custom fields
    function mysite_custom_define() {
      $custom_meta_fields = array();
      $custom_meta_fields['ahli_no_kp'] = 'No Kad Pengenalan';
      $custom_meta_fields['ahli_pekerjaan'] = 'Pekerjaan';
      $custom_meta_fields['ahli_syarikat'] = 'Syarikat/Organisasi';
      $custom_meta_fields['ahli_alamat_syarikat'] = 'Alamat Syarikat/Organisasi';
      $custom_meta_fields['ahli_no_ahli_pskk'] = 'No Ahli';
      return $custom_meta_fields;
    }
    
    // Secondly create the columns and fill them respectively in all users page 
    function mysite_columns($defaults) {
      $meta_number = 0;
      $custom_meta_fields = mysite_custom_define();
      foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
        $meta_number++;
        $defaults[('mysite-usercolumn-' . $meta_number . '')] = __($meta_disp_name, 'user-column');
      }
      return $defaults;
    }
    
    function mysite_custom_columns($value, $column_name, $id) {
      $meta_number = 0;
      $custom_meta_fields = mysite_custom_define();
      foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
        $meta_number++;
        if( $column_name == ('mysite-usercolumn-' . $meta_number . '') ) {
          return get_the_author_meta($meta_field_name, $id );
        }
      }
    }
    
    // Thirdly show this same information on the user profile and edit pages
    function mysite_show_extra_profile_fields($user) {
      print('<h3>Maklumat Keahlian</h3>');
    
      print('<table class="form-table">');
    
      $meta_number = 0;
      $custom_meta_fields = mysite_custom_define();
      foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
        $meta_number++;
        print('<tr>');
        print('<th><label for="' . $meta_field_name . '">' . $meta_disp_name . '</label></th>');
        print('<td>');
        print('<input type="text" name="' . $meta_field_name . '" id="' . $meta_field_name . '" value="' . esc_attr( get_the_author_meta($meta_field_name, $user->ID ) ) . '" class="regular-text" /><br />');
        print('<span class="description"></span>');
        print('</td>');
        print('</tr>');
      }
      print('</table>');
    }
    
    // Fourthly save respective fields when updated
    function mysite_save_extra_profile_fields($user_id) {
    
      if (!current_user_can('edit_user', $user_id))
        return false;
    
      $meta_number = 0;
      $custom_meta_fields = mysite_custom_define();
      foreach ($custom_meta_fields as $meta_field_name => $meta_disp_name) {
        $meta_number++;
        update_usermeta( $user_id, $meta_field_name, $_POST[$meta_field_name] );
      }
    }
    
    // Finally integrate the functions to WP thru these handles
    add_action('show_user_profile', 'mysite_show_extra_profile_fields');
    add_action('edit_user_profile', 'mysite_show_extra_profile_fields');
    add_action('personal_options_update', 'mysite_save_extra_profile_fields');
    add_action('edit_user_profile_update', 'mysite_save_extra_profile_fields');
    add_action('manage_users_custom_column', 'mysite_custom_columns', 15, 3);
    add_filter('manage_users_columns', 'mysite_columns', 15, 1); 
    

    I tried adding Membership Level manually but to no avail. Additional column was created but still empty. See my code below.

    function mysite_custom_define() {
      $custom_meta_fields = array();
      $custom_meta_fields['ahli_no_kp'] = 'No Kad Pengenalan';
      $custom_meta_fields['ahli_pekerjaan'] = 'Pekerjaan';
      $custom_meta_fields['ahli_syarikat'] = 'Syarikat/Organisasi';
      $custom_meta_fields['ahli_alamat_syarikat'] = 'Alamat Syarikat/Organisasi';
      $custom_meta_fields['pmpro_membership_level'] = 'Jenis Keahlian';
      $custom_meta_fields['ahli_no_ahli_pskk'] = 'No Ahli';
      return $custom_meta_fields;
    }
    

    I guess I didnt put the usermeta for membership level properly, because I didnt know what to put instead of “pmpro_membership_level”.

    Can you please help me to show membership level in the users list table?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Plugin Author Andrew Lima

    (@andrewza)

    Hi @tarundeology

    Thank you for using Paid Memberships Pro.

    Paid Memberships Pro does not store the level data as user meta. These are stored in custom tables instead.

    You may be able to use the function pmpro_getMembershipLevelForUser( $user_id ) which will return the membership level object for that particular user. Would this be a workable solution for you?

    Thread Starter tarundeology

    (@tarundeology)

    Thanks @andrewza for your suggestion. I will try it.

    I wonder, why the membership level gone after I added the snippet above? The column is there, but the level for each member is gone. I think its cleaner if I didnt interfere or override the member level that’s already there.

    Thread Starter tarundeology

    (@tarundeology)

    I tried to add the column by adding the code below in functions.php but the membership level is still now showing. I guess I didnt add the function you suggest above properly.

    function new_modify_user_table( $column ) {
        $column['jenis_keahlian'] = 'Jenis Keahlian';
        return $column;
    }
    add_filter( 'manage_users_columns', 'new_modify_user_table' );
    
    function new_modify_user_table_row( $val, $column_name, $user_id ) {
        switch ($column_name) {
            case 'jenis_keahlian' :
                return pmpro_getMembershipLevelForUser( $user_id );
            default:
        }
        return $val;
    }
    add_filter( 'manage_users_custom_column', 'new_modify_user_table_row', 10, 3 );

    Can you show me how to add the function properly please? I’m not a programmer so I’m a bit lost when it comes to syntax.

    Thanks in advance @andrewza

    Plugin Author Andrew Lima

    (@andrewza)

    Looking at your code, the pmpro_getMembershipLevelForUser function returns the level object and not just the name. You should be able to get the name/ID of the level from that function.

    Is there a reason you are custom coding this? This is default functionality of Paid Memberships Pro.

    You may reference the code here to work from – https://github.com/strangerstudios/paid-memberships-pro/blob/42e68b668ba38e8fa65b56dd48e647bff5116a81/includes/init.php#L187-L201

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Membership Level not showing in the admin user list’ is closed to new replies.