How do I add multiple urls to a wp_get_refererer? - php

I have a cf7 form that is connected to multiple pages on the site. How do I allow these pages to have access to one thank you page? I tried this code below but it doesnt work.
It doesnt work as in, only array[0] is getting redirected. Not the others.
I am still a noob with PHP so do pardon the logic
add_action('template_redirect', function() {
$theOrigins = array(
'https://web1',
'https://web2',
'https://web3',
'https://web4',
);
$ref = wp_get_referer();
// ID of the thank you page
if (!is_page(1234)) {
return;
}
// coming from the form, so all is fine
if (in_array($ref, $theOrigins)) {
return;
}
// we are on thank you page
// visitor is not coming from form
// so redirect to home
wp_redirect(get_home_url());
exit;
} );

Related

Gravity forms, checking for duplicate entry and redirecting to that post after once submission fails

I have a gravity form that creates a custom post. On success, I managed to redirect the user's browser to the new post.
However, when the validator fails because the user is typing the same data on the form, I would like them to be redirected to the already existing post.
I've been researching, this may be possible to achieve with the filter "gform_validation". But im not sure how the logic here would work.
Here is my approach, currently not working.
add_filter( 'gform_validation_1', 'custom_validation' );
function custom_validation( $validation_result ) {
$form = $validation_result["form"];
$entry = $_POST["input_21"];
if($entry != null ){
// Do nothing
}
else{
$validation_result["is_valid"] = false;
header("Location: http://www.anypage.com"); /* Redirect browser to some page */
}
$validation_result["form"] = $form;
return $validation_result;
};
Currently when the form fails, it keeps redirecting to the homepage.
I'll appreciate any help on this. Thanks.

Render login form in my custom theme Wordpress

I render login form on my header template using this code:
<?php wp_login_form($args); ?>
When I pass proper credentials, it redirect me to homapage and all seems to be fine, but when I put wrong login or pass, it redirect me to the folowing url:
http://localhost/wordpress/wp-login.php
So the question is how I can output errors on the same page , and prevent redirection to the wp-login ? I try to find solution but didnt have any results. Thanks!
Add this to your functions.php :
add_action( 'wp_login_failed', 'my_front_end_login_fail' ); // hook failed login
function my_front_end_login_fail( $username ) {
$referrer = $_SERVER['HTTP_REFERER']; // where did the post submission come from?
// if there's a valid referrer, and it's not the default log-in screen
if ( !empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin') ) {
wp_redirect( $referrer . '?login=failed' ); // let's append some information (login=failed) to the URL for the theme to use
exit;
}
}
This code redirects to the same page as the user tries to log in from.
Change $referrer for another page.
Hope it will works for you.

Check from which page the user is coming to the current page in wordpress

I want to check if a user comes from a specific page to the current page.
e.g
if (user comes from /bingo.php)
{ wp_redirect( /newpage.php );exit; }
else { // do nothing };
thank you :)
Without completely answering your question, I'd say you could look into $_SERVER['HTTP_REFERER']. This contains the previous page your user came from. Please note that this is not always set.
Here is my code for my case:
if (!is_user_logged_in() ) {
$icamefrom = $_SERVER['HTTP_REFERER'];
if ( $icamefrom == 'the_site_you_want_to_check') { wp_redirect( '/signup' ); exit; };
}

wordpress login failed redirect url does not work properly

In my wordpress project i created a front end log in for users and when user enters a wrong log in information it must redirect the login failed attachment with the url. Below code does it for the first time and then it append to the url continuously when trying to log in. How can i limit this.
add_action('wp_login_failed', 'my_front_end_login_fail');
function my_front_end_login_fail($username){
// Get the reffering page, where did the post submission come from?
$referrer = $_SERVER['HTTP_REFERER'];
// if there's a valid referrer, and it's not the default log-in screen
if(!empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin')){
// let's append some information (login=failed) to the URL for the theme to use
wp_redirect($referrer . '&login=failed');
exit;
}
}
Any helps would be appreciated.
try this code
add_action('wp_login_failed', 'my_front_end_login_fail');
function my_front_end_login_fail($username){
// Get the reffering page, where did the post submission come from?
$referrer = $_SERVER['HTTP_REFERER'];
// if there's a valid referrer, and it's not the default log-in screen
if(!empty($referrer) && !strstr($referrer,'wp-login') && !strstr($referrer,'wp-admin')){
// let's append some information (login=failed) to the URL for the theme to use
////already not login failed append
if(!strstr($referrer,'&login=failed'))
{
wp_redirect($referrer . '&login=failed');
}
else
{
////if alreday append go to same url
wp_redirect($referrer);
}
exit;
}
}

WordPress - Check if user is logged in

I am fairly new to WordPress. On my homepage I have a navigation bar which I only want to show to people who are logged in as users.
In my header.php the function is_logged_in doesn't seem to work.
I want to place a condition in my header.php file to check if the user has logged in (and then display the navigation).
Any advice would be helpful.
Use the is_user_logged_in function:
if ( is_user_logged_in() ) {
// your code for logged in user
} else {
// your code for logged out user
}
Example: Display different output depending on whether the user is logged in or not.
<?php
if ( is_user_logged_in() ) {
echo 'Welcome, registered user!';
} else {
echo 'Welcome, visitor!';
}
?>
Try following code that worked fine for me
global $current_user;
get_currentuserinfo();
Then, use following code to check whether user has logged in or not.
if ($current_user->ID == '') {
//show nothing to user
}
else {
//write code to show menu here
}
get_current_user_id() will return the current user id (an integer), or will return 0 if the user is not logged in.
if (get_current_user_id()) {
// display navbar here
}
More details here get_current_user_id().
This problem is from the lazy update data request of Chrome.
At the first time you go to homepage. Chrome request with empty data. Then you go to the login page and logged in. When you back home page Chrome lazy to update the cookie data request because this domain is the same with the first time you access.
Solution:
Add parameter for home url. That helps Chrome realizes that this request need to update cookie to call to the server.
add at dashboard page
<?php
$track = '?track='.uniqid();
?>
<img src="/img/logo.svg">
I think that.
When guest is launching page, but Admin is not logged in we don`t show something, for example the Chat.
add_action('init', 'chat_status');
function chat_status(){
if( get_option('admin_logged') === 1) { echo "<style>.chat{display:block;}</style>";}
else { echo "<style>.chat{display:none;}</style>";}
}
add_action('wp_login', function(){
if( wp_get_current_user()->roles[0] == 'administrator' ) update_option('admin_logged', 1);
});
add_action('wp_logout', function(){
if( wp_get_current_user()->roles[0] == 'administrator' ) update_option('admin_logged', 0);
});

Categories