0

I've added this code snippet to my functions.php

//hooks
add_action( 'show_user_profile', 'Add_user_fields' );
add_action( 'edit_user_profile', 'Add_user_fields' );

function Add_user_fields( $user ) { ?>

    <h3 class="sizeShapeH3">Select Chapters</h3>
    <table class="form-table">
        <tr>
            <th><label for="dropdown" >MPF Chapters </label></th>
            <td>
            <?php 

                $topselected= get_the_author_meta( 'user_top', $user->ID );  
            ?>

           <select name="user_top" id="user_top">
            <option value="MPF Pune Central:" <?php echo ($topselected == "MPF Pune Central:")?  'selected="selected"' : '' ?>>MPF Pune Central:</option>
            <option value="MPF Pune East:" <?php echo ($topselected == "MPF Pune East:")?  'selected="selected"' : '' ?>>MPF Pune East:</option>
            <option value="MPF Pune West:" <?php echo ($topselected == "MPF Pune West:")?  'selected="selected"' : '' ?>>MPF Pune West:</option>
            <option value="MPF Pune South:" <?php echo ($topselected == "MPF Pune South:")?  'selected="selected"' : '' ?>>MPF Pune South:</option>
            <option value="MPF PCMC:" <?php echo ($topselected == "MPF PCMC:")?  'selected="selected"' : '' ?>>MPF PCMC:</option>


        </select>

        </td>
    </tr>


</table>

<?php }

add_action( 'personal_options_update', 'save_user_fields' );
add_action( 'edit_user_profile_update', 'save_user_fields' );

function save_user_fields( $user_id ) {

    if ( !current_user_can( 'edit_user', $user_id ) )
        return false;

    //save top
    update_usermeta( $user_id, 'user_chapters', $_POST['user_chapters'] );

    }

Now I see the chapter dropdown list at the user profile. but it is not saved when I change the user chapter. Eg: from the above list if I select MPF Pune East and press the button Update Profile then again I see MPF Pune Central as default. Seems it is not get saved when we click Update Profile button. do we have to something else to get it saved?

  • Can you reformat your code so it will be readable – Aftab Sep 7 '17 at 8:24
  • please sir you can check now – Anil Patidar Sep 7 '17 at 8:25
  • Please check the answer. – Aftab Sep 7 '17 at 8:29
0

I can see

$topselected= get_the_author_meta( 'user_top', $user->ID ); ,

here you are using "user_top" as the meta_key but while updating you are using "user_chapter"

update_usermeta( $user_id, 'user_chapters', $_POST['user_chapters'] );

Whatever name you are providing in <select name="user_top" id="user_top">, should be used in $_POST['']

So here you are using "user_top" as your select name, so it will be $_POST['user_top']

  • so can i replace "user_top" to "user_chapter" – Anil Patidar Sep 7 '17 at 9:01
  • 1
    $topselected= get_the_author_meta( "user_chapter", $user->ID ); – Anil Patidar Sep 7 '17 at 9:02
  • yes you can do.. but whatever you are using.. use common in both the place – Aftab Sep 7 '17 at 9:03
  • my meta_key is ..user_chapter_selected and i am using this format . update_usermeta( $user_id, 'user_chapters', $_POST['user_chapter_selected'] ); $topselected= get_the_author_meta( 'user_chapter_selected', $user->ID );... but not working..same problem – Anil Patidar Sep 7 '17 at 9:12
  • and one more thing is my meta_key in wp_usermeta table not in wp_user table... – Anil Patidar Sep 7 '17 at 9:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.