Restricting user access to a particular page - Wordpress - php

I've got this brilliant piece of code that restricts access to the wp-admin login page unless the user logged in is an admin to the site:
add_action( 'init', 'blockusers_init' );
function blockusers_init() {
if ( is_admin() && ! current_user_can( 'administrator' ) &&
! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
wp_redirect( home_url() );
exit;
}
}
I want to add another function that restricts access and performs the same redirect to my home page BUT for a specific page of my site.
But this isn't working:
add_action( 'init', 'blockusers_init' );
function blockusers_init() {
if ( is_admin() && ! current_user_can( 'administrator' ) &&
! ( defined( 'mysite.co.uk/shop' ) && DOING_AJAX ) ) {
wp_redirect( home_url() );
exit;
}
}
And I'm not surpised by that but I was hoping somebody had to correct code to perform this function?
Thanks

You can add this to your header.php file to check if they're on the page and if they're an admin or not
if(is_page(PAGE_ID)){
current_user_can( 'manage_options' ){
---do redirect here or whatever else---
}
}
You can also create load the function via the init action like you did.

Related

WordPress restricted page redirect for non logged users to login page with PHP

I would like to redirect a visitor of a WordPress website to a login page when they are not logged in and on a certain page. I have the following PHP, which doesn't seem to work. Does anyone know what I am doing wrong?
add_action( 'admin_init', 'my_redirect_if_user_not_logged_in' );
function my_redirect_if_user_not_logged_in() {
if ( ! is_user_logged_in() && is_page( '3220' ) ) {
wp_redirect( 'https://www.loginpage.com ' );
exit;
}
}
template_redirect is the right hook to handle redirection.
Here is updated code:
add_action( 'template_redirect', 'my_redirect_if_user_not_logged_in' );
function my_redirect_if_user_not_logged_in() {
if ( ! is_user_logged_in() && is_page( 3220 ) ) {
wp_redirect( 'https://www.loginpage.com ' );
exit;
}
}

How to block admin area unless username is X (not user role is) in WordPress?

I have a site where I want some users to have the role type of admin but still not be able to access the admin area (don't ask!) This is more of a temporary fix whilst my new site is being built.
I am using this code at the moment which blocks everyone to the admin area unless role type is admin - but how can I block admin unless username is 'mack' for example.
add_action( 'init', 'blockusers_init' );
function blockusers_init() {
if ( is_admin() && ! current_user_can( 'administrator' ) &&
! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
wp_redirect( home_url() );
exit;
}
}
Obviously the user still needs to access all other front end pages of the site, just not the admin area.
use wp_get_current_user():
$current_user = wp_get_current_user();
if ($current_user->user_login != 'mack') {
//It's not mack...
}
If you want a temporary fix without working in your code maybe can you disable user
https://srd.wordpress.org/plugins/disable-users/
Otherwise as #Eduardo say you can do something like this
add_action( 'init', 'blockusers_init' );
function blockusers_init() {
$current_user = wp_get_current_user();
if ( is_admin() && ! current_user_can( 'administrator' ) &&
! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) && $current_user->user_login != 'mack' ) {
wp_redirect( home_url() );
exit;
}
}

Throw a 404 error at /wp-admin/

I'm running the Rename wp-login.php plugin on my WordPress website. This plugin allows you to 'rename' wp-login.php and /wp-admin/ so the visitor can't find the admin login.
However, when the visitor visits /wp-admin/ he'll be seeing the 'You must log in to access the admin area' error message.
I want to change this so the visitor sees a 404 page. The code for this error message is as follows:
if ( is_admin() && ! is_user_logged_in() && ! defined( 'DOING_AJAX' ) ) {
wp_die( __( 'You must log in to access the admin area!', 'wp-login' ) );
}
I have tried changing it to
if ( is_admin() && ! is_user_logged_in() && ! defined( 'DOING_AJAX' ) ) {
function generate_404() {
global $wp_query;
$wp_query->set_404();
}
add_action('wp','generate_404');
}
and I have also tried
if ( is_admin() && ! is_user_logged_in() && ! defined( 'DOING_AJAX' ) ) {
global $wp_query;
$wp_query->set_404();
status_header( 404 );
get_template_part( 404 ); exit();
}
But none of that did work. Please help me out.

how to change redirect when login in theme my login wordpress

Hello I want to change redirect when login in theme my login wordpress
now when login yet, it will go to profile page but I don't want this. I want to change it to go to my homepage.
I find code in themed-profiles.php . I think I much edit code in line 166 but how to edit? please help.
if ( is_user_logged_in() ) {
//$redirect_to = get_option('shop-subearphone2');
$redirect_to = Theme_My_Login::get_page_link( 'profile' );
wp_redirect( $redirect_to );
exit;
Okay, I got it sorted. I just removed the redirection module from used a WordPress function and put function.php
function redirect_to_profile() {
$who = strtolower(sanitize_user($_POST['log']));
$redirect_to = get_option('home') . '/profile?' . $who;
return $redirect_to;
}
add_filter('login_redirect', 'redirect_to_profile');
add_action( 'init', 'blockusers_init' );
function blockusers_init() {
if ( is_admin() && ! current_user_can( 'administrator' ) &&
! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
wp_redirect( home_url() ); // set here your redirection Url
exit;
}
}
Try this code
if ( is_user_logged_in() ) {
add_action('init','redirect');
function redirect(){
global $pagetheme;
if( 'wp-login.php' == $pagetheme) {
wp_redirect('http://yourpage.com/'); // instead of yourpage.php to what page you want to redirect
}}
}
You can put a custom file in the /wp-content/plugins folder which must be called 'theme-my-login-custom.php' and in there I just put:
<?php
function custom_redirect() {
return "/member";
}
add_filter( 'tml_redirect_url', 'custom_redirect' );
?>
This did the job for me using TML 6.4.5

Woocommerce - Hide shop for unregistered user

I have a woocommerce web site and I would like to hide the shop when the user in not logged. I put this code in the file ! archive-product.php which is in my template 'twentytwelve-child' in a woocommerce folder.
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
auth_redirect();
get_header( 'shop' ); ?>
Normaly the 'auth_redirect()' have to redirected me in the login page, but it just doesn't work.
I tried also with this code but it does not work also.
$login = is_user_logged_in();
if ($login == FALSE ) {
wp_redirect( home_url() );
exit;
}
Did I do something wrong?
Thanks you. I also add some other features which can be helpful.
// Redirect none registered users to a login page
function custom_redirect() {
if( (is_shop() || is_product() || is_product_category() ) && ! is_user_logged_in() ) {
wp_redirect( site_url( '/mon-compte' ) );
exit();
}
}
add_action("template_redirect","custom_redirect");
You don't need to modify Woocommerce template file for what you are trying to achieve. Just add the following code to functions.php
function custom_redirect() {
if( is_shop() && ! is_user_logged_in() ) {
wp_redirect( home_url() );
exit();
}
}
add_action("template_redirect","custom_redirect");

Categories