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');
Related
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 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;
}
}
}
I would like to change below code to redirect some external link rather than redirecting to login page. How can I do this?
<?php
function vbcr_simple_content_restriction() {
global $wp_query;
if( !is_user_logged_in() ) {
if( is_category() || is_archive() || is_single() || is_tax() || is_tag() || is_feed() || is_comment_feed() || is_attachment() || is_search() ) {
$option_redirect_to = get_option('redirect-to');
if(empty($option_redirect_to)) {
// retrieve WP login URL
$option_redirect_to = wp_login_url();
}
$status = "302";
wp_redirect( $option_redirect_to, $status ); exit;
}
}
ob_flush();
}
function content_restriction_output_buffer() {
// this is needed for the redirection
ob_start();
As you can see it goes to wp login url:
$option_redirect_to = get_option('redirect-to');
if(empty($option_redirect_to)) {
// retrieve WP login URL
$option_redirect_to = wp_login_url();
How can I change it to this link: https://example.com/external ?
Basically I have two user roles that can access to my website, the first one is a subscriber and the second one a customer, and I want them to meet certain conditions:
if any of this one is not logged in try to access to shop page, they are gonna be redirected to the register page
if there is a subscriber or a customer logged in and they try to go to the register page, they are gonna be redirected to their "my account" page
if there is a subscriber logged in but is not a customer and try to access to the shop page, he is gonna be redirected to the id confirmation page (a page with a form that subscribers need to fill and send, and then later we confirm his identity and change their role to customers)
if there is a customer logged in, he can access to the shop page, and not the id confirmation page.
not logged in users can't access to the id confirmation page, they are gonna be redirected to the register page
I have this code below, but it gives me an issue, don't know why it redirect my customers to "my account" page after going to the shop page:
add_action( 'template_redirect', 'subscribers_redirection' );
function subscribers_redirection() {
if ( !is_user_logged_in() && !current_user_can( 'subscriber' ) && !current_user_can( 'customer' ) && ( is_shop() || is_product_category() || is_product_tag() || is_product() ) ) {
wp_redirect( home_url( '/register' ) );
} else if ( is_user_logged_in() && current_user_can( 'subscriber' ) && !current_user_can( 'customer' ) && ( is_shop() || is_product_category() || is_product_tag() || is_product() ) ){
wp_redirect( home_url( '/id-confirmation' ) );
} else if ( is_user_logged_in() && current_user_can( 'subscriber' ) && current_user_can( 'customer' ) && ( is_shop() || is_product_category() || is_product_tag() || is_product() ) ){
wp_redirect( home_url( '/shop' ) );
} else if ( is_user_logged_in() && is_page( 'Register' ) ){
wp_redirect( home_url( '/my-account' ) );
} else if ( !current_user_can( 'subscriber' ) && is_page( 'id-confirmation' ) ){
wp_redirect( home_url( '/register' ) );
exit();
}
}
My assumption:
I think that for some reason my customers are been redirected to the register page first, but one of the conditions says that if you are a customer and are on the register page, you are gonna be redirected to "my account" page, the thing is why they are been redirected to register first after going to shop.
also I tested with this other code below too:
add_action( 'template_redirect', 'verify_logged_in' );
function verify_logged_in() {
if ( !is_user_logged_in() ) {
if ( is_shop() || is_product_category() || is_product_tag() || is_product() || is_page( 'id-confirmation' ) ) {
wp_redirect( home_url( '/register' ) );
} else ( is_user_logged_in() ) {
function subscribers_redirection() {
$current_user = wp_get_current_user();
$user_id = $current_user->ID;
$user_info = get_userdata($user_id);
$user_rols = implode(', ', $user_info->roles) . "\n";
switch ($user_rols) {
case "subscriber":
if ( is_shop() || is_product_category() || is_product_tag() || is_product() ) {
wp_redirect( home_url( '/id-confirmation' ) );
} else if ( is_page( 'Register' ) ) {
wp_redirect( home_url( '/my-account' ) );
} else {
exit();
}
break;
case "customer":
if ( is_page( 'id-confirmation' ) || is_page( 'Register' )) {
wp_redirect( home_url( '/my-account' ) );
} else {
exit();
}
break;
default:
exit();
}
}
add_action( 'template_redirect', 'subscribers_redirection' );
}
}
}
but didn't work the second if statement "if is logged in". Different from the first code, I have not assumptions on why is not working this second one.
May be you need this way of your script? As for me, this algorithm is more unterstandable. If wp_redirect works correctly on your web-site, then this filter will work as you expected. Firstly you detect the page of visit and after that you check the login status. (true_case) ? something_one : something_two is the same as if (true) { something_one } else { something_two }.
if (is_shop() || is_product_category() ||
is_product_tag() || is_product() || is_page( 'id-confirmation' )) {
(is_user_logged_in()) ? subscribers_redirection() : wp_redirect(home_url('/register'));
}
More over, you have too many duplicates of code. Try to simplify it, because half of it isn't needed for such easy task.