IF conditions, data from cookie not validating - php

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 :)

Related

How to use slug to redirect a user to a different page using wp_redirect()

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;
}
}

Display content to diverses type of role but not all

I would like to display content to a certains kind of user (some of them custom) if they are logged-in.
Is there a way to associate :
if( is_user_logged_in() && ($user_role != "Administrator" && $user_role != "customrole") ):
// display content
else:
// redirect back to other page
wp_redirect( 'https://customurl.com' );
exit;
endif;
But when I try thsio code I have a fatal error. What do I do wrong please ?
Best regards,
Clément
if you wanna use multiple conditions you have to remove the () afther your && in the if statement and try it like this
if(is_user_logged_in() && $user_role != "Administrator" && $user_role != "customrole"){
//display content
}
else{
wp_redirect( 'https://customurl.com' );
exit;
}
Finally it was a mix, I used permission and a custom field if I want some part accessible to everyone :
$access_to_everyone = get_field('access_to_everyone');
// var_dump($access_to_everyone);
if ( $access_to_everyone == true ) {
$access = "granted";
} else {
if( is_user_logged_in() ) {
if ( !current_user_can('administrator') ) {
$access = "refused";
}
} else {
$access = "granted";
}
} else {
$access = "refused";
}
}
if ( $access == "granted" ) {
display A;
} else {
redirection";
}

Redirect user if not specific user

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;
}
}
}

How to change redirection way on very basic content restriction?

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 ?

registartion form on wordpress

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');

Categories