If you place your if/else statement within the function you call on the init
hook, the redirect will only occur if your $var
evaluates to false. For example
function 290127_init_validate(){
if ( ! is_post_type_archive('book') ) {
return;
}
if ($var != null) {
// do something
} else {
wp_redirect('new url');
}
}
add_action("init", "290127_init_validate");
WordPress provides a number of conditional tags which can be used to provide even more granular control over when a function should run. You can use these conditional tags at the beginning of your function and return
early if they don’t evaluate to true. The example function above will only run on ‘book’ custom post type archive page.
Related Posts:
- Rewrite Slug for CPT Archive Pages to Plural Name of Slug
- Change author base slug for different roles
- How to add custom rewrite rule to .htaccess?
- Problem with add_rewrite_rule and pagination (paged and page query_vars)
- Custom Permalinks for Blog Posts Only
- How to show the same content on multiple URLs?
- Using a page template without a page
- How to seamlessly redirect between different archive and singular slugs?
- Overwrite rewrite-slug of built in post-type ‘post’
- Why does wordpress still strip my query var?
- When to call `add_rewrite_rule` for safety
- URL rewrites and pagination
- Rewrite rules goes away
- Rewrite Rule for Post “Subpage”, with pagination
- unexpected problem in url rewrite
- Need to make a php file inside theme accessible via url
- Rewrite loading custom template file but is_home() returns true, and there’s no 404
- Rewrite rule for admin-ajax.php
- Change WordPress comments url / word
- Cache issues with redis and nginx
- WordPress removes spaces in URL on pagination
- Adding more pages to author pages
- How to check current URL for endpoint in a template file?
- add_rewrite_rule not working for language specific characters
- Add query string to url and display it as normal url part /folder/
- Is it possible to remove feeds from rewrites?
- Flush Rewrite Rules on init or rest_api_init?
- WordPress .htaccess – route other URLs to another app
- Rewrite URL – insert custom variables as a directory path
- How to properly rewrite url by custom var
- Can’t add external rewrites
- How to use URL structure to call PHP code
- Can I have /blog and /foo and /bar all point to the same blog?
- Append a value to a certain page’s URL before page load
- How do I retain url parameters the entire time a user browses my site?
- Different URL rewrite rules loading the same page
- Cannot access wp-admin without trailing slash – .htaccess configuration for WordPress behind a nginx proxy
- Wildcard forward all posts and pages with few exceptions
- Help with Rewrite Rule for Custom Post Type with Query String
- Create Dynamic SEO Friendly URL for Virtual Page
- add_rewrite_rule with query variables
- WordPress author url to alias domain url? domain.com/author/{username}/
- Replace specific middle part of url
- Rewriting a subdomain page on one WordPress installation to a parent domain on another
- How to redirect large amount of URLs?
- How to achieve my custom post url structure?
- How to create a front end user profile with a friendly permalink and a 404 trigger
- Rewrite URL for single category
- Pretty URLs and rewrite
- ngnix Url rewrite doesn’t work
- Understanding the query string after .htaccess rewrite
- Rewrites rules disappear after a while
- Is it possible to create dynamic endpoint URLs?
- How to get default permalink link text?
- Using WP Rewrite to rewrite custom urls in this scenario
- Localize URL bases
- How can i ensure that SQL statements are not displayed if an enduser types the wrong variable name in the URL
- How to redirect Webp files?
- Create rewrite rule for subpage
- WordPress simple Url Rewrite
- Proper way to set up rewrite with Wp
- Add rel=canonical to only one page
- Change /search to /somethingelse
- Why is a rewrite rule in wordpress prevent pagination?
- Using custom url parameters in a page
- Rewrite custom taxonomy slug – but only parent items
- Rewrite parameters as Url for SEO
- Home page url rewriting WordPress
- Custom post type rewrite with categories and static pages
- Custom folder with PDF files (not in WP Library) – Create url for each file
- WP returns to domain root instead of siteurl
- SEF add_rewrite_rule & add_rewrite_tag not working
- Rewrite WordPress Url from root to subfolder without moving files
- How to pass username in url
- Problem building an url with argument
- How do I rewrite URL that has custom parameter
- Rewrite rule for post as a child of a CPT
- Url rewrite, add rule, redirect as POST request?
- Broken page redirect
- Add query arg for pretty permalinks in wordpress way
- Homepage cannot be found
- How to add 2 variable rewrite rule?
- Get custom posts by segment of the URL
- Add ReWrite from Old Pages to WordPress (remove .php)
- Remove trailing slash
- Rewrite Url to a SEO-friendly format
- Use Parent Pages for URL Structure without Landing Page
- add_rewrite Rule
- How to show my application as referral in WordPress Stats
- Fully mask *all* traces of WordPress installation subdirectory?
- Appending parameters to WordPress query string in backend
- rewrite rule of post when accessed from archives pages
- Help to cleanup this code
- Rewrite Question
- Change the search URL
- Listing Authors – URL Formats [duplicate]
- Add rewrite endpoint and .htaccess
- Remove url parameter using wordpress
- Change the slug ( url ) to a sequence number starting from 0 in the custom record type?
- Change url site.it/wordpress