admin管理员组

文章数量:1291134

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

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

Share Improve this question edited Jun 14, 2021 at 21:12 Shakeel Ahmad 1386 bronze badges asked Jun 14, 2021 at 9:49 OlivierOlivier 196 bronze badges 1
  • "how to define that it is the value of the form?" I think you need to check if it's set, because for the first load it will give you an error. isset($_POST['cars']) ? $userval = esc_attr($_POST['cars']) : $userval =''; – anton Commented Jun 14, 2021 at 11:07
Add a comment  | 

1 Answer 1

Reset to default 1

You made error, you made checks on the value which were null, i compile your code and made changes, here is the code it will help you.


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

        if ($userval && $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');

本文标签: Change user meta value with shortcode