22

Two things wrong here: Don't use $post->guid as an url You must exit() after using wp_redirect() (see the Codex) wp_redirect() does not exit automatically and should almost always be followed by exit. To redirect to your new post's page: //..... code as in question $post_id = wp_insert_post($new_post); $url = get_permalink( $post_id ); ...


22

/** * Redirect admin pages. * * Redirect specific admin page to another specific admin page. * * @return void * @author Michael Ecklund * */ function disallowed_admin_pages() { global $pagenow; # Check current admin page. if ( $pagenow == 'edit.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'page' ) { ...


15

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'; ...


14

Once you're in the template it's too late, as headers have already been sent. You have to hook earlier in the request to check, like the template redirect hook: add_action( 'template_redirect', 'wpse52455_redirect' ); function wpse52455_redirect(){ // do your check and call wp_redirect here } Note that this will get called on every request, so you ...


12

You can only use wp_redirect before content is sent to the browser. If you were to enable php debugging you'd see a "headers already sent" error due to get_header() on the first line. Rather than process the form in the template, you can hook an earlier action, like wp_loaded, and save some queries to the db if you're just going to redirect away. EDIT, ...


11

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 ...


9

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'; ...


8

As Otto pointed out, you're sending data to the browser before calling wp_redirect(). get_header() will output the page's HTML <head> You're printing the entire form to the page before processing it. To fix the "headers already sent" issue, you need to move all of your form processing from the bottom of the page to the top of the page. If you need ...


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(...


7

I am not sure if this will help... but I found that I had some code in a template and I was starting with get_header() in this way: <?php /** * .. Template comments */ get_header(); if(...) { ... if(...) { ... wp_redirect($url); exit(); } } ?> and was getting the same issue of header previously sent... What I did ...


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 ...


7

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');


7

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 ...


6

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(...


6

According to the Codex page for wp_redirect(), you should follow your wp_redirect() calls with exit. add_action( 'wp_login', 'redirect_on_login' ); // hook failed login function redirect_on_login() { $referrer = $_SERVER['HTTP_REFERER']; $homepage = get_option('siteurl'); if (strstr($referrer, 'incorrect')) { wp_redirect( $homepage ); ...


6

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


5

You should hook into the user_register action. I did something similar on a recent site (not involving having to fill out profile fields, but involving having to renew a membership). I will propose a multi-part solution (let me know if any of it is confusing, it's been a long day and I might not explain it perfectly). EDIT: Upon thinking about it further, ...


5

Never ever use the post GUID value, it does not have to match the real URL of the post. http://codex.wordpress.org/Changing_The_Site_URL#Important_GUID_Note wp_redirect( get_permalink( $post_id ) ); exit(); // always exit Also, make sure wp_redirect is not plugged by something else which prevents it from doing its job correctly. Deactivate all plugins and ...


5

Wordpress provides a way to prevent the header HTML from being rendered, by appending &noheader=true to the url. That will cause the header HTML to wait for you to call it manually, so that you can do a redirect before that. To later render the header HTML from your page, you'll have to use this: if ( isset($_GET['noheader']) ) { require_once(...


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

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

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 ...


4

Moving get_header(); to the bottom of that code should fix the problem. Your code will execute before any headers are sent and the redirect will work. // ... wp_redirect( get_permalink($pid) ); exit(); //insert taxonomies } get_header(); ?> I assume there is more code on the page below what you posted? If not I don't see the need for get_header() at ...


4

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 = '&...


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

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

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['...


3

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 ...


3

I solved this issue myself, it was quite easy and I'm surprised nobody else supplied an answer. Having said that, the solution is to add a few lines of code that check what the author ID is of the profile you're viewing and compare it to the ID of the currently logged in user. This code goes at the top of members/single/profile.php <?php // Global $...


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