Redirect User based on page template used - php

i have wordpress blog. I use different templates for blog posts and medical cases. I installed plugin for creating custom templates per post and it do their job. But now want medical cases to be available just for logged in users.
Page that i want to manage is using my custom template:
So i search in google for function that will limit access by template and by logged in status. And writen this function:
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( is_page_template( $template = 'templates/clinic_case.php' ) && is_single() && ! is_user_logged_in() ) {
wp_redirect( 'url/wp-login.php', 301 );
exit;
}
}
but when try to load, content is loading just fine, and should not. Any help?

My guess is that by the time Wordpress processes your code, it has already passed the template_redirect hook.

Related

Redirect not - logged in users to custom login page

I'm currently building my own website using wordpress.
I designed my own login - page and want users to be redirected to that page if they are not logged in.
There are several solutions to this problem, however, none of them seem to work.
I tried:
Adding code to the functions.php, such as:
<?php
function admin_redirect() {
if ( ! is_user_logged_in() ) {
wp_redirect( home_url( '/{custom page goes here}/' ) );
exit;
}
}
add_action( 'get_header', 'admin_redirect' );
It apparently didn't work - I also tried some other code like (to my header.php):
<?php
if ( ! is_user_logged_in() ) {
wp_redirect('http://somepagehere');
exit;
}
Use of plugins - I tried using "Force Login" and "WPS Hide Login".
First of all, installing "Force Login" didn't work when accessing the main page and only prohibited access when adding a slug to the main page (ex.: oho.nachhilfe.de/xyz). It also didn't redirect to the login page, but instead simply redirected to a 404 page (no matter what my login URL was).
Changing my login URL to a custom one using "WPS Hide Login" also didn't work, as this only replaced my custom login page with the default wordpress login page, therefore, my custom page simply got overwritten with the wordpress one.
Any ideas how to solve the issue?
Here's the page I need help with: https://www.oho-nachhilfe.de
First of all, let's all agree that the login customization handling in WordPress is awful.
Like Chris said, forward compatibility and messing with core don't go well together.
If it's just a permalink question you can always customize them instead of re-creating the whole login system.
Moving on. Both of your spinets are perfectly working, tho get_header is the wrong hook to use. Redirects needs to happen before determining which template to load. which is happening before get_header but after init and wp.
You can use, template_redirect do do just that.
Fires before determining which template to load.
<?php
add_action( 'template_redirect', function () {
if ( ! is_user_logged_in() && ! is_page( 'login' ) ) {
wp_safe_redirect( home_url( '/login/' ) );
exit;
};
} );
You can refer to the WP Plugin API/Action Reference to have a better understanding of the hook firing sequence.

wordpress code snippet php restricted pages access

I'm working on an existing wordpress project and i'm using ultimate member plugin. I think that to restrict access to pages if user is logged or not we have to pay for additional modules. So i tried php code snippets. I tried to use XYZ PHP code snippet but the snippet code is not working although it's active. I think i have to add the snippet created somewhere but don't know where plus the wordpress is in french. Can i go directly to a page edit and add snippet here is the code snippet :
<?php
if ( !is_user_logged_in() ) {
wp_redirect( 'https://xxxxir.com/register' );
//auth_redirect();
}
xyz-ips snippet="redirection"
Thanks.
If you want to restrict specific page you can do it this way.
Page that you want to restrict should have slug such as xxx.
// Put these codes in function.php
// xxx is slug of your page
add_action('wp_head', function(){
global $post;
$post_slug = $post->post_name;
if ( $post_slug == 'xxx' && !is_user_logged_in() ) {
wp_redirect( 'https://xxxxir.com/register' );
}
});

How To Restrict Access To Custom WordPress Template

I have a custom template that I built which manages a separate table inside a WordPress database. I have had success restricting access unless logged in on all other pages (including existing custom templates) accept my custom template (page id 9597) using this code:
add_action( 'template_redirect', 'add_restrict_access');
function add_restrict_access(){
if( ! is_user_logged_in() && is_page( 9597 ) ) {
wp_redirect( '/wp-login.php' );
exit;
}
}
If I change the is_page number to any other page ID, the redirect works. Anyone have any ideas?
SSL cert issue. Page was viewable without SSL, as soon as I added https it started working properly. Weird.

How do I redirect from BuddyPress component for non logged in user

I have a specific use case where I want to show non logged in users BuddyPress Members and Groups in the sidebar of a public page, but when a user clicks on a member or group (or any BP component) I want to redirect them to the Register page.
I have been successful with the below code in my functions.php file to redirect based on the slug of the /members and /groups page, however I cannot figure out how to make it work either for /groups/group-a or members/member-a, etc or better yet for all BuddyPress components.
The end goal is that when a non logged in user navigates to any BuddyPress component they are redirected to my register page.
//custom redirect for non logged in users
add_action( 'template_redirect', 'redirect_to_specific_page' );
function redirect_to_specific_page() {
if ( is_page( array( 'groups', 'members') ) && ! is_user_logged_in() ) {
wp_redirect( 'http:mysite.com/register', 301 );
exit;
}
If you're talking about the BP widgets for members and groups, then the issue is the while loop for those widgets. Take a look at them and how they include links in the loop.
The best solution is to write custom widgets that use loops that use a conditional to create a link for the member or group. And then you don't need your template_redirect function.

How would you enqueue scripts in wordpress to show only on single posts & blog pages?

How would you enqueue a script in a theme that you only want to show on the blog and single posts?
I checked other questions here but didn't get convincing answer.
I got the following code from Wordpress site from a question as :
function enqueue_files() {
if ( is_page( 'your-page' ) ) {
// enqueue specific page script files here
} else {
// enqueue common scripts here
}
}
add_action( 'wp_enqueue_scripts', 'enqueue_files' );
So after taking a look at is_page() function I am confused that as I need to only show them at single posts & blog pages and while the following function would work only for static pages and since I want it to be dynamic for all of single posts pages and blog pages so how would I be able exactly to do that then with which function?
Use is_singular. It combines is_page() with is_single().
Linky.
It'll also activate on attachment pages though. If that's a problem for you, just use is_page() || is_single().
You don't need to pass the page/post slug (and it'll actually break what you are trying to accomplish. So you just do:
if ( is_page() || is_single() ) {
// if ( is_singular() ) { // or this if you prefer. :)
// enqueue specific page script files here
}
If you want to detect the blogroll, use is_home, I thought you were only targeting single posts.
Link.
Enqueue specific scripts only for the blog homepage and single posts of post type post:
function enqueue_files() {
if ( is_singular('post') || is_home() ) {
// enqueue specific scripts for blog homepage and single posts of post type post
} else {
// enqueue common scripts here
}
}
add_action( 'wp_enqueue_scripts', 'enqueue_files' );
Explanation
is_singular('post') checks if a singular post of specified post type post is being displayed (thanks #Umair Shah Yousafzai for this hint)
is_home() determines if the query is for the blog homepage

Categories