我添加了两个用户元,它们的功能非常好。现在,我想根据输入显示一条警告消息。我通过一个get变量(名为meta_warning
通过add_query_arg
) 并相应显示消息。但是在刷新页面时,消息会重复(因为get变量直接在url中传递)。
我注意到wordpress使用了一个类似的变量(updated=1)来显示“User updated”消息。但url中不存在此变量。我如何才能做到这一点?这样我的警告消息在重新加载时不会重复。
memberAdmin.class.php
<?php
class memberAdmin
{
public function __construct()
{
add_action(\'load-user-edit.php\', array($this, \'member_meta_message\'), 10, 2);
add_action(\'show_user_profile\', array($this, \'member_meta_form\'));
add_action(\'edit_user_profile\', array($this, \'member_meta_form\'));
add_action(\'edit_user_profile_update\', array($this, \'member_meta_add\'));
}
function member_meta_message()
{
/* URL: http://localhost/wordpress/wp-admin/user-edit.php?
user_id=6&
wp_http_referer=/wordpress/wp-admin/users.php&
meta_warning[0]=postcode
*/
echo \'<pre>\';
print_r($_GET);
echo \'</pre>\';
/************* OUTPUT *******************
Array
(
[user_id] => 6
[updated] => 1
[wp_http_referer] => /wordpress/wp-admin/users.php
[meta_warning] => Array
(
[0] => postcode
)
)
********************************************/
if(!empty($_GET[\'meta_warning\']))
{
echo \'<div class="notice notice-warning is-dismissible"><p>\'.implode(\', \', $_GET[\'meta_warning\']).\' fields not updated</p></div>\';
// Message displayed correctly
}
}
function member_meta_add($user_id)
{
$err_fields = array();
if (isset($_POST[\'submit\'])) {
$membObj = new stdClass();
$membObj->postcode= (isset($_POST[\'postcode\'])) ? strip_tags($_POST[\'postcode\']) : \'\';
if (!empty($membObj->postcode))
update_user_meta($user_id, \'postcode\', $membObj->postcode);
else
array_push($err_fields, \'postcode\');
$membObj->mobile = (isset($_POST[\'mobile\'])) ? strip_tags($_POST[\'mobile\']) : \'\';
if (!empty($membObj->mobile))
update_user_meta($user_id, \'mobile\', $membObj->mobile);
else
array_push($err_fields, \'mobile\');
if (!empty($err_fields)) {
add_filter( \'wp_redirect\', function( $location ) use ( $err_fields ) {
return add_query_arg( \'meta_warning\', $err_fields, $location );
});
}
}
}
function member_meta_form($user)
{
require_once ABSPATH . \'/lib/myWPUtilObj.class.php\';
$myWPUtilObj= new myWPUtilObj();
$umetas = $myWPUtilObj->get_member_meta($user->ID);
?>
<h3><?php _e("Member information", "blank"); ?></h3>
<table class="form-table" role="presentation">
<tr>
<th><label for="postcode"><?php _e("Postal code"); ?></label></th>
<td><input type="text" name="postcode" id="postcode" value="<?php echo esc_attr($umetas->postcode); ?>" class="regular-text" required="required" /><br /></td>
</tr>
<tr>
<th><label for="mobile"><?php _e("Mobile"); ?></label></th>
<td><input type="text" name="mobile" id="mobile" value="<?php echo esc_attr($umetas->mobile); ?>" class="regular-text" required="required" /><br /></td>
</tr>
</table>
<?php
}
}
?>
提前谢谢你。也非常感谢您回答这个问题。