就个人而言,我会存储帖子ID(而不是URL)。类似于:
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\' );
这样的链接
http://example.com/my-post/?bookmark=true
将添加
my-post
到当前用户的书签。
并显示书签列表:
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;
}