WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Updating other users metadata (8 posts)

  1. Timothy Jacobs
    Member
    Posted 1 year ago #

    I have created a custom user meta field, and that value is being used to get data from an external API and display it on the user profile page. On admin pages I have used the following code to make sure that when an administrator account views a user profile, s/he can view the data displayed from the API.

    And then calling the API, and retrieving the cached data from a transient prefixed by the user id yadadada. This all works perfectly, and the data displays properly. But if the administrator then tries to update another user's account custom meta field despite the fact that I pass the correct userid to the update_user_meta function, meta value does not update. Below is the code for the update function.

    function af_askFRED_update_fencer_field() {
        $user = wp_get_current_user();
        $userid = $user->ID;
    
        if (current_user_can('delete_users') && isset($_GET['user_id'])) { // check if user is admin, if so get user id from the url
            $userid = $_GET['user_id'];
        }
    
        $usfaID = $_POST['af_askFRED_usfaID'];
        update_user_meta($userid, 'af_askFRED_usfaID', $usfaID);
        delete_transient($userid.'AF_USFA_KEY');
    
    }
    
    add_action('personal_options_update', 'af_askFRED_update_fencer_field');

    NOTE: If I update any other profile field it works perfectly.

  2. Timothy Jacobs
    Member
    Posted 1 year ago #

    I stumbled across this action:

    add_action('edit_user_profile_update', 'af_askFRED_update_fencer_field');

    and I tried using that as well, but I still can't get it to work.

    Any help would be fantastic!

  3. bcworkz
    Member
    Posted 1 year ago #

    You are getting one value as if the request is a GET and another value as if the request is a POST. It needs to be one or the other. I also don't see why you are hooking into an action, it seems to me the update can happen on request without depending on some other event. I obviously don't have the whole picture, so hooking an action may be OK, but it sounds like you are unsure of which action to use, and it doesn't seem like you need to use one at all.

  4. Timothy Jacobs
    Member
    Posted 1 year ago #

    I have been religiously using the WROX wordpress plugin book, and they say to use an action, as well as the codex. I tried removing the actions, but that doesn't work. I am using GET to get the user id from the URL: http://127.0.0.1:8080/wordpress/wp-admin/user-edit.php?user_id=2. The GET method gets "2" as a user ID whenever the current user can delete users. I know that that part of the function works because it can properly fetch the transient value that is prefixed by the user id.

    I am using POST to get the data that is submitted from a form on the user profile. Here is the entire function:

    function af_askFRED_display_fencer_field() {
        if(current_user_can('view_tournaments') || current_user_can('delete_users')) {
            $user = wp_get_current_user();
            $userid = $user->ID;
    
            if (current_user_can('delete_users') && isset($_GET['user_id'])) {
                $userid = $_GET['user_id'];
            }
    
            $usfaID = absint(get_user_meta($userid, 'af_askFRED_usfaID', true));
    
            if(current_user_can('view_tournaments') == false){?>
    
            <tr>
                <th><b>Fencer Information</b></th>
            </tr>   <?php } ?>
        <tr>
            <th scope="row">USFA ID</th>
            <td>
                <form>
                    <?php echo "<input id='af_askFRED_usfaID' name='af_askFRED_usfaID' type='text' value='$usfaID' />";?>
                </form>
            </td>
        </tr>
            <?php
            af_askFRED_personal_info_table_backend_profile();
        }
    }
    
    // Trigger this function on 'personal_options' action
    add_action('personal_options', 'af_askFRED_display_fencer_field');
    
    // Monitor form submits and update user's setting if applicable
    function af_askFRED_update_fencer_field() {
        $user = wp_get_current_user();
        $userid = $user->ID;
    
        if (current_user_can('delete_users') && isset($_GET['user_id'])) {
            $userid = $_GET['user_id'];
        }
    
        $usfaID = absint($_POST['af_askFRED_usfaID']);
        $usfaID = substr($usfaID, 0, 9);
    
        update_user_meta($userid, 'af_askFRED_usfaID', $usfaID);
        delete_transient($userid.'AF_USFA_KEY');
    }
    
    add_action('personal_options_update', 'af_askFRED_update_fencer_field');
    add_action('edit_user_profile_update', 'af_askFRED_update_fencer_field');
  5. bcworkz
    Member
    Posted 1 year ago #

    It's hard to say without knowing the context that initiates the actions. I see that a parameter passed in the URL and retrieved with $_GET is probably correct for the first function. I still don't think it will work in the second function. It is possible to pass a parameter in a form's action URL, but highly unusual. A POST request is a new request, any values from a previous requests do not exist.

    Your form element has no submit means, so unless it's triggered by javascript, I don't see how it can be submitted. Since you have not specified an action, it gets sent to the page initiating the action, without URL parameters. Since you have not specified a method, it defaults to GET, so there will be no values in $_POST, just the one input field value in $_GET, and no WP userid is sent.

    It is always best practice to use action and filter hooks to extend WordPress whenever possible. There are situations where it's not possible, but this isn't sounding like one of them, but do ensure these actions are actually being called as part of your debugging efforts.

  6. Timothy Jacobs
    Member
    Posted 1 year ago #

    I am hooking into the wordpress profile pages, so that handles the submission of the form for me, and passes the data into the into the _POST global variable. By using the personal options update action, it monitors for a form submission and then I get the data from that input and pass it into the user meta data.I am just adding additional inputs to the form.

    I know that this actually works because my form works fine when the admin is editing his/her own user profile, or when a user is editing his/her profile. So I know that the method I am using to get it works. Only it does not work when the admin is editing another users profile.

  7. bcworkz
    Member
    Posted 1 year ago #

    Ah, it is suddenly clear. Sorry for being rather thick. Thank you for your patience. The $_GET['user_id'] will not work in the second function, only the first. The form submit, as usual, sends no URL parameters. It works for admin because the id is left over from the previous statement. The form's user id field name is also user_id, so simply change GET to POST and all should be fine. Cheers.

    (btw, you don't need the form tags in your first function, they are ignored by the browser, being inside the other form, but were confusing me)

  8. Timothy Jacobs
    Member
    Posted 1 year ago #

    Thank you so much, I never would have thought of that. I have to thank you for being patient with me :)

Topic Closed

This topic has been closed to new replies.

About this Topic