How to Stop Accessing WordPress Page When User Login - wordpress

I am trying to stop users from accessing a page when they already login. Anyone please help me how to do this. I checked it on google and all over the stake overflow but did not find any solution. Hope to get answer soon.
Thanks

try this code bellow , put it within your theme function.
function my_page_template_redirect()
{
if( is_page( 'page-slug' ) && is_user_logged_in() )
{
wp_redirect( home_url() );
exit();
}
}
add_action( 'template_redirect', 'my_page_template_redirect' );
Change page slug based on your case, you also can use page_id check these references bellow. Don't forget to save permalink after put that code
Reference :
https://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect
https://developer.wordpress.org/reference/functions/is_page/

Related

Woocommerce thankyou page is not working after checkout

Today I ran into an interesting issue. After payment thankyou page is not displayed. There is only white screen with menu. You can see it in the screenshot. thankyou page http://prntscr.com/ofpnuu
I tried to create a custom thankyou page and tried to redirect it from the function.php but nothing happened. Then I installed the plugin to create thankyou page but again nothing happened. I have run out of ideas. Can someone tell me how to solve this issue.
Use the hook below to redirect to the thank you page. Create a thank you page and redirect to that url...
Check the code below:
add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase() {
global $wp;
if ( is_checkout() && ! empty( $wp->query_vars['order-received'] ) ) {
wp_redirect( 'http://www.yoururl.com/your-page/' );
exit;
}
}

Change 'home' link in breadcrumb to specific page

I'm using the Avada theme, and trying to edit the breadcrumb Home link. It has to refer to another link. The wordpress given code isn't working. Someone got any idea?
Using original given wordpress code isn't working. The exact URL isn't doing the job, nor is the return get_permalink( $shop_page_id ).
Code below isn't working:
add_filter( 'woocommerce_breadcrumb_home_url', 'woo_custom_breadrumb_home_url' );
function woo_custom_breadrumb_home_url() {
$shop_page_id = get_option( 'woocommerce_shop_page_id' );
return get_permalink( $shop_page_id ) ;
}
Hope anyone can help me to get this done using the Avada theme?

How can I remove search result page

I made a new website, and I have a little problem. The site doesn't need any kind of search engine etc..
So I don't need the search result page.
Is there maybe a way to make searching and search page redirect to home page?
So basically I want to redirect any user trying to access this page https://example.com/?s= to home;
Thank you
You could add the code below into your themes functions.php
add_action( 'template_redirect', 'redirect_s_to_homepage' );
function redirect_s_to_homepage(){
if ( is_search() && ! empty ( $_GET['s'] ) ){
wp_safe_redirect( home_url(), 301 );
exit;
}
}
It makes sure your loading search page and that there is a search variable set, and redirects you.
EDIT
The code above will work when there is an actual search query. example: ?s=something.
Since you are also wanting to redirect if there is no query, try the snippet below.
add_action( 'template_redirect', 'redirect_s_to_homepage' );
function redirect_s_to_homepage(){
if ( is_search() && isset( $_GET['s'] ) ){
wp_safe_redirect( home_url(), 301 );
exit;
}
}
The first code was making sure ?s= wasn't empty. The edited code just makes sure ?s= is set.
We have an action hook called 'template_redirect' , for search query we can do following to achieve:
function wm_search_redirect(){
global $wp_query;
if( $wp_query->is_search) {
wp_redirect( home_url() );
die;
}
}
add_action( 'template_redirect', 'wm_search_redirect' );
Please test it on non live site, as it is untested code just for idea.

Wordpress Homepage Redirect to other page

SO I have this problem when I go to my main website, for example, www.mysite.com I want to redirect it to www.mysite.com/home I'm using this plugin 'link. it works but not always, I don't understand why so then I wrote a little script in the header file
$GetURL = (isset($_SERVER['HTTPS']) ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"
if ($GetURL === 'www.mywebsite.com') {
header('Location: www.mywebsite.com/home');
}
But still, it doesn't redirect. what could be a problem?
You can set your page as Front Page is Setting -> Reading or
add_action('template_redirect', 'function(){
if(is_front_page()){
wp_redirect(get_permalink('your_page_id'), 301)
}
});
Maxim's answer helped me redirecting the homepage to a custom post type archive, but it contains two typos. First, there must not be an apostrophe before "function"; second, the wp_redirect command needs to be closed with a semicolon.
This is a version of the code which, put into functions.php, points the front page to the archive of the post type 'project':
add_action(
'template_redirect',
function( $post_type ){
$post_type = 'project';
if ( is_front_page() ){
wp_redirect( get_post_type_archive_link( $post_type ) );
}
}
);
Obviously, you can use get_permalink() to redirect to a static page as well.

Require WordPress Login only for posts, not pages?

I'm trying to create a WordPress site with a login system, but I only want the login to be required to view POSTS. They should be able to view pages without logging in. I did some research, but no one had exactly what I was looking for, so I tried to puzzle something together, and this is what I came up with, but it doesn't work.
function my_force_login() {
global $post;
if ( ( is_single() && !is_user_logged_in() ) ){
auth_redirect();
}
}
Any ideas?
add it to your single.php
<?php auth_redirect(); ?>
This function will check if the user is logged in and if he is not it will redirect him to the login page http://codex.wordpress.org/Function_Reference/auth_redirect
Your code works, but it has run on the correct point:
add_action( 'template_redirect', function()
{
global $post; // Use this to detect the custom post types if needed
if ( ( is_single() && !is_user_logged_in() ) )
{
auth_redirect();
}
});
Details of the post is being displayed in the single.php. Please put the code of displaying post's content into the condition. Then you will get the solution. Try the following code into the single.php in the twentyforteen theme.
if ( is_user_logged_in() ) {
get_template_part( 'content', 'single' );
}
Now get logged out and refresh the browser, you can see none of content are shown.

Resources