I want to protect the configuration page of a backup plugin in wp-admin.
My user ID = 1. If user is other than 1 redirects out of wp-admin.
The backup page is this: http://www.mysite.com.br/wp-admin/options-general.php?page=updraftplus
I wrote this code but I do not have much experience so please forgive me because there must be errors
*I want to put this code in functions.php, I believe it's the right place.
add_filter( 'parse_query', 'redirect_user' );
function redirect_user($query) {
$user_id = get_current_user_id();
if (strlen($user_id) <> 1)
global $pagenow,$post_type;
if (is_admin() && $pagenow=='options-general.php' && $post_type =='page') {
if ($post_type == "updraftplus") {
wp_redirect( home_url() );
}
}
}
}
I think I got the code right:
add_action( 'admin_init', 'redirect_user_backup' );
function redirect_user_backup() {
global $pagenow;
$user_id = get_current_user_id();
if( $pagenow == 'options-general.php' && isset( $_GET['page'] ) && $_GET['page'] == 'updraftplus' ){
if ($user_id <> 1) {
wp_redirect( admin_url( '/index.php' ), 301 );
exit;
}
}
}
Related
I have created a page template that will be used to show a custom wordpress login form.
<?php
/*
* Template Name: Login
*/
get_header();
if( !is_user_logged_in() ){
?>
html code
<?php
} else {
$user = wp_get_current_user();
if( current_user_can('edit_users') && in_array('customrole', $user->roles) ){
wp_redirect( site_url('/role-page') );
exit;
}
if( current_user_can('edit_users') && in_array('customrole1', $user->roles) ){
wp_redirect( site_url('/role-page1') );
exit;
}
}
get_footer();
?>
I've noticed that if an user is logged in, the page will be loaded and is blank, the users will be not redirected to the desired location. How I can fix this problem and let the logged users be redirected based on their role?
Use template_redirect
add_action( 'template_redirect', function() {
if ( is_user_logged_in()){
$restricted = array( 250(login page id) ); // all your restricted pages
if ( in_array( get_queried_object_id(), $restricted ) ) {
wp_redirect( site_url( '/custom_page slug' ) );
exit();
}
}
});
I think we can use wp_redirect before content is sent to the browser. You should use hooks for that.
But you can try this for custom template:
global $current_user;
$role = $current_user->roles;
$current_role = implode( $role );
if ( $current_role == 'customrole' ) {
$url = site_url( '/role-page' );
echo "<script>window.location.href = '$url';</script>";
exit;
}
I am trying to redirect users to the form page if they have not yet filled it so that if they visit the pages in my conditions, they can be redirected to back to fill the form first.
I have been able to redirect other pages properly but I'm having an issue with a page that contains a parameter.
The link https://example.com/wp-admin/admin.php?page=booknetic
I tried:
//Redirect user back to application form if they have not filled it
add_action( 'template_redirect', 'redirect_if_user_logged_in' );
function redirect_if_user_logged_in() {
if ( is_front_page() || isset($_GET['page']) || is_page('profile') || is_page('account')
&& is_user_logged_in() && $count == 0) {
wp_redirect( '/first-time-application-temp/');
exit;
}
}
AND
//Redirect user back to application form if they have not filled it
add_action( 'template_redirect', 'redirect_if_user_logged_in' );
function redirect_if_user_logged_in() {
if ( is_front_page() || $_SERVER['REQUEST_URI'] == '/wp-admin/admin.php?page=booknetic' || is_page('profile') || is_page('account')
&& is_user_logged_in() && $count == 0) {
wp_redirect( '/first-time-application-temp/');
exit;
}
}
is_front_page(), and is_page('profile') || is_page('account') && is_user_logged_in() && $count == 0)
Note* $count is a global variable that checks whether the user had an entry in the form.
How do I test whether the is_page == wp-admin/admin.php?page=booknetic ?
Could it be failing because the menu link is just a custom link that redirects to the page https://example.com/wp-admin/admin.php?page=booknetic which is an existing page created by another plugin?
add_action( 'template_redirect', 'redirect_if_user_logged_in' );
function redirect_if_user_logged_in() {
if ( (is_front_page() || isset($_GET['page']) || is_page('profile') || is_page('account') ) && is_user_logged_in() && $count == 0) {
wp_redirect( '/first-time-application-temp/');
exit;
}
}
OR
add_action( 'template_redirect', 'redirect_if_user_logged_in' );
function redirect_if_user_logged_in() {
if ( ( is_front_page() || $_SERVER['REQUEST_URI'] == '/wp-admin/admin.php?page=booknetic' || is_page('profile') || is_page('account') ) && is_user_logged_in() && $count == 0) {
wp_redirect( '/first-time-application-temp/');
exit;
}
}
I need help, I have been at this for too many hours for me to no be embarrassed. Please, why am I not getting this?
function members_only() {
$cookie_name = 'cookie_name';
$error_url = home_url($path = '/403-error/');
global $pagenow;
$array_cookie_value = json_decode( stripslashes($_COOKIE[$cookie_name]), true);
$woo_user_id = $array_cookie_value['user_id'];
$user_meta = get_user_meta($woo_user_id);
$user_order_status = $user_meta['doris_shop_enabled'][0];
if ( is_admin() || is_front_page() || $pagenow == 'wp-login.php' || is_page('403-error') ) {
echo('Do nothing');
} elseif ( !isset($_COOKIE[$cookie_name]) || (isset($_COOKIE[$cookie_name]) && $user_order_status === 0) ) {
wp_safe_redirect( $error_url );
exit;
} else {
echo("Very limited.");
}
}
add_action( 'wp', 'members_only' );
The problem was in Chrome data that was saved, outside of the cookie and hard-refresh which I make regularly. I needed to go into the Settings > Cookies and other website data and empty it all from there. After that I got it to work.
Yes I made some minor changes to the code also, but that was the main problem.
Thanks all for trying to help :)
I have created a page for a certain user in wordpress. Let's say his username is John. I am looking for PHP script that allow only 'John' to access that page and if users with different username other than 'John' tries to access the page they are redirected to another page.
I am new to PHP, so here's some code I have tried. But it redirects all users, even the user with username 'John'
<?php $user_info = get_userdata(1);
$username = $user_info->user_login;
if ( $username=='John' ) {
echo '';
} else {
wp_redirect( home_url() );
exit;
}
?>
Here's a wordpress page with parameters to get userdata - https://codex.wordpress.org/Function_Reference/get_userdata
You can use wp_get_current_user() function instead.
global $current_user;
get_currentuserinfo();
$username = $current_user->user_login;
if ( $username == 'John' ) {
echo '';
} else {
wp_redirect( home_url() );
exit;
}
This may solve your problem.
add_action( 'init', 'blockusers_init' );
function blockusers_init() {
global $current_user;
$current_user = wp_get_current_user();
if ( 'John' == $current_user->user_login ) {
//your desire page url
wp_redirect( 'your page url' );
exit;
} else {
wp_redirect( home_url() );
exit;
}
}
Take reference from
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;
}
}
https://premium.wpmudev.org/blog/limit-access-to-your-wordpress-dashboard/
Try this : First check if user is logged_in if yes then take the user information and do what ever you want with it
if(is_user_logged_in()){
$current_user = wp_get_current_user();
if($current_user->user_login == "John"){
wp_safe_redirect("Where ever you want");
exit;
}
else
wp_safe_redirect(home_url('/'));
NOTE : Make sure to check your username in database , I set my username as my lastname.
I am trying to hide wordpress login\register\lostpassword\logout forms and redirect to my own pages
I have tried this
add_action('init','possibly_redirect');
function possibly_redirect(){
global $pagenow;
if( 'wp-login.php' == $pagenow ) {
wp_redirect('http://google.com/');
exit();
}
}
but this will redirect all pages to 1 page i want to edit individuals like registration and lost password each to redirect elsewhere so I tried this
/* שינוי עמוד התחברות *//**
/*התחברות*/
function possibly_redirect(){
global $pagenow;
if( 'wp-login.php' == $pagenow ) {
if ( isset( $_POST['wp-submit'] ) || // this is line 33
else wp_redirect( home_url('/%D7%94%D7%AA%D7%97%D7%91%D7%A8/') ); // or wp_redirect(home_url('/login'));
exit();
}
}
add_action('init','possibly_redirect');
/*התנתקות*/
function possibly_redirect(){
global $pagenow;
if( 'wp-login.php' == $pagenow ) {
if ( isset($_GET['action']) && $_GET['action']=='logout') || // in case of LOGOUT
else wp_redirect( home_url() ); // or wp_redirect(home_url('/login'));
exit();
}
}
add_action('init','possibly_redirect');
/*איפוס יסמא*/
function possibly_redirect(){
global $pagenow;
if( 'wp-login.php' == $pagenow ) {
if ( isset($_GET['checkemail']) && $_GET['checkemail']=='confirm') || // in case of LOST PASSWORD
else wp_redirect( home_url('/%D7%90%D7%99%D7%A4%D7%95%D7%A1-%D7%A1%D7%99%D7%A1%D7%9E%D7%90/') ); // or wp_redirect(home_url('/login'));
exit();
}
}
add_action('init','possibly_redirect');
/*הרשם*/
function possibly_redirect(){
global $pagenow;
if( 'wp-login.php' == $pagenow ) {
if ( isset($_GET['checkemail']) && $_GET['checkemail']=='registered') ) return; // in case of REGISTER
else wp_redirect( home_url('/%D7%94%D7%A8%D7%A9%D7%9E%D7%94/') ); // or wp_redirect(home_url('/login'));
exit();
}
}
add_action('init','possibly_redirect');
dont get me wrong I have no idea how to write php I took this from some searches and tried to play around so it works... but it does not work... sometimes it shows an error
Parse error: syntax error, unexpected T_ELSE in /home/content/11/9595411/html/luachmodaot/wp-content/themes/boozurk/functions.php on line 33
can someone help me out with this please?
see below for correct php code syntax, but i've no idea if it will work based on what you have given. Are you creating a theme yourself or modifying a theme?
you had a number of errors after the one you posted.
Basically your if statement syntax was wrong - should be if(you exist && you == you) {do this please;} else {ok do this instead;} -- if you mess up the brackets you get an error. btw you dont need to check isset $var && $var == something, just check its equal to something, if its not, it will fail.
why do you want to redirect away from the pages? if its something as simple as styling you can do something like this https://premium.wpmudev.org/blog/create-a-custom-wordpress-login-page/. if you dont know php, it might be a place to start rather than creating your own method
function possibly_redirect(){
global $pagenow;
if( 'wp-login.php' == $pagenow ) {
if ( $_POST['wp-submit'] ) { // this is line 33 - the error was telling you had a open statement....syntax for if statement : if( ) { } else { }
wp_redirect( home_url('/%D7%94%D7%AA%D7%97%D7%91%D7%A8/') ); // or wp_redirect(home_url('/login'));
exit();
}
if( 'wp-login.php' == $pagenow ) {
if ( $_GET['action']=='logout') { // in case of LOGOUT
wp_logout();
wp_redirect( home_url() ); // or wp_redirect(home_url('/login'));
exit();
}
}
if( 'wp-login.php' == $pagenow ) {
if ( $_GET['checkemail']=='confirm') { // in case of LOST PASSWORD
wp_redirect( home_url('/%D7%90%D7%99%D7%A4%D7%95%D7%A1-%D7%A1%D7%99%D7%A1%D7%9E%D7%90/') ); // or wp_redirect(home_url('/login'));
exit();
}
}
if( 'wp-login.php' == $pagenow ) {
if ( $_GET['checkemail']=='registered' ) { //return;---> use in function only!!!!!!!!
wp_redirect( home_url('/%D7%94%D7%A8%D7%A9%D7%9E%D7%94/') ); // or wp_redirect(home_url('/login'));
exit();
}
}
}
add_action('init','possibly_redirect');