Wordpress Form Submit button GETS (or POSTS) form data to external page - wordpress

We have a wordpress website that does marketing display, but now we want to allow a customer to submit an email and selection to a separate website with a landing page that will handle the backend DB work and finish them in the other website.
Something like,
www.ourmarket.com/getdata (on Submit button click GETS to...)
www.ouradminsite/landingpage.aspx (which processes the data that the use will not see then...)
www.ouradminsite/login.aspx (where the user can now login)
I am not familiar with WP at all, but I was able to create a page with a form that has the textbox/combobox I need.
I thought it would be something simple, but somehow it seems not. I read about AJAX and doing something in functions.php and creating a custom .js file, but when working on the marketing site I find no way to add this type of function in.
My fall back is to have the WP page just have a link to a generic landing page where they enter data, but it would be visually jarring to the customer unless I duplicate the WP site for one page.
Is there an easy way to just tell WP to redirect to an external page with a GET?
UPDATE--------------
I like to think I'm making progress. I found a link that may have given me a good start. I added a function to the functions.php file located in my WP theme. It starts like this:
add_action("gform_post_submission_4", "set_post_content", 10, 2);
function set_post_content($entry, $form){
//Gravity Forms has validated the data
//Our Custom Form Submitted via PHP will go here
// Lets get the IDs of the relevant fields and prepare an email message
$message = print_r($entry, true);
// In case any of our lines are larger than 70 characters, we should use wordwrap()
$message = wordwrap($message, 70);
// Send
mail('myuser#mycompany.com', 'Getting the Gravity Form Field IDs', $message);
**wp_redirect("http://my.hearbuilder.com/hellomoto.aspx",301);**
}
From there I tried to edit the function to do that wp_redirect, just a simple one to start. This is added under the mail statement:
wp_redirect("http://my.hearbuilder.com/hellomoto.aspx",301);
From this link, when I fill out the form I can get the email, but the new page did not display. I added the exit; line and still got the same result, the page seems like it hangs.
The end result is that I need to have the new website landing page display (after it processes the data from the Wordpress form.
What am I still missing?

yes use wp_redirect()
if($_POST):
$textbox=$_POST['textboxname'];
$url= 'url'.'?custom=hello&textbox='.$textbox.'&anothervalue='.$anothervalue;
wp_redirect($url);
exit;
endif;
you can easily add the variables to the string as needed. The easiest way to control the url properly is to post the information to the same page , catch the post and redirect (place before get_header call or any output has started)
The other way is php Curl which is more difficult esp when dealing with .asp pages, but if you have access to the other server it makes figuring it out easier!

Related

How to POST to admin-post.php from Avada formbuilder form

Using the Avada theme's form builder functionality, I’m attempting to use the “Send to URL” option to POST the form and then run an api call in a plugin I’ve written.
I was thinking I could set the “Send to URL” value to /wp-admin/admin-post.php and add a hidden field to the form named “action” with a value of “make_api_call” to get it to run the code in my plugin set up like so:
add_action('admin_post_make_api_call', 'make_api_call');
function make_api_call()
{
//todo: make the api call
wp_redirect(‘another-page’);
}
However, this does not work and returns this from the server: {"status":"error","info":"url_failed"}
What I want to do is to POST the form to admin-post.php, have my plugin run code when it POSTs, and then redirect to another page.
I've checked the documentation for the AVADA theme and it only says that you can specify a url to post to but doesn't give any additional details.
So I eventually got something to work, and I'm not knowledgeable or experienced enough with WordPress development to know if it is the "right" way, but it works well.
I realized that using the "Send to Url" option on the Avada form, it was POSTing the form to the admin-ajax.php file. There's plenty of documentation on that and I was able to partially make that work but I was not able to make it fit my use case b/c even though there is a way to configure the Avada form to redirect to a different URL on success I couldn't append parameters to that URL based on the return value from admin-ajax.php.
For future reference, here's what I was able to make work but not fit my use case by having the Avada form submission set to Send to Url. (I'm recreating this and some of it's from memory since I went with a different solution, so it may not be 100% runnable.)
The way admin-ajax works is all requests to admin-ajax.php are eventually handled by a WordPress action (filter?) like so:
add_action( 'wp_ajax_my_action', 'my_function' );
In the above, my_action is what you've set as the form's action by creating a hidden input element on your html form named action and setting it's value to "my_action". The my_function argument is the name of the function you want to run when the action happens.
add_action( 'wp_ajax_my_action', 'my_function' );
function my_function(){
//do stuff
}
Watching the request in Chrome's dev tools, I could see the action the form was setting was fusion_form_submit_form_to_url.
So ended up with this:
add_action( 'wp_ajax_fusion_form_submit_form_to_url', 'my_function' );
function my_function(){
//do stuff
}
You can see that the url you enter in the Form Submission URL field gets passed to admin-ajax as fusionAction. Whether the Avada theme does something additional with that - I don't know but you could use it to control the logic that gets executed in my_function. I suspect there's an action in the Avada form that works similar to the wp_ajax_ WordPress action but by the time I got this far I realized this wasn't going to work so I pivoted to the actual solution, below.
All of that worked okay but you can't redirect out of a call to admin-ajax.php unless you do it on the client side and I didn't want to dive into that.
What I was able to make work was configuring the Avada form to do a traditional HTTP POST. I added a hidden input element on the Avada form with a name of formName and the value set to the name of the form I wanted to handle.
In my plugin code, I hooked into the WordPress init action as in the code sample below, and then customized the logic to be executed based on which formName was sent in.
add_action('init', 'callback_function');
function callback_function()
{
if (isset($_POST['formName'])) {
$form = $_POST['formName']; //from the hidden input element "formName"
//there is a call to wp_redirect in each case
switch ($form) {
case 'form1':
process_form_1();
break;
case 'form2':
process_form_1();
break;
default:
# code...
break;
}
//without this "exit" you will get errors similar to "headers already sent"
exit;
}
}
This allowed me to run the code I needed to run based on what form was submitted, and redirect to the correct place afterward.

Redirect "dumb" URL to Author's custom post

I have created a website whereby users register and create their own templated profile pages. The profile pages are automatically created as Custom Posts upon registration, with the user being set as the Author of their specific post (profile).
(Users can only ever have one Custom Post)
I want to redirect a "dumb" URL like www.website.com/my-profile to a user's custom post when they are logged in.
For example, when John Smith visits www.website.com/my-profile he is directed to his profile page: www.website.com/users/john.smith
I have found many PHP solutions going the other way, but I can't seem to find a solution that does what I need. Any help would be greatly appreciated. Thanks!
This may not be the correct answer to the original query, but proved to be a solid workaround:
Instead of redirecting www.website.com/my-profile to www.website.com/users/john.smith every time it is entered in the URL bar, I created a shortcode that could be used when needed throughout the site.
add_shortcode('bt_redirect_user_link', 'bt_redirect_user_link');
function bt_redirect_user_link ($atts) {
// check if user is logged in
if (is_user_logged_in()) {
// get current user object
$current_user = wp_get_current_user();
// get user nickname
$user_nickname = $current_user->data->user_nicename;
// set the link href
$link_href = '/users/' . $user_nickname;
// output the link html
return $link_href;
}
}
Fortunately for me, the www.website.com/my-profile link (which needs to be redirected) is only available on buttons/icons visible to logged in users. This may not be a fully workable solution for websites that need to display the link to logged out users, and I assume IF/ELSE statements would needed to be added in those cases.

WordPress functions.php - Admin html injection and submitting forms

I created a new navigation item on the left for my WP Admin:
add_action( 'admin_menu', 'addManagementMenuItem' );
function addManagementMenuItem(){
add_menu_page('Issue Management', 'Issue Management', 'manage_options', 'issue_management_slug', 'issue_management_building_function','',3);
}
function issue_management_building_function(){
if(!current_user_can('manage_options')){
}
else {
?>
...
...
So where I have the ellipsis ... is where my HTML begins and I write out some information to the page with various php echo statements to print some data out.
What I would like to do is now give the user the ability to enter in a filter and press submit. This would issue a POST to another page which would receive the post data, run some stuff, and spit out something else to the screen. I was just thinking this would take the user away from the WP-ADMIN area entirely (what I want to do is keep the user all within the right pane so it looks like it's natively happening on WordPress under my new admin area)
Something feels wrong about this approach above where I'm putting tons of html into functions.php - what is the way to create pages for a custom admin section where I can do things like post forms and go to multiple pages?
I was thinking the best solution would be to put an iframe in my injected HTML in functions.php, and then the pages can talk to themselves just like normal behind the scenes in WP-admin.
Could anyone point me in the right direction?
thanks!
Considering the user input/_POST features you'd like to add to this, you may want to consider building this functionality out as your own plugin. I've always kept custom functionality limited to non-user interaction in the functions.php file, but anything further would probably be better fit as it's own plugin.
For example, what if you created a plugin directory named nullhypothesis:
add_action( 'admin_menu', 'addManagementMenuItem' );
function addManagementMenuItem(){
add_menu_page('Issue Management', 'Issue Management', 'manage_options', 'nullhypothesis/file_to_do_your_bidding.php', 'issue_management_building_function','',3);
}
It's that fourth parameter that in the documentation mentions that you should include the menu_slug, but it doesn't necessarily need to only be a function - it can also be a file you define.
Then, in your file_to_do_your_bidding.php file (within your plugin), you can add whatever _POST functionality you'd need it to. It could also exist as the 'admin' page that the administrator/whoever interacts with.
Was that what you were looking for?

Send thank you email in wordpress after user is redirected from other website

This is the problem I am trying to find a solution to: I manage a wordpress site that deals with donations. There are 5 options. Once the user clicks on one of them they are sent to a third party site to make a donation. After they make the donation they are redirected back to my site with a set of data (name, email etc.). What I need to do is get that data and send a thank you email to the user. Is there some wordpress plugin I can use to handle the incoming data and send the email? Or maybe use a plugin like Contact form 7 to do it? I am not really familliar with wordpress. Really basic knowledge
The easiest way to do this would be to create a custom page template with logic to capture the data sent after donating in it and apply the template to a page.
E.g.
wp-content/themes/your-theme/ipn.php
<?php
/**
* Template Name: IPN
*/
// Grab the data that's returned, however it's returned.
$data = $_GET['data'];
// Use WordPress mail function to send your email
wp_mail( $to, $subject, $message, $headers, $attachments );
// Either redirect to another page or display a thank you message.
Then go into your admin, create a new page called IPN, select your page template from the drop down and publish. You can use the URL to this page to pass data back to it from the 3rd party site after payment.

how to hide a page from being seen in wordpress backend and frontend

In my plugin i have created a custom template that prints a requested sidebar. and for running the code of this template i assigned a custom page to it (by calling update_metadata) .
Is it a good idea for getting content of a specific sidebar into Ajax call ?
Now my problem is that WORDPRESS shows it in the dashboard and front page , and after searching i have not found any easy to understand solution for Hiding a page completely so only can be accessed by its id .
Can any one tell me how to do that ?
you are going about this the wrong way. You can create a function that can create anything that can be created on a wordpress page.
But if you really must you can create a page outside of the database, etc:
add_action('init', 'add_rewrite_rule');
function add_rewrite_rule(){
// add_rewrite_rule(REGEX url, location, priority (i.e. top is before other rewrite rules)
// I created a custom post type for this plugin called market -- replace post_type with whatever you want
//basically tell wordress to add a query var if sidebar is added to url.
add_rewrite_rule('^sidebar?','index.php?is_sidebar_page=1&post_type=market','top');
}
// register a query var
add_action('query_vars','market_set_query_var');
function market_set_query_var($vars) {
array_push($vars, 'is_sidebar_page');
return $vars;
}
// associate a template with your quer_var
add_filter('template_include', 'market_include_template', 1000, 1);
function market_include_template($template){
if(get_query_var('is_sidebar_page')){
$new_template = (theme or plugin path).'/pages/yourpage.php'; // change this path to your file
if(file_exists($new_template))
$template = $new_template;
}
return $template;
}
This will not be a page that will be in the admin section or in any query that relates to pages but someone could of course navigate to this page. But as i said above you would be better to create a function to create your sidebar. If you want a seperate file to handle the "view" you use require_once 'filename'; a file and keep your functions area free of html.
If you are creating functions in a wordpress plugin dont forget many functions may not be available until later in the load process. Use add_action() if you run into any undefined functions
edit:
you are loading wordpress before you get to the template so you have all the functions. (google wp load for more info) + get_header() / get_footer() will also load a few things like css, etc. I had a small typo in the code above, fixed that but basically what you are doing is telling wordpress if someone lands on www.example.com/sidebar to apply a query_var (rewrite rule). Wordpress will look up its saved vars (final function) and return the template assoc. The 2nd function just registers the var.
You also have wp_functions in any file you create and include in a plugin, etc hence why you can create a file that does exactly the same as this page.

Resources