问题描述
我正在尝试使用一些自定义字段自定义 wordpress 站点用户的个人资料。然后自定义字段应显示在前端。 我已设法在用户管理仪表板上显示字段,以允许站点管理员进行更新。
问题 1:但是,保存配置文件后,更新未保存在仪表板中。
//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,$_POST['due']);
update_user_Meta($user_id,'status',$_POST['status']);
update_user_Meta($user_id,'received',$_POST['received']);
}
?>
然后这些字段不会显示在前端。任何帮助将不胜感激。
这是名为dashboard.PHP 的个人资料页面的屏幕截图。我的自定义代码位于注释 //Code by 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,true );
$status = get_user_meta( $user_id,true );
$received = get_user_meta( $user_id,true );
echo esc_html( $due );
echo esc_html( $status );
echo esc_html( $received );