php - wordpress 允许管理员使用自定义字段编辑用户配置文件
问题描述
我正在尝试使用一些自定义字段自定义 Wordpress 站点用户的个人资料。然后,自定义字段应显示在前端。我已设法在用户管理仪表板上显示字段,以允许站点管理员进行更新。
问题 1:但是,保存配置文件后,更新未保存在仪表板中。
问题 2:更新后的字段未显示在用户配置文件的前端。
这是我在孩子主题的functions.php中的代码:
//add field for editing on user profile
/**
* @param $user
* @author Webkul edited by Wano
*/
// add_action('show_user_profile', 'wk_custom_user_profile_fields');
add_action('edit_user_profile', 'wk_custom_user_profile_fields');
function wk_custom_user_profile_fields($user)
{
echo '<h3 class="heading">Custom Fields</h3>';
?>
<table class="form-table">
<tr>
<th><label for="due">Amount Due</label></th>
<td><input type="text" class="input-text form-control" name="due" id="due" />
</td>
</tr>
<tr>
<th><label for="status">Payment Status</label></th>
<td>
<select id="status" name="status" size="">
<option value="pending">Pending</option>
<option value="approved">Approved</option>
</select>
</td>
</tr>
<tr>
<th><label for="received">Payment received</label></th>
<td><input type="text" class="input-text form-control" name="received" id="received" />
</td>
</tr>
</table>
<?php
}
这是仪表板中的外观。到目前为止一切都很好。
然后我在这里保存字段。
//Save custom user profile meta data fields
//add_action('personal_options_update', 'wk_save_custom_user_profile_fields');
add_action('edit_user_profile_update', 'wk_save_custom_user_profile_fields');
/**
* @param User Id $user_id
*/
function wk_save_custom_user_profile_fields($user_id)
{
// $custom_data = $_POST['due'];
// update_user_meta($user_id, 'due', $custom_data);
if (!current_user_can('edit_user', $user_id)) {
return false;
}
update_user_meta($user_id, 'due', $_POST['due']);
update_user_meta($user_id, 'status', $_POST['status']);
update_user_meta($user_id, 'received', $_POST['received']);
}
?>
然后这些字段不会显示在前端。任何帮助将不胜感激。
这是名为dashboard.php 的配置文件页面的屏幕截图。我的自定义代码位于评论之间 //Wayne 的代码。
我显示的是登录用户的用户名,然后我打算在下面显示自定义字段 $due、$status 和 $received。我在代码中包含了 custom-code.php。
这是我在 custom-code.php 中用来显示显然非常错误的字段的内容。
<?php
do_action('wk_custom_user_profile_fields', $due);
do_action('wk_custom_user_profile_fields', $status);
do_action('wk_custom_user_profile_fields', $received);
?>
解决方案
看来逻辑是对的。
可能,将值提取到您的HTML
模板可以解决问题
add_action( 'edit_user_profile', 'wk_custom_user_profile_fields' );
function wk_custom_user_profile_fields( $user ) {
$due = get_user_meta( $user->ID, 'due', true );
$status = get_user_meta( $user->ID, 'status', true );
$received = get_user_meta( $user->ID, 'received', true );
echo '<h3 class="heading">Custom Fields</h3>';
?>
<table class="form-table">
<tr>
<th><label for="due">Amount Due</label></th>
<td><input type="text" class="input-text form-control" name="due" id="due" value="<?php echo esc_attr( $due ) ?>"/>
</td>
</tr>
<tr>
<th><label for="status">Payment Status</label></th>
<td>
<select id="status" name="status" size="">
<option value="pending" <?php selected( $status, 'pending' ) ?>>Pending</option>
<option value="approved" <?php selected( $status, 'approved' ) ?>>Approved</option>
</select>
</td>
</tr>
<tr>
<th><label for="received">Payment received</label></th>
<td><input type="text" class="input-text form-control" name="received" id="received" value="<?php echo esc_attr( $received ) ?>"/>
</td>
</tr>
</table>
<?php
}
我认为,显示没有的字段do_action
可以解决问题
custom-code.php
内容:
<?php
$user_id = get_current_user_id();
if ( ! is_user_logged_in() ) {
return;
}
$due = get_user_meta( $user_id, 'due', true );
$status = get_user_meta( $user_id, 'status', true );
$received = get_user_meta( $user_id, 'received', true );
echo esc_html( $due );
echo esc_html( $status );
echo esc_html( $received );
推荐阅读
- azure - 如何在 Azure Active Directory 中设置服务原则显示名称
- reactjs - GraphQL / Relay - 是否需要在主 QueryRenderer 中查询所有表?
- python - 如何使用正则表达式解析html中的段落
- sql - 从多对多关系的桥接表中检索数据的查询
- c# - 在 where 子句中设置了动态值的实体框架不起作用
- c# - 如何避免在 Windows 窗体应用程序的文本框中存储重复的日期?
- phpmailer - setFrom 替代 goDaddy
- c# - 使用 CreateWindowEx() 的 WPF 自定义 MainWindow
- javascript - jQuery .change() 在 HTML 文本编辑器上不起作用
- ios - 深度复制 CVPixelBuffer 用于 Swift 中的深度数据