You can’t. If you wish to redirect a user by PHP, you should set the response header. If you output any data, you will not be able to, and you will get a Header already sent
error.
Look at this line:
wp_redirect( home_url('/?page_id=7') );
exit;
echo $result;
die();
When you use exit
( which is the same as die()
), you are terminating the script. The next lines will never be executed. So my suggestion is to either use AJAX to redirect, or change your form to something like this:
<form action="<?php echo admin_url ( 'admin-ajax.php' ); ?>">
<!-- This is to set the action of your query args -->
<input type="hidden" name="action" value="search"/>
<!-- Rest of your form here -->
</form>
Then, remove the line after exit
and also the AJAX script, if you aren’t relying on it for something important.
Related Posts:
- Ajax URLs without #!, how to prevent falling into single.php on load or reload?
- Need help with AJAX login to call php in functions.php to handle redirects based on user cap (role)
- Submitting my form to the database and then redirecting to payment site
- How do I make a redirect in PHP?
- Adding “Remember Me” in custom login
- Retrieve POST data from AJAX call
- The correct method to pass query vars in AJAX using ajaxurl
- Using wp_send_json to return HTML
- PHP – redirect https to http and www to non-www
- WordPress Ajax always returns a 404 error
- Can i check if user is doing any ajax request?
- How to use wp_localize_script in a WordPress page other than functions.php?
- Real time Duplicate title check
- Ajax template: how to handle head section
- Update WordPress Custom Field with AJAX on cached page
- Is Auto Post recognition a WordPress feature? How to turn it off?
- Is the regular ajax request method safe or I should use admin-ajax.php?
- AJAX in WordPress, sending coords data to MySQL and show after into map
- Ajax form submit within a Post Metabox
- PHP If user is logged in & on home page redirect
- Returning a value from a PHP page
- Ajax $wpdb not returning table data
- How to run JS, PHP and etc. inside WP post?
- Trying to implement Ajax comments in WordPress, getting WP error
- Can’t search posts using WP_QUERY inside AJAX Function
- Woocommerce add to cart quantity buttons with AJAX
- Why is $_REQUEST an empty array in admin-ajax.php?
- AJAX request status 200 but no actual “response”
- How to get original custom taxonomy slug after the slug has been rewritten?
- Save Search System
- dynamic dependent select dropdown
- Are nonces in WP REST API optional by default?
- How do I display the main domain for my WordPress install?
- Plugins not working on AJAX requests
- Problem with login form
- How do I Import / Upload Files with jQuery AJAX?
- Set Cache-Control header for 301 redirects
- wp ajax return 0
- Moving code from theme header to functions.php
- WordPress plugin: render only page content using ajax call
- Showing latest post without 301 redirect
- AJAX button action in foreach
- HTML escaping data with ajax requests
- Ajax button not working
- Function won’t run onclick using Ajax
- How to deal with too many $_POST variable conditions from ajax request at backend? [closed]
- Simple AJAX notification when the new post is added to the database
- Using Argument from Function to Re-Direct Visitor (WordPress)
- Is it possible to create new user from external form using REST API?
- Get related product based on subcategory selected from category id with ID 142
- When echo follow Ajax button inside Ajax pagination template not working?
- jQuery Ajax PHP function call returning [object Object]
- WP_Query with AJAX returning empty array
- How to get post category list as select in front-end?
- Is there a hack for using is_page() within the function.php file?
- Manually Create PHP Page For A Redirect
- Submit form to db
- Change Query Arguments (filter) with jQuery/Ajax or PHP?
- Can not call .php files after switching to %postname%
- WordPress Conditional / Multiple Taxonomy Query
- Using Ajax to submit a form, and run a SQL Select query based on user input from the form
- Disclaimer that will show every refresh of the page
- AJAX function running TWICE with WordPress admin-ajax.php
- Add more user roles to a PHP logout redirect function [closed]
- Tracking Visitor LatLng with WordPress using JS, PHP. How to put data which was extract using JS into DB
- WordPress Search Ajax + Isotope + InfiniteScroll
- Failed to load resource: the server responded with a status of 500 (Internal Server Error) through wp_admin_ajax.php
- 404 on old link after changing post url
- How to call my PHP function with AJAX ? wp_query
- Can’t pass var from php wp_ajax into ajax script : result undefined or null
- Ajax filter with loadmore button
- Admin-ajax.php 404 error
- How to pass values from one function to the other using an array variable
- get_the_author_meta( ‘ID’ ) just return 1
- custom mailchimp form using HTTP API
- Infinite loop when logging out using custom login form
- $.ajax results in 403 forbidden
- WordPress is setting from header wrongly in wp_mail
- WordPress ajax filter returning all posts when it should be filtering by category
- ajaxurl usage for a custom function
- WordPress Redirect / Add_Rewrite_Rule – Non Index.php Page
- Prepared DB Query from _POST array
- Pass form attributes to another php template in wordpress
- Custom HTML form using PHP – help with ajax/username validation
- AJAX Returning Way Too Many Posts
- WordPress AJAX load post metadata in modal
- redirect 301 with special character like WIX site “#!”
- wordpress ajax return 0
- Ajax call URL 404’ing when pushed to staging server
- Modify category archive page loop on functions.php
- Run insert if no entry otherwise run update with ajax
- Adding instant search to wordpress page
- Get permalink for a post from inside WordPress and route to a related site
- AJAX & PHP | Call a specific PHP function from a PHP file via AJAX?
- Load php function with AJAX on modal window
- How to use multiple 404 Error Pages in WordPress
- notify users when changed database
- WordPress Query -> Ajax Query
- how to display tables via ajax
- Redirect loop upon installation of my plugin