Contact form with Wordpress wp mail - php

I want to build a simple contact form, without plugins, on Wordpress using the build in wp_mail function. Standard fields, email subject and message.
The following features are required:
Ajax Submit (nice to have) (I use the jQuery framework)
If Ajax submit, a non javascript
fallback.
I'll validate the form client side,
but would also like to know the best
way to do server side validation, and
pass the message back, within the context of Wordpress.
What is the best practice to achieve this? Any pointers or tutorials. I found this question of note.
If you thinking I'm just reinventing the wheel let me know. I normally use Contact Form 7 (plugin) but am bored of its heavy html markup and the way it loads JS on everypage.
Thanks!

Contact Form 7 is written very well, and ripping apart the guts of that code is a great place to start for pointers and ideas.
Most of the time you could just strip out the core functionality there, and just create a more lightweight front end to suit your needs.

Related

How do I make a custom <form> element be used in place of a <form> in a pre-existing plugin?

I am customizing a theme and a plugin in Wordpress. The plugin has a button where a user can click to bring up a form where they can ask a question. I want users to use post questions through this button on this plugin, but I don't fancy the visuals on it. So I thought I'd make a form on my landing page that will post into the plugin's form. If this is a terrible idea in the long run, please feel free to let me know because I am still new to web dev.
So the goal is to create a new form whose aesthetics are to my liking but still use the plugin's built in posting capability. How do I make my form paste into the plugin's form and then submit it? You'll notice that my form has 1 additional field: the add money field. I also need to integrate this into the plugin. If anyone know how to begin modifying the code for this I'm all ears to anything I need to start reading or a general way of how to approach this.
My form
Plugin's form
Summary:
Need to have my form paste to plugn form and submit
Need to add a field to the plugin code that my form can post to similar to how the other fields will do do
You can post from a different form if you get the destination URL or javascript that is handling the original form post. You can use the debugger in Firefox or Chrome to inspect the page contents to see how the form is handled.
Adding the money field will be more complicated because you will need to update the server side handling to accept the additional parameter so that it is not simply ignored or causing errors. How much work this is depends on what the server has to do. It may involve adding a column to the database or creating a new table which you will likely want to do the wordpress way if you want to have something that is maintainable.
Lastly keep in mind, that every time the original plugin is updated, it may bust your add on code unless you create your own plugin from the original.

Is it possible to have a contact form on a pop up box without using ajax?

I am sorry for the stupid question, I realize that what I want to do must be extremely simple, but I can't find my way to do it.
I learned basic programing, mostly front end, and enough php to interact with databases and other basics. I can use jquery, javascrip, ajax, no problem in normal situations. But I am not fluent at all in using object-oriented php and I work on a Yii environment.
I have a contact form on the website I'm working on and it works perfectly sending the data to the database. Now I have to re-create the same form, and use it on a pop up box, but I have no idea how to do the php validation and saving to the database in that situation. I've always used Ajax and I tried it, but apparently you need special synthax with ajax on Yii that I am not familiar with.
So is there any way to have the php validation and saving being performed without ajax, (and without the form submitting that refreshes the page and closes the lightbox)?
"Popups": http://jqueryui.com/dialog/
ClientSide validation: http://jqueryvalidation.org/
PHP-Validation: Easiest Form validation library for PHP?
After everything I tried, because I don't know the Yii framework enough to use ajax in their synthax, I found a way to make my form work with simple jQ.
I have an empty onsubmit attribute on my form, that will change to "return false" if there are errors in the js validation, keeping the page from refresh and showing the errors. And if there aren't errors the form will submit the info to php validation then save to the database and all the php will be executed and the lightbox will re-open with the success message.

Adding action to Contact Form 7 submit w/ PHP hook

