WordPress redirect after successful form validation and submit - wordpress

I have a custom page template with a form in page-report.php.
I do validation on it, so I need the action to lead to the same form, but I need to redirect to a different page on successful validation.
wp_redirect() is not working, because is spitting out the header() function after the output was started already.
if($_POST['report'])
{
if($validator->ValidateForm())
{
wp_redirect('http://thankyou') // redirect
}
}
I cannot use ob_start() and ob_flush() because the header is not included in this page template.
I tried to put a function in functions.php :
add_action('get_header','redirect_to');
function redirect_to($page){
if($page)
{
wp_redirect('http://www.google.com');
}
}
But that works only if I don't have the conditional if().
If I use it, the wp_redirect() is being spat out after the output was started.
What is my best approach to do this?
Thanks.

I think you have to use the save_post hook:
do_action('save_post', 'custom_add_save');
function custom_add_save($postID){
// called after a post or page is saved
if($_POST['report']) {
if($validator->ValidateForm())
{
wp_redirect('http://thankyou') // redirect
}
}
Also you could just try using a plugin instead of your own code...Gravity Forms and Contact form 7 both work well.
}

I got it...
Since I was doing everything from inside an admin page, the header was fired up before the wp_redirect() as it was explained in the question.
So I ended up making a new function at the top:
add_action('admin_init','redirect_to');
function redirect_to()
{
if ( isset($_REQUEST['action']) && 'adduser' == $_REQUEST['action'] ) {
wp_redirect($redirect);
die();
}
}
}
That is making sure that the redirect_to() function will be fired up before the header (on admin_init). Maybe not the most elegant solution, but it works perfect.

So in the case anybody is looking for "Redirect after post" in wordpress, this is how you do it:
wp_redirect($_SERVER['REQUEST_URI']);

Try this
if( $_POST['report'] ) {
if( $validator->ValidateForm() ) {
header( 'Location: http://thankyou' ) ;
}
}

Related

WP redirect in late action

So I hooked into simple action after some point in plugin
function redirect_on_complete($user_id, $lesson_id) {
$next_prev_lessons = sensei_get_prev_next_lessons($lesson_id);
if ( !empty($next_prev_lessons['next_lesson']) ) {
wp_redirect( get_permalink($next_prev_lessons['next_lesson']) );
}
}
add_action('sensei_user_lesson_end','redirect_on_complete',10,2);
How can i implement redirection here? sensei_user_lesson_end is firing to late when some content is alraedy loaded...
I would like to do this without Javascript.
No way to do this here, only using JS redirection, so what I have done is opened HTML and inserted JS redirect.

Give access to only two (/home, /inbox) page for a particular user with specific role in wordpress

I want to give only two page (/home, /inbox) access to my user with role "Vendor", if user tries to access other pages than it will automatically redirect to "/inbox" page, I put the below code to achieve the functionality but after adding this to function.php, site again n again redirect and finally dies with message "Page isn't properly redirected". please suggest what is wrong with my tried code or any other solution.
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->slug,array("home","inbox"))) {
wp_safe_redirect('/inbox');
}
}
add_action('template_redirect', 'vendor_redirect');
The main issue the way I tried to get the page slug, the correct way to get the slug is "$post->post_name". also I put exit after wp_safe_redirect as well because codex suggest that:
function vendor_redirect() {
global $post;
if(current_user_can('Vendor') && !in_array($post->post_name,array("home","inbox"))) {
wp_safe_redirect(get_permalink(get_page_by_path( 'inbox' )));
exit;
}
}
add_action('template_redirect', 'vendor_redirect');

Redirect away from login page

I'm trying to find a way to prevent the login screen from showing up when user makes a mistake filling out the login form.
So, right now, I have the login form printed on a custom template. But if the users makes a mistake filling it out, it redirects them to the wordpress form to try again. I want to redirect away from that.
This is the code I had but it's not working.
add_action('login_form', 'redirect_invalid_login');
function redirect_invalid_login(){
global $error;
if($error)
header('Location: '.get_bloginfo('url').'/client-login/?message=6');
}
Now login_form is too late I believe because stuff already gets printed to the page. I haven't been able to find a hook that works. Is there a hook for this? Or perhaps a different way to do it?
Okay, so here is the solution:
add_action('login_redirect', 'redirect_login', 10, 3);
function redirect_login($redirect_to, $url, $user) {
if($user->errors['empty_password']){
wp_redirect(get_bloginfo('url').'/client-login/?message=6');
}
else if($user->errors['empty_username']){
wp_redirect(get_bloginfo('url').'/client-login/?message=7');
}
else if($user->errors['invalid_username']){
wp_redirect(get_bloginfo('url').'/client-login/?message=8');
}
else if($user->errors['incorrect_password']){
wp_redirect(get_bloginfo('url').'/client-login/?message=9');
}
else{
wp_redirect(get_bloginfo('url').'/client-login');
}
exit;
}
You can try this
add_action( 'wp_login_failed', 'redirect_invalid_login' );
function redirect_invalid_login() {
wp_redirect(get_bloginfo('url').'/client-login/?message=6');
exit;
}

