have an own customized form in wordpress.
action=<?php bloginfo('template_url'); ?>/send_form.php"
on submit, it goes always to send_form.php
but this php is for sending the info - and i cant style with with the theme....
is there a way to stay on the current page while it sends the info and print out and message in a field that the form has been sended succesfully???
anyone have some suggestion for me?
The answer to this is quite involved and getting it right takes a little care, but here are the essential points of one approach:
The action must be set to load the current page and you can do this by changing the action attribute in the <form> tag to: action=""
In the template file for this page, detect when the page loads after a form submission. You can do this by checking the state of the $_POST variable.
If there are post variables, process the form submission in the template file. You need to look at what happens in "send_form.php" to figure out what to do. Take care that you don't introduce security issues (e.g. be sure to use the NONCE).
Redirect back to the same page to get rid of the post variables. If you don't do this people will get those "Do you want to resubmit the form" warning messages. See the PHP header() function for doing the redirect and note that this must happen before any output is sent to the page.
Use server sessions to display a "Form submission successful". Set a session variable to your message before the redirect, then detect it when the page loads, display it, and remove it so the message doesn't display the next time the page loads.
That should give you a starting point.
If anyone else has a simpler solution, I'd love to hear it too.
If you want to do it through plugin instead of the theme, then use "admin_post"(admin_post_YOUR_ACTION) and "admin_post_nopriv" (admin_post_nopriv_YOUR_ACTION) actions. (Btw, you can use those actions through theme also)
Here is a good explanation: https://www.sitepoint.com/handling-post-requests-the-wordpress-way/
Related
I'm new to Wordpress developing and I have found what feels like a clumsy solution to a problem that seems like it should be common. Please help me find a cleaner solution with less potential for buggy behavior.
Objective: Get date range from user on a Wordpress page, use date range in API request, display data from API response on Wordpress page.
Current solution: Start and end dates are submitted via an HTML form in a Wordpress page with POST method. The POST request does not route to any other page. In addition to start and end parameters, the form has a hidden action parameter with a custom value I have specified: <input type="hidden" name="action" value="custom-value">. I have written a plugin that uses the init hook. The hooked function checks $_REQUEST['action'] to see if it matches the custom value in the form, and if so it does a GET request to an API (recreation.gov) using the start and end dates from the form. Up to this point, the solution seems pretty solid to me. Here's where the tutorials ran out and I had to get......inventive.
The data from the API request is processed and formatted into HTML, and this string is stored as a transient using set_transient with a 3 second lifespan. In the original page that the form was submitted from, there is a shortcode that simply returns the transient. Now I have the data from the API on the same page that the user specified the date range from.
This solution works, but seems like there is a high probability of things going awry if multiple users are using the page at the same time, and seeing or overwriting each others' results. There must be a better way to get the data from the plugin that does the API request back to the Wordpress page.
Since this question is about methodology, not syntax, I haven't included any example code, but I could if that would be helpful. Thanks in advance for your advice.
The solution I ended up finding was ditching the plugin and doing everything from inside a shortcode. Shortcode has access to $_REQUEST['action'], which I wasn't aware of, so I was able to get the user input and do the API call in the shortcode, then format the results and return them. No need to deal with plugins and hooks because the shortcode is executed as the page is loaded.
I know the methods of tracking a form submission but in this case unfortunately I couldn't make it. Once the form is submitted, you can see in the debug mode that the exact same variables fire as in the case that the user tried to submit that form and it failed (because some fields weren't filled in). So the variables of GTM upon submitting don't help.
I tried another approach, with the url redirect. But the thing is that the redirection is to site.com/buy?add-to-cart, and this parameter causes adding to cart and then it redirects to the checkout page. This checkout page can be reached from many places so I can't track the form submission with page view either.
I don't know what to do already, is there another way that I didn't think of?
Thanks in advance
My client would like to give each customer a password and once entered, this should then redirect them to a certain page. I.e. password 'cake' would go to a page about cakes, password 'cats' would go to a page about cats. Sounds simple enough, is there a plugin that you guys are aware of that would do it? Or is it possible straight from WP itself?
Thanks
Take a look at Peter's Login Redirect. I haven't used it myself, but it has good ratings, has been updated recently, and it sounds like it can redirect based on the login criteria entered.
On a side note, I can't wait to see this website that features cats and cakes!
Good luck.
I'm sure there are better solutions out there. I accomplished what you are looking for by using conditional fields and html in Gravity forms. To mimic a password, I removed the Gravity forms submit button, and I basically created my own submit button in the html field to redirect the visitor to my desired url. Using conditional fields, I hide the button until the user enters the exact passphrase.
This method also allowed me to create hide/show multiple buttons matching different passcodes. We give private clients different passcodes to redirect them to different pages and content.
I've searched for hours for that solution! Here You go #ajguk: http://thecodecave.com/plugins/smart-passworded-pages/ or http://wordpress.org/plugins/smart-passworded-pages/
The plugin is called "Smart passwor pages" (for those experts that can't reed what the link says). It is as close as it gets 4 me. The plugin is old, but it works. Instalation is simple. You just need to put shortcode [smartpwpages] on a page which gives You a login field and every childpage under page with shortcode (if passworded protected throug WP) will work exactly like you asked. You will need to enter into login field only a password and it will redirect user to a page that this password protects... Just need to make your passwords unique for every childpage.
Cheers!
Drupal 6.22
Fivestar 6.x-1.19
Rules 6.x-1.4
These modules were the latest as of this posting.
My goal is to redirect a user to a page when they vote on a node. Should be simple enough.
I've created a rule for when a user votes on a node. No condition. I've added an action to add a message to the log to verify the rule is being triggered. Which it is. Then adding an action to redirect to the homepage (for testing). The redirect never happens. If I check the option "Immediately issue the page redirect", the ajax vote never saves and just hangs at "saving vote...".
I don't think I'm doing anything wrong and will post this to the fivestar module issues. Has anyone else had this issue?
http://drupal.org/node/252859
A "redirect to url ..." will definitely cause issues with Fivestar's AJAX voting mechanism. Here's what's happening:
What normally happens:
- User clicks on a Fivestar widget to rate.
- AJAX request is made by the current page.
- VotingAPI saves the vote.
- Fivestar generates XML and prints the page.
- The current page receives XML, then updates the voting widget with the new values.
What happens with VotingActions:
- User clicks on a Fivestar widget to rate
- AJAX request is made
- VotingAPI records the vote
- VotingActions hooks in and says "oh I'm redirecting this URL" <-- Where things go wrong
- Instead of returning XML to the calling page (which would update the votes), the original page gets back a full HTML page of wherever VotingActions redirected the page.
- Fivestar doesn't get to generate it's XML, because VotingActions has ended the execution by using a drupal_goto().
So I don't think it's a bug in either VotingActions or Fivestar, it's just a case of using two features in incompatible ways. To make this work, Fivestar would have to not be AJAX driven, instead reloading the page to save a vote.
Fivestar provides a nice JavaScript hook that you can use to go to the next page. If you add JavaScript to the page that contains something like this:
function fivestarResult(voteResult) {
window.location.href = 'http://google.com';
}
How can I create a form that requires anonymous viewers to register in order to view it?
Right now, I've created a content-type (Submit Plan) which is a primary link available to everyone (anonymous + authenticated). I've restricted 'view' user permissions to anonymous users but they can still see the 'Title' input (I don't want that).
I'd like it so that when an anonymous user clicks on 'Adding a Plan' primary link (the Submit Plan content-type), it goes to the page and says:
"You must register for an account" OR
Redirects them to the registration page asking them to register to submit a plan.
I've been searching for a module or maybe some code to use but have come up short on this topic. Any help would be appreciated. Thanks!
If you're going to be redirecting users to the registration page, I'd strongly consider using something like logintoboggan to make the registration > node-creation process smooth. Otherwise, registration is a multi-step process and I'd imagine it being easy for users to lose their way back to the Add a Plan form in the process.
For the "show links or show the form" direction, there are at least two ways of approaching that: 1) create a custom page where you "import" the add_plan form (or show the links). 2) modify the node/add/plan page itself, either through themeing or the fapi (the forms api).
Here's a promising looking post for the first method: http://drupal.org/node/357895.
Here's a place to start with FAPI http://api.drupal.org/api/drupal/developer--topics--forms_api.html/6
Here's a post about theming the node form: http://11heavens.com/theming-the-node-form-in-Drupal-6
Without having tried this, I'd probably lean toward method one.
Update: just had another thought: you could also add the plan form to the registration form so they'd fill them out in one shot. I'm not sure of how to do that in general, but the node profile would work if they're only ever going to make one plan, and if not, you can look at how that's put together.
This can be done a few ways.
One, in your menu output, you can change the link to Submit Plan like this:
<?php
global $user;
if ($user->uid == 0) {
print 'Add a Plan';
} else {
print 'Add a Plan'l;
}
?>
The above code looks to see if the user object has a UID. 0 is anonymous, so that will print the link that sends them to register. Otherwise it will take them to the node add form for Submit Plan content type. This also assumes you control your own menu output. You can also override it in a similar manner by using a theme function.
There are a few ways you can do this, so start there and let me know what you think.