【问题标题】:WP Edit user custom fields updateWP 编辑用户自定义字段更新
【发布时间】:2017-05-06 19:07:11
【问题描述】:

所以我已经很接近了,但这里肯定有一个错误。 我正在做的是我在 WP wooCommerce 安装中添加了 3 个自定义字段。我已经通过 hooks 和 woo 模板添加到注册和我的帐户页面。我可以从他们的个人资料区域/前端成功地以用户身份更新。 我坚持的是让编辑用户管理表单更新这些字段。我可以显示这些字段,但它们只是没有更新。

寻求想法和帮助。我错过了什么?提前致谢。

这是我在子主题functions.php中的代码 - ADMIN EDIT USER

function sbb_custom_user_profile_fields($user) {
?>
<legend><h4>Reseller Info</h4></legend>
<table class="form-table">
<tr>
<th>
    <label for="ssb_reseller_tax_id"><?php _e('Resseller Tax ID'); ?></label>
</th>
<td>
    <input type="text" name="sbb_reseller_tax_id" id="sbb_reseller_tax_id" 
value="<?php echo esc_attr( get_the_author_meta( 'reseller_tax_id', $user->ID 
) ); ?>" class="regular-text" />
</td>
</tr>
<tr>
<th>
    <label for="sbb_title"><?php _e('Title'); ?></label>
</th>
<td>
<input type="text" name="sbb_title" id="sbb_title" value="<?php echo esc_attr( get_the_author_meta( 'title', $user->ID ) ); ?>" class="regular-text" />
</td>
</tr>
<tr>
<th>
    <label for="sbb_fax"><?php _e('Fax'); ?></label>
</th>
<td>
    <input type="text" name="sbb_fax" id="sbb_fax" value="<?php echo esc_attr( get_the_author_meta( 'fax', $user->ID ) ); ?>" class="regular-text" />
</td>
</tr>
</table>
<?php
update_user_meta( get_the_author_meta( $user->ID ), 'reseller_tax_id', htmlentities( $_POST[ 'reseller_tax_id' ] ) );
update_user_meta( get_the_author_meta( $user->ID ), 'fax', htmlentities( $_POST[ 'fax' ] ) );
update_user_meta( get_the_author_meta( $user->ID ), 'title', htmlentities( $_POST[ 'title' ] ) );
}

add_action('show_user_profile', 'sbb_custom_user_profile_fields');
add_action('edit_user_profile', 'sbb_custom_user_profile_fields');
add_action('edit_user_profile_update', 'sbb_custom_user_profile_fields' );

【问题讨论】:

    标签: php wordpress woocommerce edit


    【解决方案1】:

    我在您发布的代码中看到的直接问题是您为输入名称添加了前缀。您可以使用元键reseller_tax_id 保存值,但输入名称为sbb_reseller_tax_id。这是您需要在$_POST 中使用的输入名称。

    $_POST[ 'reseller_tax_id' ] ) 将变为 $_POST['sbb_reseller_tax_id'] )

    我建议在尝试保存表单之前检查表单是否已提交,并且您有输入值。仅仅查看一个页面不应该具有执行更新的效果。

    【讨论】:

    • 谢谢你,Nathan - 我会试一试,让你知道
    猜你喜欢
    • 1970-01-01
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多