Site icon Hip-Hop Website Design and Development

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-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.