1

Hi can anyone help me to save the post title selected in this select box? I would like to let people select posts out of a dropdown select box and save the value to their profile.

Help is very appreciated!

add_action( 'show_user_profile', 'product_selection_field', 5 );
add_action( 'edit_user_profile', 'product_selection_field', 5 );
function product_selection_field( $user ) {
$args     = array( 'post_type' => 'product', 'product_tag' => 'partner' );
$products = get_posts( $args );
foreach ( $products as $page ) {
            if ( strtolower($product_name) == $page ) {
                $role_option .= "<option value='".strtolower($page->post_title)."' selected='selected'>";
                $currentrole = strtolower($role_name);
            } else {
                $product_option .= "<option value='".strtolower($page->post_title)."'>";
            }

            $product_option .= $page->post_title;
            $product_option .= "</option>";

}
?>
<div class="form-group full-width">
<label for="club_choice"><?php _e("Select your club"); ?></label>
    <select name="club_choice" id="club_choice" class="input"> 
    <?php 
    echo $product_option;
    ?>
    </select>
</div>
<?php 
}

 // then I save the field
 add_action('user_register','adding_extra_reg_fields');
 function adding_extra_reg_fields($user_id) {
if ( !empty( $_POST['club_choice'] ) )
update_user_meta( $user_id, 'club_choice', $_POST['club_choice'] );

 }

2 Answers 2

2

Ahh it turns out it wasn't able to stay selected because I was using the "strtolower" function to remove capital letters. Which in turn caused there to be a mismatch between the selected value and the saved value in the database.

add_action( 'show_user_profile', 'product_selection_field', 3 );
add_action( 'edit_user_profile', 'product_selection_field', 3 );

function product_selection_field( $user ) {
if( current_user_can('free') || current_user_can('administrator') ) {
$user_ID = get_current_user_id();
$current_product = get_user_meta( $user_ID, "club_choice", true );
$args     = array( 'post_type' => 'product', 'product_tag' => 'partner' );
$products = get_posts( $args );

   foreach ( $products as $page ) {
    $string = $page->post_title;
    $product_option .= "<option value='" . $page->post_title . "' " . selected(     $current_product, $string, false ) . ">";
    $product_option .= $page->post_title;
    $product_option .= "</option>";
}
?>
<div class="form-group full-width">
    <?php
  $user_ID = get_current_user_id();
  $all_meta_for_user = get_user_meta( $user_ID, "club_choice", true );
  echo '<label><strong>Your current club selected is: </label>';
  print_r( $all_meta_for_user );
  echo '</strong><br/>';
?>
</div>
<div class="form-group full-width">
    <label for="club_choice"><?php _e("Select your club"); ?></label>
        <select name="club_choice" id="club_choice" class="input"> 
            <?php 
        echo $product_option;
        ?>
        </select>
</div>
<?php 
}
}


// then I save the field
add_action( 'personal_options_update', 'adding_club_reg_fields', 10, 1 );
add_action( 'user_register', 'adding_club_reg_fields', 10, 1 );
function adding_club_reg_fields( $user_id ) {
    if ( isset( $_POST['club_choice'] ) ) {
        update_user_meta( $user_id, 'club_choice', $_POST['club_choice'] );
    }
}
0

The code below works. I added personal_options_update action hook to save user profile. In product_selection_field function you need to get user meta value first. Besides you can use selected function to add selected attribute to option tag.

add_action( 'show_user_profile', 'product_selection_field' );
add_action( 'edit_user_profile', 'product_selection_field' );

function product_selection_field( $user ) {
    $product_name = get_user_meta( $user->id, "club_choice", true );
    $args     = array( 'post_type' => 'product', 'product_tag' => 'partner' );
    $products = get_posts( $args );

    foreach ( $products as $page ) {
        $product_option .= "<option value='" . strtolower( $page->post_title ) . "' " . selected( $product_name, $page->post_name, false ) . ">";
        $product_option .= $page->post_title;
        $product_option .= "</option>";
    }
?>
<div class="form-group full-width">
    <label for="club_choice"><?php _e("Select your club"); ?></label>
        <select name="club_choice" id="club_choice" class="input"> 
        <?php 
        echo $product_option;
        ?>
        </select>
    </div>
<?php 
}

// then I save the field
add_action( 'personal_options_update', 'adding_extra_reg_fields', 10, 1 );
add_action( 'user_register', 'adding_extra_reg_fields', 10, 1 );
function adding_extra_reg_fields( $user_id ) {
    if ( isset( $_POST['club_choice'] ) ) {
        update_user_meta( $user_id, 'club_choice', $_POST['club_choice'] );
    }
}
1
  • Hi yes, I see I had the wrong action on this paste.. Actually the value is saving now. I created a dump with $all_meta_for_user = get_user_meta( ); print_r( $all_meta_for_user ); Just the value wont stay selected in the dropdown. Which is kind of a problem.. Oct 5, 2014 at 23:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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