Require WordPress Login only for posts, not pages? - wordpress

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.

Related

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.

hide wordpress posts and categories for not logged in users

Basically I handled this issue by overriding parent theme files in child theme.
i.e., by using
if( is_user_logged_in() ){
...................
wordpress loop
}
I had to do this for every template ( wherever there is wordpress loop )
Though I didn't have to do this for displaying sidebars as if there was category in widget and whenever user clicks on it to view it, it would automatically say you don't have proper privileges to view this content.
So, my question is, is there any better way to hide wordpress content ( this may be anything, like normal posts, custom post types,.. ) from just visitors.
function bt_hide_from_guestes( $content ) {
global $post;
if ( $post->post_type == 'post' ) {
if ( !is_user_logged_in() ) {
$content = 'Please login to view this post';
}
}
return $content;
}
add_filter( 'the_content', 'bt_hide_from_guestes' );
Above code didn't help
It looks like your filter function should work for you. Try setting a priority and the "accepted arg" number:
add_filter( 'the_content', 'bt_hide_from_guestes', 10, 1 );

How to Stop Accessing WordPress Page When User Login

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/

redirect specific page category if not logged in

I'm sure this was working but now it's not. I added categories to pages with:
function add_categories_to_pages() {
register_taxonomy_for_object_type( 'category', 'page' );
}
add_action( 'init', 'add_categories_to_pages' );
then tagged a range of pages with a 'client' category and added to functions:
add_action( 'template_redirect', 'client_redirect_to_login' );
function client_redirect_to_login() {
$category_slug = 'client';
global $pages;
if ( ! is_user_logged_in() && in_category( $category_slug, $pages ) ) {
wp_redirect( site_url( '/login' ) );
exit();
}
}
the intention, simply enough, is that not logged in users trying to directly access any of the 'client' pages is redirected to my custom /login page. being returned directly to the page they were trying to access would be a bonus but this, THIS, used to work. there's a dozen pages and growing, so restricting by category is easier than by page ID array - but i can't see what i'm doing wrong.
all advice greatly appreciated!
From global variables pages in codex $pages will return:
The content of the pages of the current post. Each page elements contains part of the content separated by the <!--nextpage--> tag.
And you need either post/page ID or object when using in_category() function as a second argument, as noted here.
You can try to get the category of the visited page by setting:
$current_cat = get_the_category();
And then check
in_category( $category_slug, $current_cat );
inside the conditional.

Woocommerce: how to jump directly to product page when category only holds one item

I have a top menu which permits the display of product categories.
In this case, the name of the application I am selling.
When this menu item is clicked, it shows the contents of the category - as it should do.
However, as this category only contains one item, I want to jump straight to the product page, instead of displaying a category page with one item.
Here is a link to the page in question : boutique.zimrahapp.com/categorie-produit/app/
I have not been able to find either a hook or a template where I can adjust the output or do a redirect.
Has this kind of thing already been done ?
The above code didn't work for me. I have a solution that works, but it redirects all single archive results. In my case I wanted to also redirect single tags.
/* Redirect if there is only one product in the category or tag, or anywhere... */
function redirect_to_single_post(){
global $wp_query;
if( is_archive() && $wp_query->post_count == 1 ){
the_post();
$post_url = get_permalink();
wp_safe_redirect($post_url , 302 );
exit;
}
}
add_action('template_redirect', 'redirect_to_single_post');
I had asked this same question here: Woocommerce: How to automatically redirect to the single product if there is only one product on a category page?
WooCommerce redirects a search query with only one result to that result. You can see how they are doing it here.
Modifying their code you get something like this:
function so_35012094_template_redirect() {
global $wp_query;
// Redirect to the product page if we have a single product
if ( is_product_category() && 1 === $wp_query->found_posts ) {
$product = wc_get_product( $wp_query->post );
if ( $product && $product->is_visible() ) {
wp_safe_redirect( get_permalink( $product->id ), 302 );
exit;
}
}
}
add_action( 'template_redirect', 'so_35012094_template_redirect' );
Untested, so watch out for copy/paste fails. Always use WP_DEBUG so you can figure out what went wrong.

Resources