Redirect after plugin activation

How do I redirect users to my plugin settings page after they activate my plugin, I tried
register_activation_hook(__FILE__,'activate_myplugin');
function activate_myplugin()
{
//create and populate DB tables
wp_redirect(plugin_setting_url);
}
but it does not work.
You should be able to do something like this:
register_activation_hook(__FILE__, 'my_plugin_activate');
add_action('admin_init', 'my_plugin_redirect');
function my_plugin_activate() {
add_option('my_plugin_do_activation_redirect', true);
}
function my_plugin_redirect() {
if (get_option('my_plugin_do_activation_redirect', false)) {
delete_option('my_plugin_do_activation_redirect');
wp_redirect(MY_PLUGIN_SETTINGS_URL);
}
}
This will redirect to option page only if that plugin is activated only without using bulk activation mode .
register_activation_hook(__FILE__, 'my_plugin_activate');
add_action('admin_init', 'my_plugin_redirect');
function my_plugin_activate() {
add_option('my_plugin_do_activation_redirect', true);
}
function my_plugin_redirect() {
if (get_option('my_plugin_do_activation_redirect', false)) {
delete_option('my_plugin_do_activation_redirect');
if(!isset($_GET['activate-multi']))
{
wp_redirect("options-general.php?page=your-plugin-option-page");
}
}
}
thanks for your code - it´s great, but only has one downside: upon bulk activation of plugins, you also get redirected to your defined redirect page - which might confuse user when deactivating/activating all plugins at once for test/debug reason. I therefore would propose the solution, to add an option to only redirect to your page on FIRST plugin activation:
register_activation_hook(__FILE__, 'my_plugin_activate');
add_action('admin_init', 'my_plugin_redirect');
function my_plugin_activate() {
add_option('myplugin_redirect_on_first_activation', 'true');
}
function my_plugin_redirect() {
if (get_option(MYPLUGIN_REDIRECT_ON_FIRST_ACTIVATION_KEY) == 'true') {
update_option(MYPLUGIN_REDIRECT_ON_FIRST_ACTIVATION_KEY, 'false');
wp_redirect(MY_PLUGIN_SETTINGS_URL);
}
}
Don't worry it's very simple.
Simply paste this code in you plugin.php file
function_activation_redirect( ) {
exit( wp_redirect( 'http://45.118.207.78/amarwp/wp-admin/admin.php?page=custompage' ) )
}
add_action( 'activated_plugin', 'funtion_activation_redirect' );
http://45.118.207.78/amarwp/wp-admin/admin.php?page=custompage
in my case this is the path of my page where I want to redirect my page.
Hello i have used bellows code redirect after plugin activation. You can use this code. It's working nicely.
register_activation_hook(__FILE__, 'nht_plugin_activate');
add_action('admin_init', 'nht_plugin_redirect');
function nht_plugin_activate() {
add_option('nht_plugin_do_activation_redirect', true);
}
function nht_plugin_redirect() {
if (get_option('nht_plugin_do_activation_redirect', false)) {
delete_option('nht_plugin_do_activation_redirect');
if(!isset($_GET['activate-multi']))
{
wp_redirect("edit.php?post_type=headline&page=news-headline");
}
}
}
nht_ is my plugin prefix & "edit.php?post_type=headline&page=news-headline" is redirect page. please replace this those.

Wordpress Plug-ins: How-to add custom URL Handles

I'm trying to write a Wordpress Plug-in but can't seem to figure out how you would modify how a URL gets handled, so for example: any requests made for:
<url>/?myplugin=<pageID>
will get handled by a function in my plug-in. I'm sure this is a very simple to do, but I'm pretty new to working with Wordpress and couldn't find it in the documentation.
In order to handle just a specific URL use the code below:
add_action('parse_request', 'my_custom_url_handler');
function my_custom_url_handler() {
if(isset($_GET['myplugin']) && $_SERVER["REQUEST_URI"] == '/custom_url') {
echo "<h1>TEST</h1>";
exit();
}
}
add_action('parse_request', 'my_custom_url_handler');
function my_custom_url_handler() {
if( isset($_GET['myplugin']) ) {
// do something
exit();
}
}
That should set you on the right direction. parse_request happens before WordPress runs any of the complicated WordPress queries used to get the posts for the current URL.

Resources