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
- WordPress, AJAX and pre_get_posts using conditional tags
- Are nonces in WP REST API optional by default?
- Change “add to cart” button color based on Woocommerce product category [closed]
- Ajax request returns ‘Array’. How to output the actual results?
- How to send Ajax request from my plugin in admin dashboard?
- Posting and image insertion problems after EasyApache4 and PHP 7 upgrade
- Ajax call return 404
- How to add a do_action on refreshing of WP customizer?
- Refresh table data with Ajax
- How to pass Select value from Javascript to PHP to generate select option on change
- get_children displaying images removed from the page
- How do I display the main domain for my WordPress install?
- Setting value of session with Ajax not working
- AJAX success response is not working but it’s saving my changes
- how to make custom ajax handler?
- [jQuery]Adding shortcode
- Display parent and child taxonomies in separate drop down select fields
- Show success or error messages in Ajax response to WordPress custom registration form
- How to add a Custom Mailchimp AJAX Newsletter Subscribe Form
- Plugins not working on AJAX requests
- Problem with login form
- Fancybox type popup window that’s not an iframe
- Using Select HTML in Bootstrap Popovers
- How to disable controls in theme customizer?
- Can I use a jQuery Ajax request in Code Snippets Plugin for WordPress?
- how to update and display an option without reloading the page
- How do I Import / Upload Files with jQuery AJAX?
- WordPress admin-ajax.php 400 bad request
- WordPress Ajax Call inserting data but success response false
- Loading php file with AJAX in WordPress frontend
- Set Cache-Control header for 301 redirects
- wp ajax return 0
- How to redirect users based on role and content of redirect_to?
- Moving code from theme header to functions.php
- Ajax contact form return 0
- WordPress plugin: render only page content using ajax call
- AJAX – Returning Two JSON Objects with One PHP Function
- Pull GetOption() variable into jQuery dynamically created html
- Showing latest post without 301 redirect
- Having Issue on Redirecting With Session in WordPress
- Get the URL of the page from which an ajax request has been launched within ajax callback
- unable to use ‘new WP_Query’ in AJAX call
- admin-ajax Firing Error 400 When Logged In
- AJAX button action in foreach
- is_user_logged_in() not working in AJAX validation call
- Unset session variable on page reload / setup but exclude AJAX
- Add Ajax Hooks and Call from Custom Template PHP
- WordPress ajax-action failing because of newline in response
- Making an AJAX call to refresh a table
- HTML escaping data with ajax requests
- WordPress Ajax Always returning 0
- Using a $GET parameter from a URL, to redirect to a URL (WordPress)
- Using data sent via AJAX in multiple functions on a WP plugin
- Problem with AJAX in wordpress plugin
- load ajax using admin-ajax.php
- Ajax button not working
- Function won’t run onclick using Ajax
- How to keep track of when a vote is cast?
- WordPress plugin admin page – using WordPress function in linked php file
- Load Posts on Click via Ajax into a DIV
- WordPress redirect redirecting too many times or not at all
- set post limit at load more ajax wordpress
- Use value from Ajax call in PHP function
- Woocommerce with Lazy Load php and 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
- AJAX wp_insert_user WORKS but responds with “The site is not enabled”
- Short code template + ajax
- Ajax filter button display all posts
- Can I make get_users() query global?
- Using Argument from Function to Re-Direct Visitor (WordPress)
- Compare $_POST returned values with term array
- I am receiving a pluggable.php warning sign on my only http:// page
- Is it possible to create new user from external form using REST API?
- Any ideas why I am getting “I’m getting “wp-admin/admin-ajax.php 401 (Unauthorized)”
- Login redirect. Check user meta and redirect accordingly
- WordPress REST API register_rest_route give a 500 error
- Where to insert redirect code based on http_referer?
- How to use AJAX to call php page with parameter
- Is this a correct usage of ob_start() in my WordPress project?
- Get related product based on subcategory selected from category id with ID 142
- How to allow download url redirection only if user logged in WordPress site?
- A $_POST should occur when submit form but is not?
- When echo follow Ajax button inside Ajax pagination template not working?
- My jQuery Ajax form submit is still refreshing page?
- jQuery Ajax PHP function call returning [object Object]
- admin-ajax GET response bad request
- Shortcode to pull posts
- Ajax search shows all results when user empties input?
- Why i can’t get custom fields value or post ID via Ajax?
- Calling PHP Scripts in Ajax that are in PHP Snippets
- javascript variable to wordpress php variable
- Post not populating for custom post type based on category selection
- SOAP Request/Parsing XML From wp_send_json_success
- Output votes to div dynamically in wordpress
- Get uploaded image and attach it to the new post
- Cannot access $wpdb, comes back NULL