You have to hook in earlier, I would probably use template_redirect
:
add_action( 'template_redirect', 'wpse_74577_redirect' );
function wpse_74577_redirect()
{
// the code from your question
}
Related Posts:
- How can I redirect to my custom page without loading the Dashboard itself?
- Set a variable to using in redirected page
- if is_page() is not working with wp_redirect
- How to see the dashboard of a downloaded wordpress website?
- What is the difference between $paged and $page?
- multi page password protection
- Create a “Dummy” parent page for a hierarchy in page listing?
- Custom SQL Query for WordPress page
- Change admin startpage to Pages-page?
- How restrict page for users logged? [closed]
- Only allow new subpages to be created
- Hide Visibility Option From WordPress Publish Metabox
- WordPress 4 and wp_editor() Not Showing Visual/Text tabs
- How to make nested page structure?
- Redirect to a page if ancestors is empty
- Get Order of Meta Box in a Page/Post
- Check to see if page exists problems
- Modify Page Title before output via wp_page_list
- List Child Pages of Parent Parent Page (Child pages from Grand Parent)
- Custom /page/2/ template (different from index.php)
- How to add meta boxes to Pages
- Front Page not showing on Pages
- WordPress pages with hierarchy
- Missing parent page attribute
- Hiding Page by Title from Editing
- Two Search pages, One search form
- Why doesnt ONE of my links in the footer work properly? [closed]
- Create subpage – filter parent pages list
- Use lots of pages, or categories/posts?
- How to have custom tinyMCE buttons break onto next line when too long
- Get all pages that are published
- Add a predefined page to site
- How do i change page template in bulk?
- Determine page content based on page parent
- Weird issue with is_pages() with array
- Missing ‘Page Attributes’ in page edit
- How to show the root webpage on all subpaths
- New user Registration data imported into Page
- Could be a namespace conflict (?) with login form and register form on the same page
- The concept of non-blog content
- Loading Multiple Comments Pages via Ajax?
- Change base URL of pages created by certain users
- Remove Content edit box from ALL pages (not posts)
- What is full URL for a post?
- How to list a page tree?
- How to create a page and display only the posts with a specific custom field value?
- Remove comment section from new page
- How to have a next page for post?
- Can I change the “Home” text in the menu?
- Search child pages from a specific page parent
- Proper procedure for wp_insert_post?
- Read more on pages WordPress
- Parent page shows no child content
- How to disable delete option for a specific page? [closed]
- How to create different template designs for different pages in WordPress [closed]
- Page templates in subdirectories and auto generator
- Use page template for custom $_GET content
- Get rid of “comment” field on non-blog pages [duplicate]
- Custom page only editable by one user
- Page Title repeating
- Password Protect Pages Not Working
- How to control display of page lists on sidebar that doesn’t have childrens
- wp_nav_menu: output featured image of each page listing
- Problems in creating a static site with WordPress
- Creating new page/tab in groups section not working! [closed]
- Prompting for review / reapproval of page content
- Limit the amount of main pages
- Add separate list of pages under Admin
- How to transfer a site to hosting?
- Add custom action to the page listing screen
- How to list pages from an author?
- Add the same content to multiple pages
- Cannot update page – with too many characters?
- Share a folder with files
- Front Page post mirroring revisions to other pages, constantly updating itself
- Is it possible to embedd this web app to my website [duplicate]
- Using external DB within wordpress
- Microsoft Security Essentials is blocking my WordPress website in IE11
- Author information on all pages
- WordPress Pages “allow comments” meta option can’t be checked
- Loading internal page
- Firing page_publish where page is child page
- Add role and edit page capabilities
- Add text to the bottom of particular pages
- Access wordpress pages using a self signed shared ssl
- Google indexed pages for one page site
- WordPress, page 2 shows the same posts as page 1
- How to exclude page in pages module
- get id custom tables on current page
- Parent for a single (single.php)
- Create a new page for each form selection
- pagination does not work with query_posts. Help! [duplicate]
- Subpage Conditional
- Archive with specific keyword by category sorted by date
- How to pass the Querystring in pages?
- I have over 3000 child pages – but I need a different main page
- How to enable automatic search results in WordPress
- How to make a conditional statement that checks if the page is the child of a certain page?
- Edit the text location of a form
- Fresh WordPress installation not able to simply show page content