• Greetings.

    I’m working on a custom plugin which puts custom fields in the User Profile. I have 2 diff types of fields. One is for mobile_number. That part works. I have another group of checkbox items. They’re both meant to use update_user_meta() when the form processes, but my checkboxes aren’t updating.

    While debugging (xdebug stepping w/ VS Code), I see that the _POST[checkboxes] values are there at first, but then (after some native functions process) the http _POST request values are no longer there, and _GET is populated with the &user_id and &wp_http_referer . I’ve not seen this sort of thing before. (Is that a result of Ajax?)

    My database queries etc work correctly outside of having the code in the User Profile page.

    Here’s a chunk of what i’m working with. What do you recommend I look for?

    The function to process the _REQUEST data / update_user_meta():

        public function zto_membergroup_members()
        {
            $zto_helpers = new Zto_Helpers();
            if (isset($_REQUEST['updated'])) {
                $membergroups_from_db = get_user_meta($user_id, 'zto_membergroup');
                $membergroups = \wp_parse_args($membergroups_from_db, $zto_helpers->zto_membergroup_defaults());
                $membergroups['zto_membergroup_one'] = sanitize_text_field($_REQUEST['zto_membergroup']['zto_membergroup_one']);
                $membergroups['zto_membergroup_two'] = sanitize_text_field($_REQUEST['zto_membergroup']['zto_membergroup_two']);
                $membergroups['zto_membergroup_three'] = sanitize_text_field($_REQUEST['zto_membergroup']['zto_membergroup_three']);
                $membergroups['zto_membergroup_four'] = sanitize_text_field($_REQUEST['zto_membergroup']['zto_membergroup_four']);
                $membergroups['zto_membergroup_five'] = sanitize_text_field($_REQUEST['zto_membergroup']['zto_membergroup_five']);
            } else {
                $membergroups = $zto_helpers->zto_membergroup_defaults();
            }
            update_user_meta($user_id, 'zto_membergroup', $membergroups);
        }

    The HTML for the form for the User Profile fields:

        <th><label for="mobile_number">Mobile Number:</label></th>
        <td><input type="text" name="mobile_number" id="mobile_number" placeholder="9876543210" value="<?php echo $mobile_number ? $mobile_number : ''; ?>" class="regular-text"></td>
    </tr>
    <?php
        $user_membership = get_user_meta($user_id, 'zto_membergroup');
        $user_membership = wp_parse_args($user_membership, $zto_helpers_general->zto_membergroup_defaults());
        $html_concat .= '<tr><td><label for="';
        $html_concat .= esc_html('zto_membergroup').'[zto_membergroup_one]" id="zto_membergroup_one">Group One Member</label>';
        $html_concat .= '<input class="cm-toggle" type="checkbox" name="';
        $html_concat .= esc_html('zto_membergroup').'[zto_membergroup_one]" id="';
        $html_concat .= esc_html('zto_membergroup').'[zto_membergroup_one]" value="1" ';
        $html_concat .= checked($user_membership['zto_membergroup_one'], '1', true).'>
    </td></tr>
    ';

    Thank you!

    To illustrate that the _POST values are there. Here, the first of five checkboxes where checked, thus _membergroup_one = 1.
    • This topic was modified 1 year, 6 months ago by ajaxStardust. Reason: clarification
    • This topic was modified 1 year, 6 months ago by ajaxStardust. Reason: illustration
Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter ajaxStardust

    (@ajaxstardust)

    I changed the first part of the update_user_meta() function to check for the correct http request. what i have above reflects the values (erroneously) of the _GET req i’d mentioned. oddly, I don’t notice any remarkable changes in the way it’s functioning:

        public function zto_membergroup_members()
        {
            $zto_helpers = new Zto_Helpers();
            if ($_POST['action'] == 'updated') {
    • This reply was modified 1 year, 6 months ago by ajaxStardust.
    Thread Starter ajaxStardust

    (@ajaxstardust)

    I think I’m on the right track, but still stuck. I discovered that when “the form” (the Settings API registered fields) work as desired, the following is what’s put in wp_usermeta. The first line shows results of a user joining one member group. the second is accompanied by a screenshot:

    # member of one group:
    a:1:{s:20:"zto_membergroup_four";s:1:"1";}
    
    # member of three groups:
    a:3:{s:19:"zto_membergroup_two";s:1:"1";s:21:"zto_membergroup_three";s:1:"1";s:20:"zto_membergroup_four";s:1:"1";}
    The form selections resulting in the a:3 (#member of 3 groups) as described above

    When the data isn’t processed as I want, the following is what I’m getting (first row, then second after second submission, and so on adding more multi-dimensional arrays):

    Note that the sub-array is present but empty the first submission resulting in a:6 instead of a:5, then a:6 is filled on second submit:

    #FIRST _REQUEST
    a:6:{s:19:"zto_membergroup_one";s:0:"";s:19:"zto_membergroup_two";s:0:"";s:21:"zto_membergroup_three";
    s:0:"";s:20:"zto_membergroup_four";s:0:"";s:20:"zto_membergroup_five";s:0:"";i:0;N;}
    
    #SECOND _REQUEST
    a:6:{s:19:"zto_membergroup_one";s:0:"";s:19:"zto_membergroup_two";s:0:"";s:21:"zto_membergroup_three";s:0:"";
    s:20:"zto_membergroup_four";s:0:"";s:20:"zto_membergroup_five";s:0:"";i:0;a:6:{s:19:"zto_membergroup_one";s:0:"";s:19:"zto_membergroup_two";s:0:"";s:21:"zto_membergroup_three";s:0:"";s:20:
    "zto_membergroup_four";s:0:"";s:20:"zto_membergroup_five";s:0:"";i:0;s:0:"";}}

    What I am expecting is a sort of combination of the two, as in the data in the db would be an array of length 5 (not multi-dimensional), like so:

    a:5:{s:19:"zto_membergroup_one";s:0:"";s:19:"zto_membergroup_two";s:0:"";s:21:"zto_membergroup_three";s:0:"";
    s:20:"zto_membergroup_four";s:1:"1";s:20:"zto_membergroup_five";s:0:"";}

    I haven’t solved anything of course. Merely wanted to show what I’m getting. I guess the first sample (with the single key/ value pair serialized) is my goal since that’s what works. I’m not looking for an answer to give me the store the array w/ 5 members, regardless of their value. The point is to allow users to define his/ her membership in 1 or more of 5 member groups.

    RE: THE _POST / _GET Requests described in the o/p:
    As far as I can tell, these fields will never process using my conditional logic– as it is, as shown above– looking for a _POST value, because _POST is empty by the time the code gets to the update function, but I could be wrong. Despite the username, sadly I am not a master of Ajax.

    Also, while debugging, I see that the _POST values are placed in a cache array, so they aren’t just disregarded. I guess it’s progress to understand that. Though that’s a mere observation (i wouldn’t say “i understand it” yet). I regret I didn’t note the name/ settings page / line where the _POST values are stored elsewhere (maybe wp-settings): I assume that’s the WP way of handling this type of Request, but I’m not familiar enough to just get it yet.

    Thank you!

    EDIT: One last thing. I realize I could have achieved this in probably under 30 minutes using a plugin like Advanced Custom Fields (which I did, in fact), but I don’t want that. I don’t like the way ACT adds the meta key/ value pairs. I’m sure there’s a way to get it to enter then as serialized, but I don’t care. That’s not interesting or useful to me at all to use a plugin to achieve what the CMS was developed to be able to do for a person who knows PHP (even a little bit of PHP), it seems more than lazy and … i can’t think of the word I’d want to use. Just lame. haha. I don’t mean to insult anyone by saying that. I was the same way w/ guitar. That’s why I was a good musician. Investing the time to learn to do something correctly tends to return a better value.

    • This reply was modified 1 year, 6 months ago by ajaxStardust. Reason: code and illustration
    • This reply was modified 1 year, 6 months ago by ajaxStardust. Reason: alignment
    • This reply was modified 1 year, 6 months ago by ajaxStardust.
    • This reply was modified 1 year, 6 months ago by ajaxStardust.
    • This reply was modified 1 year, 6 months ago by ajaxStardust. Reason: addendum
    Thread Starter ajaxStardust

    (@ajaxstardust)

    The following is the solution to prevent the wp_usermeta.meta_value from becoming an infinitely long multi-dimensional array; to handle the issue issue i encountered with the serialized data. Note the dynamic value in the foreach() loop:

    <?php
        $user_membership = get_user_meta($user_id, 'zto_membergroup');
        $zto_helpers = new Zto_Helpers();
        $user_membership = wp_parse_args($user_membership, $zto_helpers->zto_membergroup_defaults());
        if(is_array($user_membership) && !empty($user_membership[0])){
            $default_membership = $user_membership;
            // echo '<br>var_dump(default_membership): '.var_dump($default_membership);
            $user_membership = $default_membership[0];
            $user_membership = array_merge($user_membership, $default_membership);
        }
        else {
            $user_membership = wp_parse_args($user_membership, $zto_helpers->zto_membergroup_defaults());
        }
        $membergroup = get_option('zto_membergroup');
        $zto_helpers = new Zto_Helpers();
        if ($membergroup) {
            $membergroup = wp_parse_args($membergroup, $zto_helpers->zto_membergroup_defaults());
        } else {
            $membergroup = $zto_helpers->zto_membergroup_defaults();
        }
        
        
        $array0 = $user_membership;
        if(!empty($array0[0])) {
        $array1 = array_slice($array0, 0, count($array0) - 1, true); 
    $userInputArray = array('one', 'two', 'three', 'four', 'five');
    
    // Initialize the filter key array
    $filterKeys = array();
    
    // Loop through the user input values
    foreach ($userInputArray as $uiKey => $uiVal) {
        // Add the filter key if it matches the key in the nested array
        if (isset($array0[0]["zto_membergroup_$uiVal"]) && $array0[0]["zto_membergroup_$uiVal"] === '1') {
            $filterKeys[] = "zto_membergroup_$uiVal";
        }  
    }
    
    // Get the filtered array with the dynamic keys
    $array2 = array_filter($array0[0], function($key) use ($filterKeys) {
        return in_array($key, $filterKeys);
    }, ARRAY_FILTER_USE_KEY);
     
        // Merge the two arrays
        $user_membership = array_merge($array1, $array2);
    }
     
    ?>
    • This reply was modified 1 year, 6 months ago by ajaxStardust.
Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘user-edit.php – settings registered – _POST[val] not in HTTP Request’ is closed to new replies.