7

I allow users to save some notes on my wordpress site. They can only do this after they login to the site.

Now I want to save their notes in a database table and associate them with the user's id i.e. if a user has saved 2 notes, when they login and go to "my page", they should be able to see the 2 notes they saved.

Can someone please guide me on how to do this? I could find a lot of information on saving custom user metadata when they register. But could not find any help on saving information associated with a user account after they have logged in.

Thank you.

1 Answer 1

9

Take a look at update_user_meta you can save user data if they are registering or signed in, its just a matter of what user ID you pass to it.

say in your function to save the user data after he is logged in:

function save_user_data_7231(){
    global $current_user;
    if is_user_logged_in{ //check if user is logged in.
        if (isset($_POST['Notes'])){
            // get current user info
            get_currentuserinfo();
            $old_notes = get_user_meta($current_user->ID, 'user_notes', true);
            if (isset($old_notes)&& is_array($old_notes)){
                //if we saved already more the one notes
                $old_notes[] = $_POST['Notes'];
                update_user_meta( $current_user->ID, 'user_notes', $old_notes);
            }
            if (isset($old_notes)&& !is_array($old_notes)){
                //if we saved only one note before
                $new_notes = array($old_notes,$_POST['Notes']);
                update_user_meta( $current_user->ID, 'user_notes', $new_notes)
            }
            if (!isset($old_notes)){
                //first note we are saving fr this user
                update_user_meta( $current_user->ID, 'user_notes', $_POST['Notes'])
            }
        }
    } 
}

they to display there notes you can use get_user_meta

function get_user_notes_654(){
    global $current_user;
    if is_user_logged_in{ //check if user is logged in.
        // get current user info
        get_currentuserinfo();
        $old_notes = get_user_meta($current_user->ID, 'user_notes', true);
        if (!isset($old_notes)){
            $re = 'No Notes YET!';
        }
        if (isset($old_notes)){//we have notes. Removed the extra ! here.
            if (is_array($old_notes)){//more then one
                foreach($old_notes as $note){
                    $re .= '<strong>note:</strong>' . $note . '<br />'; 
                }
            }else{//just one
                $re = '<strong>note:</strong>' . $old_notes . '<br />';
            }
        }
        re .='//add note form  would come here';
        return $re;
    }
}

Hope this Helps

4
  • thanks. what if I want to save data other than string - files for instance? Commented Feb 11, 2011 at 10:00
  • saving files would be processing the upload and saving the attachment id which can be saved as a string , also if you just save the url to the file after the upload its still a string.
    – Bainternet
    Commented Feb 11, 2011 at 10:02
  • @Bainternet i know this thread is old but i helps me. Just one question i haven't set a note and still $old_notes = get_user_meta($current_user->ID, 'user_notes', true); echo 'old notes ' . isset($old_notes); returns a 1 this should be 0/false, right?
    – alex
    Commented Sep 10, 2014 at 8:24
  • Ok i edit your code a bit if (!isset($old_notes[0])) { seems to work like i expected. $old_notes always have an array as value?
    – alex
    Commented Sep 10, 2014 at 15:36

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.