How to 301 private posts rather than 404?

Sorry guys, I found my answer: add_action(‘wp’,’redirect_stuffs’, 0); function redirect_stuffs(){ global $wpdb; if ($wpdb->last_result[0]->post_status == “private” && !is_admin() ): wp_redirect( home_url(), 301 ); exit(); endif; } Posts/Pages are removed from the sitemaps, but the page still shows up on the site so that it can get 301’d.

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 function. If you hook in early enough to send headers and late enough for the query to be set up you can check … Read more

Where does wp_redirect need to be placed to make it work?

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 … Read more

wp_redirect not working after submitting form

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 … Read more

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 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(wp_verify_nonce($_REQUEST[‘name_of_your_nonce_field’], … Read more

wp_redirect() function is not working

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 ); wp_redirect($url); exit();