使用短码更改用户元值

时间:2021-06-14 作者:Olivier

使用shortcode,我想创建表单来更改汽车的用户名


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公司

1 个回复
最合适的回答,由SO网友:Shakeel Ahmad 整理而成

您犯了错误,您对空值进行了检查,我编译了您的代码并进行了更改,下面是它将帮助您的代码。


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\');

相关推荐

SHORTCODE_ATTS()中的$ATTS参数是什么?

这个WordPress developers reference page for shortcode_atts() 国家:$atts(array)(必选)用户在shortcode标记中定义的属性。但我不理解这个定义。例如,在WP Frontend Profile 插件:$atts = shortcode_atts( [ \'role\' => \'\', ], $atts ); 据我所知,shortcode\