[英]WordPress update_user_meta not saving
我创建了多个其他usermeta字段,并通过WordPress后端进行工作。
但是,我正在尝试创建一个简单的表单,允许人们单击将字段保存到其个人资料的按钮。 我已经经历过StackOverflow,wordpress.stackexchange.com和wordpress.org论坛。 人们似乎可以做到这一点,但我不能做到这一点。
任何人都可以抛弃我的任何帮助,我们深表感谢。
这是我在functions.php中的代码:
function my_function() {
if (is_user_logged_in()) {
global $post;
if (has_badge()) {
$current_user = wp_get_current_user();
$badge_ID = get_post_meta($post->ID, 'badge_id', true);
$badge_check = $current_user->$badge_ID;
$badge_id_1289 = get_user_meta( $current_user->ID, 'badge_id_1289', true);
return '<form name="update_badges" action="#" method="POST">
<fieldset>
<label for="'.$badge_ID.'">Badge Name</label>
<input type="text" id="'.$badge_ID.'" name="'.$badge_ID.'" value="'.$badge_ID.'" />
</fieldset>
<button type="submit">Claim Badge</button>
</form>';
update_user_meta( $current_user->ID, 'badge_id_1289', $_POST['badge_id_1289'] );
}
add_shortcode('subscribe','my_function');
$ badge_ID是从添加了此短代码的帖子中的自定义字段获取的,我已经确认它可以正常工作(它在输入字段中显示为值,在标签中显示为“ for”,等等。)
我什么都没有。 表单提交,页面重新加载,但没有任何内容保存到数据库,因此用户的个人资料中没有任何内容。
任何想法我可能还需要对update_user_meta做什么,或者这是一个短码问题?
@brasofilo是正确的,问题是,我打电话user_update_meta
的前 return
。 这是正确的版本:
function my_function() {
if (is_user_logged_in()) {
global $post;
if (has_badge()) {
$current_user = wp_get_current_user();
$badge_ID = get_post_meta($post->ID, 'badge_id', true);
$badge_check = $current_user->$badge_ID;
$badge_id_1289 = get_user_meta( $current_user->ID, 'badge_id_1289', true);
update_user_meta( $current_user->ID, 'badge_id_1289', $_POST['badge_id_1289'] );
return '<form name="update_badges" action="#" method="POST">
<fieldset>
<label for="'.$badge_ID.'">Badge Name</label>
<input type="text" id="'.$badge_ID.'" name="'.$badge_ID.'" value="'.$badge_ID.'" />
</fieldset>
<button type="submit">Claim Badge</button>
</form>';
}
add_shortcode('subscribe','my_function');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.