I want ONLY users who are logged in to view my WordPress site and the code I'm using is:
add_action('template_redirect', 'admin_redirect');
function admin_redirect() {
if ( !is_user_logged_in()) {
auth_redirect();
}
}
PROMLEM: if I send a hyperlink every time the user clicks it they are asked to sign in again.
Can you not just put
auth_redirect();
into your functions.php file?
This should redirect users who aren't logged in to the log in page but remember which page they were trying to access. Then when they log in it should redirect them to their destination page.
Is this not working?
From my understanding of auth_redirect(), you shouldn't have to place that check around it. The function actually handles that check itself as shown in the Codex
Also, I'd recommend moving this function into your header. By simply calling auth_redirect() in your header (which should be called on each and every page anyways), you can check if the user is logged in. If not, they should be bounced to the wp-login page.
Edit:
add_action( 'template_redirect', function() {
is_user_logged_in() || auth_redirect();
});
The folks over at the WordPress Exchange are much better with this sort of stuff. Found this link here.
Related
I have created a website whereby users register and create their own templated profile pages. The profile pages are automatically created as Custom Posts upon registration, with the user being set as the Author of their specific post (profile).
(Users can only ever have one Custom Post)
I want to redirect a "dumb" URL like www.website.com/my-profile to a user's custom post when they are logged in.
For example, when John Smith visits www.website.com/my-profile he is directed to his profile page: www.website.com/users/john.smith
I have found many PHP solutions going the other way, but I can't seem to find a solution that does what I need. Any help would be greatly appreciated. Thanks!
This may not be the correct answer to the original query, but proved to be a solid workaround:
Instead of redirecting www.website.com/my-profile to www.website.com/users/john.smith every time it is entered in the URL bar, I created a shortcode that could be used when needed throughout the site.
add_shortcode('bt_redirect_user_link', 'bt_redirect_user_link');
function bt_redirect_user_link ($atts) {
// check if user is logged in
if (is_user_logged_in()) {
// get current user object
$current_user = wp_get_current_user();
// get user nickname
$user_nickname = $current_user->data->user_nicename;
// set the link href
$link_href = '/users/' . $user_nickname;
// output the link html
return $link_href;
}
}
Fortunately for me, the www.website.com/my-profile link (which needs to be redirected) is only available on buttons/icons visible to logged in users. This may not be a fully workable solution for websites that need to display the link to logged out users, and I assume IF/ELSE statements would needed to be added in those cases.
At the moment if the user navigates to the default logout page it looks like this:
This is not consistent with the theme of my website so I would like this content inside my own custom page. Can this be done?
I have a plugin installed My Theme Plugin which is designed to let me specify a logout page but I do not know how to construct it.
Since this may help others, I'm adding my comments as an answer.
You need to initially use the logout_url filter - https://developer.wordpress.org/reference/hooks/logout_url/
This will allow you to set up a page when a user clicks on the logout link. Next, you simply create the page however you need to (basic WordPress page, special template, etc.).
On that page you would use wp_logout_url() to set the link for the Are you sure you want to logout text. e.g.
Logout
This would redirect the user to the home page after they've logged out.
Edit: shortcode to add to content:
function wpso58817718_add_logout_link( $atts ){
return 'Logout';
}
add_shortcode( 'logout_link', 'wpso58817718_add_logout_link' );
Then you can do [logout_link]
You'll have to update the end URL wp_logout_url( home_url() ) if you don't want it to go to the home page.
You can use below code to redirect user to your specific url after logout
add_action('wp_logout','ps_redirect_after_logout');
function ps_redirect_after_logout(){
wp_redirect( 'your url here' );
exit();
}
I am using Woocommerce wordpress plugin in my site. When I am in the Cart page and click "proceed to checkout" I am now going to checkout page. I want to change this flow as below.
If the user is not logged in, he should be taken to a different url. If user is logged in he will go to checkout page as usual.
Any help is appreciated.
First you create a page where you want to redirect the guest user, for example register
then write this code in your functions.php
function restrict_user() {
if (! is_user_logged_in() && (is_checkout())) {
wp_redirect("http://your site url.com/register/");
exit;
}
}
add_action('template_redirect', 'restrict_user');
Hope this will help you...
I am using "profile builder pro" plugin of wordpress in wordpress website and it works well except for "redirection after successfull registration" as it is redirecting to the same page.
here is a live link of it
http://www.selfmadesounds.com/dev3/register
Login redirection and register redirection i have set from backend and it is working for login but not for registration.
Any help will be pretty much accepted.
I dont have the pro version but had the same problem, i fixed it by hardcoding the redirect link.
Go to the page "wp-content/plugins/profile-builder/front-end/wppb.register.php"
Move to line 1026 and add the following:
$redirectLink = 'http://www.yourdomain.com/REDIRECT-PAGE';
Just simply overwrite the redirectLink before it go's and redirect.
Hope it helps
Please don't edit plugin files directly! Really bad practice.
You can solve this in a couple of ways, one way is to check if logged in and on a specific page then redirect to another page. There are nicer ways then hardcoding the urls and id, but this is better then editing plugin files directly.
Put this in your functions.php, change url and id to the one you need:
function isLoginPage() {
global $post;
return is_object($post) && (int) $post->ID === 1;
}
add_action('wp', 'redirectFromLoginpage');
function redirectFromLoginpage() {
if (isLoginPage()) {
global $wppb_login;
if (is_user_logged_in() || isset($wppb_login->ID)) { // Already logged in
wp_redirect(site_url() . '/redirect-to-this-url/');
die;
}
}
}
Basically like cragslist. once you select city on craigslist, nexttime when you go to the site, it redirects you to the city you selected.
What I want to achieve: When a person comes to the site and selects a particular category, the next time they come to the site (returning user) - the page will open up on that category section.
I would think this would be fairly easy to do via setting a cookie when the visitor clicks on the category link (or when the category page loads). When they return the following time, the cookie is read and then the page redirects accordingly.
Unfortunately my knowledge of PHP and cookies is limited, (hence my search for answers) so I need to ask if anyone can help me out!
Anyone have any ideas?
Thanks!
markratledge has a good link, but WordPress has a built in function to redirect users and pass an http status code as well as it's own preferred method of settings cookies.
wp_redirect() Function
Reference
Setting Cookies in WordPress via MTSTS
Give this a shot. I'm not too sure if it works, because I can't test it out right now, but it should point you in the right direction.
function user_cat()
{
//Check to see if our cookie is set
if(isset($_COOKIE['visitorhome']))
{
//Redirect to the link defined in the cookie
wp_redirect($_COOKIE['visitorhome'], 302);
}
else
{
//If it's a category page than get the current URL set the cookie with it.
if(is_category())
{
$user_cat = get_permalink();
setcookie("visitorhome", $user_cat, time()+86400, "/", str_replace('http://www','',get_bloginfo('url')) );
}
}
}
add_action('init', 'user_cat');
Read this article on how to set and get cookies with PHP: http://www.w3schools.com/PHP/php_cookies.asp
Then read this page on PHP header redirecting: http://php.net/manual/en/function.header.php
Put the two together, and you can direct users anywhere!
Good luck.