{"id":5363,"date":"2026-04-15T11:39:31","date_gmt":"2026-04-15T10:39:31","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=5363"},"modified":"2026-04-24T10:38:06","modified_gmt":"2026-04-24T09:38:06","slug":"redirect-after-login","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/wordpress\/login-logout\/redirect-after-login","title":{"rendered":"Redirect after login"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Using built in WordPress redirect_to functionality<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">On a page you want to redirect back to after login<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>      $redirect_to = home_url(add_query_arg(&#91;], $_SERVER&#91;'REQUEST_URI']));\n      wp_redirect(wp_login_url($redirect_to));\n      exit;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">If you&#8217;re placing the login form of your home page (or some other page to the default)<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Redirect wp-login.php<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/******************************************************************\n\/\/********** REDIRECT WP LOGIN PAGE TO OUR SITE HOME PAGE **********\n\/\/******************************************************************\n\/\/Fires page : \/wp-login.php\n\/\/This method keeps the wp-login functionality working, such as redirect_to, but causes the wp-login.php page to redirect to your chosen page with your login form\nadd_action('login_init', 'ValeBlockWpLoginPage');\nfunction ValeBlockWpLoginPage()\n{\n  $action = $_REQUEST&#91;'action'] ?? 'login';\n  $method = $_SERVER&#91;'REQUEST_METHOD'] ?? 'GET';\n\n  $allowed_actions =\n  &#91;\n    'logout',\n    'lostpassword',\n    'rp',\n    'resetpass',\n    'register',\n    'postpass',\n    'confirmaction',\n    'reauth',\n  ];\n\n  \/\/Allow actual login POSTs\n  if ($method === 'POST' &amp;&amp; $action === 'login')\n    return;\n\n  \/\/Allow other core login-related actions\n  if (in_array($action, $allowed_actions, true))\n    return;\n\n  \/\/Only block viewing the default login screen\n  if ($method === 'GET' &amp;&amp; $action === 'login')\n  {\n    $redirect_to = '';\n\n    if (!empty($_GET&#91;'redirect_to']))\n    {\n      $redirect_to = wp_validate_redirect(\n        wp_unslash($_GET&#91;'redirect_to']),\n        ''\n      );\n    }\n\n    $target = home_url('\/');      \/\/&lt;&lt;&lt;&lt;&lt;&lt;The page with your login form on it\n\n    if ($redirect_to !== '')\n    {\n      $target = add_query_arg(\n      &#91;\n        'redirect_to' =&gt; rawurlencode($redirect_to)\n      ], $target);\n    }\n\n    wp_safe_redirect($target);\n    exit;\n  }\n}\n<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">On a page you want to redirect back to after login<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>  $target = home_url(add_query_arg(&#91;], $_SERVER&#91;'REQUEST_URI']));\n  $login_url = add_query_arg(\n  &#91;\n    'redirect_to' =&gt; rawurlencode($target)\n  ], home_url('\/')\n  );\n  wp_safe_redirect($login_url);\n  exit;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Catch for page with your login form<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/********** TEMPLATE PAGE ABOUT TO LOAD **********\nadd_action(\"template_redirect\", 'mysite_template_redirect');\nfunction mysite_template_redirect()\n{ \n\n  if ( \n    (is_user_logged_in()) &amp;&amp; \n    (parse_url($_SERVER&#91;\"REQUEST_URI\"], PHP_URL_PATH) == '\/')  &amp;&amp;    \/\/&lt;&lt;&lt;&lt;Login page\n    (!(str_contains($_SERVER&#91;\"REQUEST_URI\"], 'elementor-preview')))   \/\/Don't get in the way of Elementor page editor previewing\n  )\n  {\n    $redirect_to = home_url('\/somepage');   \/\/&lt;&lt;&lt;Default page you want to  redirect to after login\n\n    \/\/Check for redirect to previous page after login\n    if (!empty($_GET&#91;'redirect_to']))\n    {\n      $redirect_to = wp_validate_redirect(\n        wp_unslash($_GET&#91;'redirect_to']),\n        home_url('\/')\n      );\n    }\n\n    wp_redirect($redirect_to);\n    die();\n  }\n\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Using built in WordPress redirect_to functionality On a page you want to redirect back to after login If you&#8217;re placing the login form of your home page (or some other page to the default) Redirect wp-login.php On a page you want to redirect back to after login Catch for page with your login form<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[228],"tags":[],"class_list":["post-5363","post","type-post","status-publish","format-standard","hentry","category-login-logout"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/5363","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=5363"}],"version-history":[{"count":6,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/5363\/revisions"}],"predecessor-version":[{"id":5387,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/5363\/revisions\/5387"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=5363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=5363"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=5363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}