{"id":5052,"date":"2018-04-11T17:16:06","date_gmt":"2018-04-11T21:16:06","guid":{"rendered":"https:\/\/themespress.ca\/?p=5052"},"modified":"2022-10-01T14:35:42","modified_gmt":"2022-10-01T18:35:42","slug":"wordpress-redirecting-a-private-page-to-customer-account-page","status":"publish","type":"post","link":"https:\/\/themespress.ca\/en\/wordpress-redirecting-a-private-page-to-customer-account-page\/","title":{"rendered":"WordPress: Redirecting a Private Page to Customer Account Page"},"content":{"rendered":"<div class=\"wpb-content-wrapper\"><div class=\"vc_row wpb_row vc_row-fluid\"><div class=\"wpb_column vc_column_container vc_col-sm-12\"><div class=\"vc_column-inner\"><div class=\"wpb_wrapper\">\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>With this tutorial, we will create a function <strong>that will redirect a private page<\/strong>. By default all private pages are password protected and limited to WordPress users. If non-connected users try to access it, it redirects to a 404.<\/p>\n<p>While we want to keep the same default conditions when we&#8217;re testing our page privately, we want to redirect it to our homepage.<\/p>\n<p>With the new function that we need to add into our <strong>functions.php file<\/strong>, we need to validate <strong>two conditions<\/strong>: first if we land on the 404 and then we validate that our query is valid for a private page.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<h2 style=\"text-align: left\" class=\"vc_custom_heading vc_do_custom_heading\" >WordPress: Redirect Private Page to Customer Account Page<\/h2><div id=\"ts-enlighterjs-container-3224990\" class=\"ts-enlighterjs-container-single-enabled  \" style=\"width: 100%;  margin-top: 0px; margin-bottom: 0px;\" data-enlighter-doubleclick=\"true\" data-enlighter-windowbutton=\"true\" data-enlighter-windowtext=\"New Window\" data-enlighter-rawbutton=\"true\" data-enlighter-rawtext=\"RAW Code\" data-enlighter-infobutton=\"false\" data-enlighter-infotext=\"EnlighterJS\" data-enlighter-indent=\"2\"><pre id=\"ts-enlighterjs-pre-3224990\" class=\"\" style=\"white-space: pre-wrap; height: 100%; margin: 0; padding: 0;\" data-enlighter-language=\"php\" data-enlighter-theme=\"enlighter\" data-enlighter-group=\"\" data-enlighter-title=\"\" data-enlighter-linenumbers=\"true\" data-enlighter-lineoffset=\"1\" data-enlighter-highlight=\"\">\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/ Redirect private page  \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\n\nadd_action(&#039;template_redirect&#039;, &#039;themespress_private_content_redirect_to_login&#039;, 9);\nfunction themespress_private_content_redirect_to_login() {\n  global $wp_query, $wpdb;\n  if (is_404()) {\n    $private = $wpdb-&gt;get_row($wp_query-&amp;gt;request);\n    $location = home_url();\n    if( &#039;private&#039; == $private-&gt;post_status  ) {\n      wp_safe_redirect($location);\n      exit;\n    }\n  }\n}<\/pre><\/div>\n\t<div class=\"wpb_text_column wpb_content_element\" >\n\t\t<div class=\"wpb_wrapper\">\n\t\t\t<p>As you can see, we are fulfilling the requirements and now we need to create a new variable that we&#8217;ll call <strong>$location<\/strong> and determine the redirect path. On our example, we used <strong><a href=\"https:\/\/codex.wordpress.org\/Function_Reference\/home_url\" target=\"_blank\" rel=\"noopener\">home_url()<\/a><\/strong>.<\/p>\n<p>When we validate that is a private page, we trigger the wp_safe_redirect from the <strong>$location<\/strong> variable.<\/p>\n\n\t\t<\/div>\n\t<\/div>\n<\/div><\/div><\/div><\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"With this tutorial, we will create a function that will redirect a private page. By default all private pages are password protected and limited to WordPress users. If non-connected users<div class=\"read-more\"><a class=\"btn button-secondary\" href=\"https:\/\/themespress.ca\/en\/wordpress-redirecting-a-private-page-to-customer-account-page\/\">Read More<\/a><\/div>","protected":false},"author":81,"featured_media":5057,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[283],"tags":[339,326],"class_list":["post-5052","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-paging","tag-users"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/5052","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/users\/81"}],"replies":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/comments?post=5052"}],"version-history":[{"count":0,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/posts\/5052\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media\/5057"}],"wp:attachment":[{"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/media?parent=5052"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/categories?post=5052"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/themespress.ca\/en\/wp-json\/wp\/v2\/tags?post=5052"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}