0

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.

6
  • is there a particular reason you've chosen to use GET instead of POST?
    – Tom J Nowell
    Jan 30, 2022 at 22:32
  • Because $_POST is empty in this page. I can only trigger the message through $_GET.
    – sariDon
    Jan 30, 2022 at 22:49
  • that doesn't make much sense, you shouldn't have ran into this problem because POST is meant to be used. Your code looks like it's incomplete though, it's not shown where or how meta_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 in member_meta_add and getting rid of meta_warning entirely? I don't see the need for the redirect either
    – Tom J Nowell
    Jan 31, 2022 at 2:49
  • I'm assuming you want to show an error notice if a user doesn't fill out these fields right? Is there a reason you didn't ask how to do that?
    – Tom J Nowell
    Jan 31, 2022 at 2:54
  • I already accomplished it. The warning (if the 'postcode' and the 'mobile' fields are left blank, the system will retain the old values) is also displayed. My problem(as stated in the question) is to stop the warning message appearing again upon refreshing the page.
    – sariDon
    Jan 31, 2022 at 8:00

1 Answer 1

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.