Skip to main content
edited title
Link
jdm2112
  • 3.7k
  • 1
  • 17
  • 23

How limit add_action to only oneredirect user after login to a specific page?

Source Link

How limit add_action to only one page?

I have a page with mixed content in three different tabs. The first tab is open access to all but the content of the last two are for logged in users only so by sending the users to login with:

<p>You need to be logged in in order to view this content. Would you like to <a href="<?php echo esc_url(wp_login_url()); ?>"><?php _e('log in') ?></a> now?</p>

I'm then handling this in functions.php by using the login_form action where I'm declaring the wanted redirect url with a redirect_to (see code block).

How do I limit this action to only work from one specific page? I've tried using the code below but it only works without the if (is_page()) part. I need it to work only when user clicks on the link from this page.

function my_login_redirect() { global $redirect_to; // Check if the page is "about us" if (is_page('about-us')) { if (!isset($_GET['redirect_to'])) { $redirect_to = site_url('/about-us'); } else { $redirect_to = $_GET['redirect_to']; } } } add_action('login_form', 'my_login_redirect'); 

Any help most appreciated! Thank you