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 |1 Answer
Reset to default 1You 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
版权声明:本文标题:Change user meta value with shortcode 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741505103a2382274.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
isset($_POST['cars']) ? $userval = esc_attr($_POST['cars']) : $userval ='';
– anton Commented Jun 14, 2021 at 11:07