WordPress Login Redirect: Redirect URL adding to login URL instead of replacing it - wordpress

I've got a redirect that if someone is logged into https://yieldofdreams.live/login they get redirected to another page.
I used the following function:
function add_login_check()
{
if ( is_user_logged_in() && is_page(608) ) {
wp_redirect('yieldofdreams.live/redirect/');
exit;
}
}
add_action('wp', 'add_login_check');
This does work, but instead of redirecting to the URL above, it instead adds to the login URL.
So that redirect takes you instead to;
https://yieldofdreams.live/login/yieldofdreams.live/redirect/
How can I make it where it completely redirects to the proper URL I want?

Use template_redirect instead.
function bks_add_login_check()
{
if ( is_user_logged_in() && is_page(608) ) {
wp_redirect(home_url() . '/redirect');
exit;
}
}
add_action('template_redirect', 'bks_add_login_check');
Learn about the hook here : https://developer.wordpress.org/reference/hooks/template_redirect/

Related

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 URL Modification

I have a WordPress page at
http://www.mysiteurl.com/pagename/
and I want any URL of the form
http://www.mysiteurl.com/pagename/{any string here}
to redirect or display my original page: http://www.mysiteurl.com/pagename/. How do I go about doing this?
Like what Kodos Johnson suggested, the function should be something like this:
function redirect_to_page() {
//get parsed uri separated by '/'
$a_uri = explode('/', $_SERVER['REQUEST_URI']);
//check if the page is the page you want to redirect
if( $a_uri[1] === 'pagename' ) {
//redirect to siteurl/pagename
wp_redirect( home_url('pagename'));
exit();
}
}
add_action('template_redirect', 'redirect_to_page');
Hope this helps!

redirection for deleted pages

Hi I need to find a way to redirect deleted pages to another pages using 301 redirection, to a specific page depending on the URL of the page.If the url is
http://example.com/projects/test-page-redirectpage/ then it should be redirect to the 'redirectpage' after page deletion.There are hundreds of pages like that which have 'redirectpage' at the end of URL,so I can not do that manually by and redirection plugin.I have wrote following code but it is only working by page name.I want such a function which can detect that the 'redirectpage' from the URL and if that url is not exit then it should be redirect to the 'redirectpage'
function get_page_by_name($pagename)
{
$pages = get_pages();
foreach ($pages as $page) if ($page->post_name == $pagename) return $page;
return false;
}
function redirect_301() {
$page = get_page_by_name('test-page-redirectpage');
if (empty($page)) {
wp_safe_redirect( home_url('http://example.com/redirectpage/'), 301 );
exit;
}
}
add_action( 'template_redirect', 'redirect_301', 1 );
You are correct with using template_redirect but you should be checking if the URL can be translated to post ID and the current request URL (from global $wp object) for occurrence of redirectpage string (at the end of it):
function so_423_redirectpage_redirect()
{
global $wp;
if (url_to_postid(home_url($wp->request)) === 0 &&
('redirectpage' === substr(untrailingslashit($wp->request), -12) || 'redirect-page' === substr(untrailingslashit($wp->request), -13))) {
wp_safe_redirect(home_url('http://example.com/redirectpage/'), 301);
exit();
}
}
add_action('template_redirect', 'so_423_redirectpage_redirect');
I think you should use the 404.php file template here (from your theme), so you're pretty sure that the page visited doesn't exist.
Before the get_header() function you can do the following check:
$requested_uri = explode('/', $_SERVER[REQUEST_URI]);
if(strpos(array_pop($requested_uri), '-redirectpage') !== FALSE) {
wp_safe_redirect(site_url('redirectpage'), 301);
}

Resources