I'm having some trouble implementing a bit of custom functionality in the Contact Form 7 plugin for Wordpress.
What I want to do is pretty straightforward. The contact form in question is a normal contact inquiry form, so I need to retain the usual functionality (mailing the data). However I also have a checkbox on the form that allows the sender to choose whether to subscribe to the client's mailing list in addition to mailing the contact inquiry.
The client uses Bronto for their mass mailing (similar to CC or Mailchimp). Bronto has a "direct add" feature (more info here) that allows you to send parameters to add contacts to the Bronto account by embedding an image whose url contains the requisite parameters (email address, list to subscribe to, etc).
I can construct the image url with the contact form parameters no problem, but actually getting the image request sent is a different matter. I am over my head both in PHP and JS here and not sure what course to take.
Currently I'm using the wpcf7_before_send_mail php hook built into CF7 and this appears to allow me to collect the form data and build the URL. However, since the plugin uses AJAX and doesn't actually redirect to another page on form submission it seems I can't successfully use any kind of php output (echo, alert, even error_log), presumably because the server doesn't know what it's supposed to write to.
In functions.php:
add_action( 'wpcf7_before_send_mail', 'bronto_contact_add' );
function bronto_contact_add( $cf7 ) {
$emailcf = $cf7->posted_data['email'];
echo $emailcf;
}
This is just a test to see if echo works - it does not. Instead the form just hangs on submission and I see the rotating loading gif forever.
What I need to do is build the image url using parameters from the cf7 object (which I can do no problem) and then somehow send that image request to the Bronto server (this is the part I am confused about). If this was a normal form that redirected to another php page upon submission I would have no problem doing this at all, but it uses AJAX which I don't know much about so I'm pretty lost now.
Can anybody help shed some light on how the best way to accomplish this might be?
If the submit is hanging after you attached your function, at least you know that it had an effect. I'm not terribly familiar with Contact Forms 7, but this is probably not the proper place for an echo, and my guess is that it is hanging because you are writing to the buffer and then it is trying to do a redirect (check your error logs). If you want to see the contents of $cf7, a better way to do it would be:
// first option, using print_r()
error_log(print_r($cf7, true));
// second option, using var_dump() if you need the additional output
ob_start(); // start buffer capture
var_dump($cf7); // dump the values
$contents = ob_get_contents(); // put the buffer into a variable
ob_end_clean(); // end capture
error_log($contents); // log contents of $cf7
The contents of the $cf7 variable will then be in your PHP error log, which will tell you how to access the different components.
I came across your thread while looking for a similar solution to the same problem with CF7 - a hang on submission when trying to pass info to my own database on the back-end for a CRM.
I did not see an answer to this issue anywhere on the web in relation to CF7 integration, so thought I would post here what I found was the problem and how it was fixed. I'm no pro, but the solution works in testing, if anyone has anything to add to this solution please chime in
Basically, if you are using Wordpress and trying to pass the info into a CRM database, I am going to assume your database tables are not on the same database as your Wordpress site database. What you are then trying to do is establish two database connections simultaneously, but the reference ID is being reused for your Wordpress database when trying to connect to your CRM. I found this was the root cause of the hang on submission during testing.
I used a deprecated command from PHP 4 that still works in PHP 5, mysql_connect, :
mysql_connect('localhost', 'root', '', true);
By passing 'true' as your fourth parameter you are able to keep the connection separate from the one running for your Wordpress site. Did this and the CF7 submission doesn't hang, it submits to the CRM and sends it out as an e-mail simultaneously, no problem.
Note too though, if something is wrong with your syntax for the CRM data submission i.e. misnamed variable, etc.. it will also hang. If passing 'true' doesn't work check your code first to make sure it's clean.
If anyone reading this has an equivalent solution for this with 'mysqli' commands I'd be interested to know it, I tried it using mysqli and couldn't get it working.
Is there some reason why you can't just prefix the table names and add them to the same database? It seems that would be a better solution and would work fine with mysqli as opposed to using archaic, insecure drivers.....
I guess, I just don't see the point of using two databases in this case... I would try using one.

How to make drupal known submitting custom content

I know this is not a drupal forum but, as I’m not getting any response there, I decided to give it a shot here.
I’m creating a web site that accepts custom content from users. So, for that matter, this site has a form and a custom module. Instead of using admin theme, this form is placed inside custom template which is created to have a uniform look with the rest of the pages. As a result, creating form elements through hook_form is out of question. Here’s where my problems lie. As this form uses custom theme, I’m not sure as to what can I do to make drupal know that user is submitting new content data when the form is submitted?
Would I need to use same query string that of content submission page of admin page like - ?q=node/add/page for action attribute of the html form?
(OR)
the only way is to map the url to my custom function and invoke some sort of hook inside of it?
Thanks
You can literally create any markup you want for your form, all you need to do is use the #theme attribute when you define the form. With it you can set theme functions for the form itself and any of the elements.
It is a very bad idea, not to use Drupal's FAPI. It solves so many problems for you, and not using it would be the first step to take if you want to open up a security hole in your site. A development framework like Drupal is not of much worth, if you don't use it's APIs.
Edit:
First thing to do, is to go to Drupal's FAPI reference. You can learn almost everything about the FAPI there.
You could use a template if you want, is just basic Drupal theming, but I would advise against it. It would be a lot more maintainable if you created theming functions for all the elements and used that instead, you could just loop through all the elements and render them like Drupal does, instead of having to edit a template file each you need to change the form. It might be a but more work now, but there's a reward to that work: cleaner and more maintainable code.
In code it looks something like this:
$form['item'] = array(
...
'#theme' => 'theme_function',
);
Doing this, the element will be rendered using the "theme_function". You can see an example of such a theme function for textfields.

Wordpress form handling

I need to add a basic form page in the website, that runs on WordPress framework.
I have the following raw materials ready:
Client side: html form layout,css and jquery validation code.
Server side: form handler php function that processes the $_POST[] data.
My problem is to integrate this code in the Wordpress framework.
I have looked at some plugins but they are doing much more than I would like and also they have their own validation which is cumbersome to change.
Could anyone suggest a good form plugin that allows just the framework hooks ?
Or is it worthwhile that I should write the plugin myself.
Thanks.
You can create a page template in the theme folder you are using that can handle this. Simply add your custom PHP code and xHTML mark up for the form. When setting the form action all you should have to do is set it to post and the page will post to itself. This may require you to add some additional logic to see if the $_POST vars are set and to process them if they are, but it's easy, clean, and will also let you give the user a status message if you are doing any kind of success/failure checking.
Alternatively you could look to using the following plugins that make this a cinch:
Cforms II
Contact Form 7
Gravity Forms (Premium Plugin)

Categories