Change user meta value with shortcode

Question

With shortcode, i want to create form to change the car’s name of user


function wp_change_namecar( $atts, $content = null  ) {
    if (is_user_logged_in()) {
        $user_id = get_current_user_id();
        $userval = //how to define that it is the value of the form?

               if (isset($_POST['validate_user'])) {
            if ($_POST['validate_user'] == $user_id) {
                if (update_user_meta( $user_id, 'user_name_car', '$userval' )) {
                    return "<div class="user_updated">Updated!</div>";
                } else {
                    return "<div class="user_updated error">Not Updated!</div>";
                }
            }
        }
        
        //Show the form
        return "<form method='post'>
            <label for="cars">Choose a car:</label>
<select id='cars' name="cars">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="fiat">Fiat</option>
  <option value="audi">Audi</option>
</select>
            <input type="submit" value="Change" />
            </form>";
        
    }
} 
add_shortcode('change_namecar','wp_change_namecar');

THX

0
Olivier 2 months 2021-06-14T04:49:31-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse