19
votes
Accepted
wp_redirect() - headers already sent
Found the answer (via)
Instead of using the function I added an action to "wp_loaded", that makes sure that it gets loaded before any headers are sended.
<?php
add_action ('wp_loaded', '...
16
votes
Redirect page URL to home URL without using a plugin
The correct way to do this is using the template_redirect hook by adding a function to your functions.php:
function redirect_to_home() {
if(!is_admin() && is_page('2')) {
wp_redirect(...
12
votes
Accepted
How can I put a wp_redirect into a shortcode?
Shortcode functions are only called when the content of the visual editor is processed and displayed, so nothing in your shortcode function will run early enough.
Have a look at the has_shortcode ...
11
votes
wp_redirect() function is not working
I have a simple solution, please read:
If you are using wp_redirect($url) in theme files, and it is not working add ob_clean() ob_start() in your function file on top.
If using in plugin add ob_clean(...
11
votes
wp_redirect() - headers already sent
You have to use wp_redirect()
before get_header()
Then it will not show header error.
10
votes
How to use nonce with front end submission form?
Use the following code inside just before tag on your front end code.
wp_nonce_field('name_of_your_action', 'name_of_your_nonce_field');
The above code will generate two hidden inputs inside your ...
8
votes
Accepted
How to redirect a specific user after log-in?
You need to use the login_redirect filter returning the redirect location:
add_filter( 'login_redirect', 'redirect_to_home', 10, 3 );
function redirect_to_home( $redirect_to, $request, $user ) {
...
8
votes
Accepted
Redirect if not logged in?
The is_login_page() function is taken from here
function is_login_page() {
if ( $GLOBALS['pagenow'] === 'wp-login.php' && ! empty( $_REQUEST['action'] ) && $_REQUEST['action'] === ...
8
votes
Accepted
Login to wp-admin "redirect_to" points to wrong URL after migration
I had the same issue. To fix it I had to make some changes to the database. Use phpMyAdmin or just log directly into the database and look at your wp_options table. Check the following two fields: ...
8
votes
How to use nonce with front end submission form?
To add a nonce to a form you can use the following function:
wp_nonce_field($action, $name, $referer, $echo)
$action = (optional) this is a string which represents the action name
$name = (optional) ...
8
votes
Accepted
htaccess https redirect from www to non-www
Before I give you the CODE, let me explain a few points:
Point 1:
It's better if you only allow https links. Mixing http & https for the same content breaks the security added by https. With ...
6
votes
Admin Page Redirect
Michael's solution appears to be intended for use inside a class, so for anyone wanting a standalone function which will work directly in functions.php, the example below includes a redirect from ...
6
votes
Accepted
How to redirect only if page doesn't exists
You can detect non-existent pages only with WordPress. Normal URLs don't point to a physical resource, their path is mapped internally to database content instead.
That means you need a WP hook that ...
5
votes
Wordpress HTTPS redirect loop
I'v added $_SERVER['HTTPS'] = 'on'; to my wp-config.php as seen on WordPress wp-admin https redirect loop and it worked perfectly!
5
votes
Accepted
Why does not this wp_redirect work in is_single()?
is_singular() only accept post_type, use is_single('2024') for specific post.
if (is_single(2024)) {
wp_redirect('http://mysitedomain.com/../rugby/');
exit();
}
Update:
Also wp_redirect() ...
5
votes
Accepted
'Headers already sent' Error When Redirecting from add_submenu_page() Callback
Background
The infamous "Headers already sent" error rears it's ugly head in circumstances where something attempts to modify the HTTP headers for the server's response after they have ...
5
votes
Accepted
wp_redirect() does not work in custom template file
Redirects are performed by outputting HTTP headers, wp_redirect() just adds some bits on top of it for flexibility.
Headers are only ever meant to be used before any and all output to a page, since ...
5
votes
Accepted
How to auto login after registration?
Have you tried this?
function auto_login_new_user( $user_id ) {
wp_set_current_user($user_id);
wp_set_auth_cookie($user_id);
wp_redirect( home_url() ); // You can change home_url() to the ...
4
votes
force login and redirect to custom login page
If you're willing to try another plugin, I suggest using Force Login. It's a simple lightweight plugin that will require all visitors to login.
Once the plugin is installed and activated, add the ...
4
votes
is_user_logged_in not working to redirect only logged out users
You need to wrap the first three || conditions in additional parentheses so they are evaluated first BEFORE the last && condition:
Psuedocode example:
here ...
4
votes
Accepted
Passing User Messages to Another Page
A standard approach is to add a query parameter to the location header (redirect), for example:
$redirect = add_query_arg( 'my-form', 'success', $redirect );
wp_redirect( $redirect );
exit;
Then on ...
4
votes
Accepted
Can I trust user input in wp_redirect()?
You can never trust user input. Always prepare a value that you want use in your own code. Example:
$path = filter_input( INPUT_SERVER, 'REQUEST_URI', FILTER_SANITIZE_URL );
if ( $path )
{
$url ...
4
votes
Accepted
Site's admin created with wpmu_create_blog accessible only on 2nd attempt
TLDR
user_roles option is broken, it can be fixed by updating the db, see the solution below.
Who & Why
I was able to recreate your issue and analyze it. As you can see in the error it is ...
4
votes
Accepted
Stop unwanted WP redirection to new url
WordPress uses a function called wp_old_slug_redirect() to find out if you're looking for a post whose slug was recently changed and redirect you to its new home.
If you want to prevent this ...
4
votes
Why does my wp_safe_redirect output the ERR_TOO_MANY_REDIRECTS error?
Because you're in an infinite loop.
When the user visits the site, init fires, and they get redirected to themes.php?page=theme_setup
Then they arrive on themes.php?page=theme_setup and the init ...
4
votes
Accepted
"Cannot modify header information" means I can't use wp_redirect
As Milo stated, you can't send header information after PHP has begun sending request response body.
Redirects belong in a function hooked into wp_loaded to ensure they run before the request body is ...
4
votes
Redirect a page based on last word in slug
There are many solutions for that..
1. .htaccess rules
You can put some redirect rules in your .htaccess file:
RewriteRule ^(.*)\-staff/$ /staff/? [L,R=301]
// some other rules
2. Using WordPress ...
3
votes
wp_redirect() - headers already sent
add_action('template_redirect', function(){
if(isset($_POST['subscriptio'])){// make this condition such that it only matches when a registraiotn form get submitted
/**
* do your stuff here
*/
...
3
votes
wp_redirect() - headers already sent
you can also do this
Instead of the below line
wp_redirect("$url");
write
echo("<script>location.href = '".$url."'</script>");
or
<?php <script><?php echo("location.href =...
3
votes
How do I redirect /search/ to ?s=
In your .htaccess add the following rule in between the <IfModule mod_rewrite.c> tag:
RewriteCond %{QUERY_STRING} (^|&)(.*)
RewriteRule ^(.*)$ /?s=%2 [R=301,L]
Based off of the default ....
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
wp-redirect × 396redirect × 153
login × 32
template-redirect × 21
url-rewriting × 20
headers × 18
php × 17
plugin-development × 17
functions × 17
urls × 17
plugins × 16
htaccess × 16
custom-post-types × 13
wp-admin × 13
user-registration × 13
permalinks × 12
multisite × 12
wp-login-form × 12
hooks × 10
actions × 9
404-error × 8
categories × 7
shortcode × 7
pages × 7
users × 7