How $_GET['updated'] variable is passed when updating a user?

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-dismissiblep'.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
    thlabel for=postcode?php _e(Postal code); ?/label/th
    tdinput type=text name=postcode id=postcode value=?php echo esc_attr($umetas-postcode); ? class=regular-text required=required /br //td
/tr
tr
    thlabel for=mobile?php _e(Mobile); ?/label/th
    tdinput 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.

Topic notices user-meta wp-redirect profiles Wordpress

Category Web


I 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;
}

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.