I am working on a site where I am using a plugin called Favorite Posts so logged-in users can add a post (a custom post type) to their favorites.
I created a page called my-favorites
which allows the logged-in user to see the posts he has favorited.
Now I need to create a page which allows the another user or visitor to check this user’s favorites.
So I need to be able to do three things:
-
Get a function related to author that returns a link like
example.com/user/username
which I can append to /favorites to get
the link –example.com/user/username/favorites
(the_author_link()
andget_the_author()
link seems to link to the website andthe_author_posts_url()
outputs the a tag with the name. any function to just get the url or maybe modify author to show user instead?) -
Get WordPress to use a template called
favorites.php
to display this
page. (If the structure matches, then how do I tell WordPress which template to use?) -
Get the user id of the user in order to show his/her favorites to
the visitor. Right now, I use the[favorite-post]
shortcode in my
template file and can pass a parameter called[favorite-post user_id = ""]
Will it automatically get the user id if the page is branched out from /author?
I found this article but it still deals with custom post types and query_vars
which I am not using. How do I make it work for the favorites page? Most other similar questions asked here were solved using a slug for the custom post type or taxonomy.
First of all, you ned a function (template tag) to display the url. You can write a custom function that output the url in the format you want, however, putting the username in a public url can be a security issue.
The function can handle the link and accept an argument
$user
that can be a user id or a user object. If non is passed, the function will try to use current post author (if in the loop).Now you have to make WordPress understand this kind of urls. You have to use a rewrite rule for the purpose.
You said in OP that you are not using query vars, but you should. The problem is that once you want to use a pretty permalink, you have to use a rewrite rule, and any parameter added via a rewrite rule is only accessible via query var and not via
$_GET
.So:
After adding this code, go in your backend, visit the page “Settings -> Permalinks” to flush rewite rules.
Now you have an url format, and when you visit that url WordPress will set a query variable ‘favorites’ filling it with the required username.
However, we wont WordPress load the ‘favorites’ page when we call that url.
What I suggest you is to create a custom page template, and assign the page “My favorites” to it.
So create a new php file, name it
'page-favorites.php'
, and inside it write only this:Nothing else.
Now, in the backend, create a page (if you don’t have it already), and assign to it the template just created.
After that, we can use ‘pre_get_posts’ to query this page when the user favorites url is used.
With this code, we tell WordPress to call first page with the template
'page-favorites.php'
when the favorites url is required.The function
user_favorites_get_user
is an helper function we use to save the required user, avoiding the use of global variables.The function
user_favorites_template
force WordPress to load ‘favorites.php’ as template.Inside
'favorites.php'
the relevant part is to output the shorcode for user favorites posts.There are 2 possibilities: an user is logged and want to see own favorites posts, or someone (logged in or not) want to see the posts favorited by an user.
That can be translated in code like this:
Now, when a user is logged in, he/she can visit the url
example.com/my-favorites
(if ‘my-favorites’ is the slug of the page assigned to “My Favorites” page template) to see his/her own posts.Any logged or not logged visitor, can use an url like
example.com/user/username/favorites
and vieving the favorites post for that specific user.