0

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');

2 Answers 2

2

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');
5
  • Thx your answer but i don't work with your code ?
    – Olivier
    Mar 19, 2021 at 12:46
  • What doesn't work? What happens when you add the shortcode? Mar 19, 2021 at 15:44
  • Ok Nour Edin Al-Habal , he was missing " ) " if (isset($_POST['validate_user']) )
    – Olivier
    Mar 21, 2021 at 10:49
  • Oh sorry, I updated it in the answer. Did that solve your problem? Mar 21, 2021 at 12:32
  • Thx Yes Nour Edin
    – Olivier
    Mar 21, 2021 at 12:50
0

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');
10
  • You're welcome but your code is incorrect! First, if you want the update meta function to run only when the button is clicked, then you have to put it inside if(isset($_POST['validate_user'])) Mar 21, 2021 at 12:55
  • I didn't realize this is a question. Yes you can update multiple meta by calling update_user_meta() for each meta you want to update. You need to modify the code a little bit. I modified your code here. You may update your answer. Mar 21, 2021 at 13:44
  • Code Don't work .. website down
    – Olivier
    Mar 21, 2021 at 14:24
  • My bad, the last return statement has to be inside if( is_user_logged_in() ). Mar 21, 2021 at 15:33
  • don't work again down .. sorry Nour
    – Olivier
    Mar 21, 2021 at 15:39

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.