Shortcode to update user meta

I want to create a button on the front end and when user click, the value “user-meta” change ‘validate’

function func_change_validate() {
    if (is_user_logged_in()) {
        $current_user = wp_get_current_user();  
        $new_value = 'validate';
        $updated = update_user_meta( $user_id, 'User_meta_change', $new_value );            
        return 'here i want create bootom to updated ?? <button type="submit">Validate</button>';
    }
} 
add_shortcode('change_validate','func_change_validate');

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Basically, you can’t show the button and update the meta at the same moment. This has to be done in two separate requests as follows:

  1. Show the button whereever you want. It needs to be a form that submits to the same page (or an ajax call to another URL, but let’s keep it simple for now).
  2. Read the value posted from the form.

Here is a simple implementation to make this work, but it can be way improved.

function wpses_385303_change_validate() {
    if (is_user_logged_in()) {
        $user_id = get_current_user_id();
        
        //If the form was posted (ie. the button was clicked) in a previous request
        if (isset($_POST['validate_user'])) {
            if ($_POST['validate_user'] == $user_id) {//A little bit of security
                if (update_user_meta( $user_id, 'User_meta_change', 'validated' )) {
                    return "<div class='user_updated'>Updated!</div>";
                } else {
                    return "<div class='user_updated error'>Not Updated!</div>";
                }
            }
        }
        
        //Show the form
        return "<form method='post'>
            <input type='hidden' name='validate_user' value='$user_id' />
            <input type='submit' value='Validate' />
            </form>";
        
    }
} 
add_shortcode('change_validate','wpses_385303_change_validate');

Method 2

Here is the code modified to update multiple meta fields:

    function wpses_385303_change_validate() {
        if (is_user_logged_in()) {
            $user_id = get_current_user_id();
            $metas = array( 
            '_nickname_validated',
            '_first_name_validated', 
            '_last_name_validated',
            );


            //If the form was posted (ie. the button was clicked) in a previous request
            if (isset($_POST['validate_user'])) {
                if ($_POST['validate_user'] == $user_id) {//A little bit of security
                    foreach($metas as $my_meta) {
                        update_user_meta( $user_id, $my_meta, 'validated' );
                    }

                    return "<div class='user_updated'>Updated!</div>";
                }
            }
        
            //Show the form
            return "<form method='post'>
                <input type='hidden' name='validate_user' value='$user_id' />
                <input type='submit' value='Validate' />
                </form>";

        }
    }
    add_shortcode('change_validate','wpses_385303_change_validate');


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x