Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 5 down vote accepted

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

share|improve this answer
    
thanks. what if I want to save data other than string - files for instance? –  lostInTransit Feb 11 '11 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 Feb 11 '11 at 10:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.