0

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 .=     
 $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
 } else {
  $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
 }
 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.

2

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(
        array(
            '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 '14 at 11:11
  • Running which function? The adding of the bookmark or the listing of the bookmarks? – TheDeadMedic May 7 '14 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 '14 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. – TheDeadMedic May 7 '14 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 '14 at 12:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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