17

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', 'my_custom_redirect'); function my_custom_redirect() { if ( isset( $_POST['subscribe'] ) ) { $redirect = 'http://example.com/redirect-example-url.html'; ...


15

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(home_url()); exit(); } } add_action('template_redirect', 'redirect_to_home');


12

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 function. If you hook in early enough to send headers and late enough for the query to be set up you can check if the content contains your shortcode and ...


10

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() ob_start() in the main plugin file on top. And make sure you have added exit() function after wp_redirect($url) Like this: $url = 'http://example.com'; ...


10

You have to use wp_redirect() before get_header() Then it will not show header error.


9

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 form tag. Now you can verify your nonce in the backend where you will process your form. Use the following code to verify the nonce you just created above. if(...


8

The is_login_page() function is taken from here function is_login_page() { if ( $GLOBALS['pagenow'] === 'wp-login.php' && ! empty( $_REQUEST['action'] ) && $_REQUEST['action'] === 'register' ) return true; return false; } function my_redirect() { //if you have the page id of landing. I would tell you to use if( is_page(...


8

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 http, you can never be sure that your visitors are shown the same page you are providing from your server. Point 2: Search engines consider http & https ...


7

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) this is a string which represents the name of the nonce. If you leave this field blank WordPress will default to “_wpnonce” Example: Frontend: <form ...


7

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 ) { if( $user->ID == 6 ) { //If user ID is 6, redirect to home return get_home_url(); } else { //If user ID is not 6, leave ...


6

The AJAX request runs in the background. Redirects here do not affect the main page. And 302 is not an error, it is just a status code. Your AJAX response should return either the URL and the status code to the calling page or just a number like 1. Then you handle the redirect in the calling page: jQuery( document ).ready( function( $ ) { var url = '&...


6

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 fires only when no content has been found for an URL. That hook is 404_template. This is called when WP trys to include the 404 template from your theme (or the ...


5

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 customize.php to a theme options page and the one from Michael's original function. function admin_redirects() { global $pagenow; /* Redirect Customizer ...


5

Your method is called too late. I don't know how you call this method but you need to run it before output is sent to the browser-- usually that means before get_header(). There are a number of hooks that can be used. For example (from https://wordpress.stackexchange.com/a/131210/21376): add_action( 'template_redirect', function() { if (is_single()) ...


5

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

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() does not work after headers are sent. Please make sure you are redirecting before headers. So you can hook this function on template_redirect and put this in ...


5

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 already been dispatched to the browser - that is to say, when the server should only be generating the body of the response. This often happens in one of two ways: ...


4

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 following code to your functions.php file to change your site's login URL to point to your custom login page: function my_login_page( $login_url, $redirect ) { ...


4

You need to wrap the first three || conditions in additional parentheses so they are evaluated first BEFORE the last && condition: Psuedocode example: here here ↓ ↓ if ( (condition || condtion || condition) && ! condition ) { //do business logic... } This now ...


4

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 the redirected-to page, you can conditionally display a message: <?php if ( filter_input( INPUT_GET, 'my-form' ) === 'success' ) : ?> Congrats! <...


4

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 = 'http://old.example.com' . $path; $url_escaped = esc_url( $url ); $status = 301; $message = "Moved to <a href='$url_escaped'&...


4

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 that is how HTTP response is structured. Hypothetically it could work in template if you make sure it fires before any output. Practically it is a normal ...


4

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 clearly a matter of permissions and capabilities, and it turns out that the whole thing is because wp_xx_user_roles option. When avoiding the regular registration ...


4

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 behaviour for this specific post, delete the _wp_old_slug post meta entry from the database for that post. If you want to prevent this behaviour for all posts, prevent ...


4

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 hook fires and redirects them to themes.php?page=theme_setup So then they arrive on themes.php?page=theme_setup and the init hook fires and redirects them to ...


4

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 generated. You need something like the following in your functions.php file: function hook_wp_loaded_require_login() { $uri = ( isset( $_SERVER['...


4

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 specific URL,such as "wp_redirect( 'http://www.wpcoke.com' )"; exit(); } add_action( 'user_register', 'auto_login_new_user' ); Taken from this SO answer: ...


3

Problem is that the function you use run after http header are sent, so it can't redirect. You have to use another way. One method can be intercept the global menu variable and add a new menu item with all properties: add_action( 'admin_menu', 'register_web_menu_page', 999); function register_web_menu_page () { global $menu; $menu[9] = array ( '...


3

You could add something like this in your htaccess: RewriteCond %{QUERY_STRING} ^(.*)$ RewriteRule ^redirect\.html$ /%1 [L,R=301] EDIT: based on comments, you'll need some additional conditionals to sort different query strings. RewriteCond %{REQUEST_URI} ^/redirect.html(.*)$ RewriteCond %{QUERY_STRING} ^p=test$ RewriteRule (.*) /new-test/ [L,R=301] ...


3

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 */ wp_redirect();//.... } });


Only top voted, non community-wiki answers of a minimum length are eligible