• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

WP SITES

2784

Original Genesis Tutorials & 6000+ Guaranteed Code

Snippets

  • Consultation
  • Full Access
  • Log in

How To Use A 301 Redirect In Your Functions File

WordPress includes some really handy pluggable functions one of which is

wp_redirect()

This function can easily be written into a custom function with a conditional tag and hook to create a 301 redirect.

Here’s a working example which redirects from a page with the slug ‘shop’ to the blog page:

add_action( 'template_redirect', 'redirect_to_blog_page' );
function redirect_to_blog_page() {
    if ( is_page('shop') && ! is_user_logged_in() ) {
      wp_redirect( 'http://www.example.dev/blog/', 301 ); 
	  exit;
    }
}

You can also use code like this to redirect to the home page.

add_action( 'template_redirect', 'redirect_to_home_page' );
function redirect_to_home_page() {
    if ( is_page('shop') && ! is_user_logged_in() ) {
     wp_redirect( home_url(), 301 );
      exit;
    }
}

You don’t need to use a 301 redirect with this function if you don’t want. You can use the wp_redirect() function for any type of redirect.

More Ways To Create Redirects

  • How To Redirect 1 Page
  • Website Redirection – Create a 301 Permanent HTTP Redirect in htaccess
  • Redirect User To Specific Page Based On Their Capability After Logging In
  • 5 Ways To Redirect Attachment Pages To The Parent Post URL
  • Redirect Old Domains URL’s to New Domains URL’s

Reader Interactions

Comments

  1. Chad says

    January 26, 2014 at 11:01 am

    Brad, you post some of the most useful, creative stuff.
    Thanks got sharing!

    Log in to Reply
    • Brad Dalton says

      January 26, 2014 at 12:43 pm

      Thanks Chad

      Log in to Reply

Leave a Reply Cancel reply

You must be logged in to post a comment.

Primary Sidebar

Code written by Brad Dalton specialist for Genesis, WooCommerce & WordPress theme customization. Read More…

Advertise · WPEngine · Genesis · Log in

  • Access Problems
  • Account Details
  • Consulting
  • Tags