I am working on function that enables registered users on my site to bookmark posts. I have searched for plugins but none seemed to do the job properly.

I have a page where users can see their info like name, email, password and so on. I want a list of posts they have bookmarked. This should be done by clicking on a link in the post, which then saves the url or a link to the post in the database.

The code for getting the url of the post:

function curPageURL() {
$pageURL = 'http';
 if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
  $pageURL .= "://";
 if ($_SERVER["SERVER_PORT"] != "80") {
 $pageURL .=     
 } else {
 return $pageURL;

Now i have the url in a variable. But how i can store this url in a field for the current user?

I was thinking about inserting a field in user table. But I'm not quite sure how and if this is the best way to go about.

1 Answer 1


Personally, I would store the post IDs (as opposed to URLs). Something like:

function wpse_143643_add_user_bookmark() {
    if ( ! empty( $_GET['bookmark'] ) && is_singular() && is_user_logged_in() ) {
        if ( ! $bookmarks = get_user_meta( $user_id = get_current_user_id(), 'bookmarks', true ) )
            $bookmarks = array();

        $bookmarks[] = get_queried_object_id();
        $bookmarks = array_unique( $bookmarks );

        update_user_meta( $user_id, 'bookmarks', $bookmarks );

add_action( 'template_redirect', 'wpse_143643_add_user_bookmark' );

So a link like http://example.com/my-post/?bookmark=true will add my-post to the current user's bookmarks.

And to display the list of bookmarks:

if ( $bookmarks = get_user_meta( $user_id = get_current_user_id(), 'bookmarks', true ) ) {
    $posts = get_posts(
            'post__in' => $bookmarks,

    $list = '<ul class="bookmarks">';
    foreach ( $posts as $_post )
        $list .= '<li><a href="' . get_permalink( $_post->ID ) . '">' . get_the_title( $_post->ID ) . '</a></li>';
    $list .= '</ul>';

    echo $list;
  • Good point, haven't even thought about using the post id. This makes more sense. How would I go about running this function when a user clicks on a link?
    – TietjeDK
    May 7, 2014 at 11:11
  • Running which function? The adding of the bookmark or the listing of the bookmarks? May 7, 2014 at 11:19
  • Sorry i didn't make myself clear. The adding of the bookmark, e.g. when the user clicks on "Bookmark this" link/button.
    – TietjeDK
    May 7, 2014 at 11:26
  • That's the first code snippet. I would wrap it in a function and hook it to the template_redirect action. See my revision. May 7, 2014 at 11:28
  • Hmm i must have missed something. When i use http://example.com/my-post/?bookmark=true and try to display it nothing shows.
    – TietjeDK
    May 7, 2014 at 12:11

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.