Want to make a wordpress accessible to view only thorugh redirection from a particular page and cannot be viewed through its own page url - wordpress

I want to create a function on my wordpress site that a particular page 'x' can be viewed or acessible only through redirection from a particular page 'y' and connot be viewed through its own url (only redirecton should be the way to view that page 'x')
I have tried this but no result what is wrong with it please check:
is_page(776) {
$referer = wp_get_referer(); if ( 'https://xnd.vtgcindia.online/' !== $referer ) { // User arrived from another source so send them away! wp_redirect( 'http://xnd.vtgcindia.onlne/' ); exit; }
}
(Note: Page "X" represent page to be viewed and Page "Y" represent page through which user get redirection or can visit. I use this for easy explanation of the situation)
Hope you answer and help!
is_page(776) {
$referer = wp_get_referer(); if ( 'https://xnd.vtgcindia.online/' !== $referer ) { // User arrived from another source so send them away! wp_redirect( 'http://xnd.vtgcindia.onlne/' ); exit; }
}

Related

How to redirect user to another page if the user is logged in - WordPress Elementor Page Builder

I have created a registration form using Elementor Page Builder. Now, I want to redirect the user to a different page if he/she is trying to access that registration page after logging in.
Is there any Elementor hook available for that? I know the WordPress function called is_user_logged_in().
function my_logged_in_redirect() {
if ( is_user_logged_in() && is_page( 12 ) )
{
wp_redirect( get_permalink( 32 ) );
die;
}
}
add_action( 'template_redirect', 'my_logged_in_redirect' );
You should get the ids of the page where the form is and the id of the page you want to redirect the user to.
Code goes in your child theme functions.php file
Reference: here
The 'Content Area Not Found' error might appear on Elementor designed sites when you use that snippet and try to edit page of ID 12 (in your example) in certain cases.
To avoid this, add the following code before the if-statement of your snippet:
if ( \Elementor\Plugin::$instance->preview->is_preview_mode() ) {
return;
}

Woocommerce guest checkout not redirecting to order-received page

I have created a flow using a function that when someone try to make an order without login he/she is redirected to a custom login-register pages where they choose to either login/register or continue as a guest - see function code below, now when someone makes an order he/she gets redirected to custom login-register page instead of order-received page. What's wrong here?
function ace_redirect_pre_checkout() {
if ( class_exists( 'woocommerce' ))
$redirect_page_id = 104549;
if (!is_user_logged_in() && is_page(17)) {
wp_redirect(get_permalink($redirect_page_id));
} elseif (is_user_logged_in() && is_page($redirect_page_id)) {
wp_redirect(get_permalink(wc_get_page_id('checkout')));
}
}
add_action('template_redirect', 'ace_redirect_pre_checkout');

wordpress redirection from header.php not working

Nobody access my full wordpress website without login, if user is not logined than redirect it to http://example/submit-project/.
I'm trying to do this with this code:
$current_user = wp_get_current_user(); $crntusr = $current_user; if($crntusr->ID == 0){ wp_redirect( 'example.com/login'; ); }
But get this error:
Warning: Cannot modify header information - headers already sent by
(output started at
/home/content/n3pnexwpnas02_data02/36/3929936/html/wp-content/themes/freelanceengine/header.php:14)
in
/home/content/n3pnexwpnas02_data02/36/3929936/html/wp-includes/pluggable.php
on line 1195
1) wp_redirect() does not exit automatically, and should almost always be followed by a call to exit.
2) you should make an redirect before your template output something.
3) Better to refrain from using absolute links like http://example.com, you can get your WP login page via wp_login_url() function.
Remove your redirect code from your header.php file and try to add this code to your functions.php:
add_action ('wp_loaded', 'my_custom_redirect');
function my_custom_redirect() {
if (!is_user_logged_in() and !in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php')) ) {
wp_redirect(wp_login_url());
exit;
}
}
Update.
If your login form is on custom page (http://example/submit-project/), then you should use this code:
add_action ('wp_loaded', 'my_custom_redirect');
function my_custom_redirect() {
if (!is_user_logged_in() and $_SERVER['REQUEST_URI'] != '/submit-project/' ) {
wp_redirect('http://example/submit-project/');
exit;
}
}

Wordpress redirects visitors from certain url to their profiles

I am searching for a way to redirect visitors coming from a certain url to their profiles in wordpress
Example if you are coming from example.com/test.html and going to example.com you will be directed to http://www.example.com/author/user/
Is that possible to be done? Thanks for help
something like
if($_SERVER['HTTP_REFERER']){
if($_SERVER['HTTP_REFERER'] == "your test url" && is_user_logged_in()){
$targetUrl = get_edit_user_link();
wp_redirect( $targetUrl );
exit;
}
}
Should do the trick - but escape the referer since its a common attack vector for web apps.
EDIT
If i am reading you question correctly, you want logged in users, to go to their profile page whenever they go to the gomepage?
in that case, you can create a function similar to
add_action('wp_head','wpmy_redirect_logged_in_users_away_from_home');
function wpmy_redirect_logged_in_users_away_from_home() {
if( is_user_logged_in() && ( is_home() || is_front_page() ) ) {
$targetUrl = get_edit_user_link();
wp_redirect( $targetUrl );
exit;
}
}

wordpress login redirect function

I am trying to create my own plugin in wordpress. Everything works great but I want to make a loginredirect check for users who want to access to the plugins page.
Here is my function in my functions.php:
function wpuf_auth_redirect_login() {
$user = wp_get_current_user();
if ( $user->id == 0 ) {
nocache_headers();
wp_redirect(get_option('siteurl') . '/wp-login.php?redirect_to=' . urlencode($_SERVER['REQUEST_URI']));
exit();
}
}
And this is from my myplugin.php:
function wpuf_user_edit_profile() {
wpuf_auth_redirect_login(); // if not logged in, redirect to login page
nocache_headers();
wpuf_post_form_style();
wpuf_user_edit_profile_form();
}
add_shortcode('wpuf_editprofile', 'wpuf_user_edit_profile');
It is not working. If I enter the plugins page as a guest in browser it does not redirect. It only shows the template uncomplete. Any help?
Where you say that it 'only shows the template uncomplete', this could indicate that there is a syntax error in your PHP code, and the server has stopped rendering the page at the point of that error.
I would suggest that you check for any errors by turning error reporting on, and seeing what happens from there.

Resources