I have added two user metas and they are functioning perfectly. Now I want to show a warning message according to the input. I have passed the status through a get variable(named meta_warning
through add_query_arg
) and the message is displayed accordingly. But on refreshing the page, the message repeats (since the get variable is passed directly in the url).
I have noticed that the wordpress is using a similar variable (updated=1) to show the 'User Updated' message. But this variable is not present in the url. How can I accomplish this? So that my warning message does not repeat on reload.
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
}
}
?>
Thank you in advance. Also so many thanks for going through the question.
I have added two user metas and they are functioning perfectly. Now I want to show a warning message according to the input. I have passed the status through a get variable(named meta_warning
through add_query_arg
) and the message is displayed accordingly. But on refreshing the page, the message repeats (since the get variable is passed directly in the url).
I have noticed that the wordpress is using a similar variable (updated=1) to show the 'User Updated' message. But this variable is not present in the url. How can I accomplish this? So that my warning message does not repeat on reload.
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
}
}
?>
Thank you in advance. Also so many thanks for going through the question.
Share Improve this question edited Jan 30, 2022 at 22:51 sariDon asked Jan 30, 2022 at 22:16 sariDonsariDon 2651 gold badge2 silver badges18 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 0I have found a javascript solution. Here is the code:
member.js (needs jQuery)
$jq = jQuery.noConflict();
$jq(document).ready(function () {
redirect_on_save_func();
});
function redirect_on_save_func() {
var qvars = get_query_func();
var nq = [];
$jq.each(qvars, function (vk, vv) {
if (vk.match('meta_warning*') == null)
nq.push(vk + '=' + encodeURIComponent(vv));
});
var hr = window.location.href;
var nqs = hr.split('?')[0] + '?' + nq.join('&');
window.history.pushState({}, null, nqs);
}
function get_query_func() {
var pairs = window.location.search.substring(1).split("&"),
obj = {},
pair,
i;
for (i in pairs) {
if (pairs[i] === "") continue;
pair = pairs[i].split("=");
obj[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
return obj;
}
GET
instead ofPOST
? – Tom J Nowell ♦ Commented Jan 30, 2022 at 22:32POST
is meant to be used. Your code looks like it's incomplete though, it's not shown where or howmeta_warning
got added to the URL so it's unclear where the mistake was made that lead you to this problem. You need to go back a few steps to an earlier mistake you've not recognised. E.g. what's stopping you outputting the notices inmember_meta_add
and getting rid ofmeta_warning
entirely? I don't see the need for the redirect either – Tom J Nowell ♦ Commented Jan 31, 2022 at 2:49