Wordpress form handling - php

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)

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.

handling submission with a "Form Maker"-created form

I'm using Drupal 7 and HAVE to use the Form Maker (https://drupal.org/project/drupal-form) module. Since it's not a normal form, I don't know how to do something like hooking as Drupal form submission Q/A explains for normal forms.
The available options for "action after submission" for those in-database forms are:
Stay on form.
Link to an article.
Display custom text.
Redirect to an URL.
Is there any way I can handle the form submission? e.g.: Is there a way to reference the form state for the submitted form in the url I specify if using (e.g.) option 4? (or whatever mechanism exists to recover that data by knowing+referencing the submitted record).
BTW I'm a total drupal n00b, and couldnt Find TFM to R (so I can follow the RTM principle) about that special issue in the Form Maker doc.
It looks like this module still uses the form API, so you should just be able to use hook_form_alter() to modify these forms. I'm sure there's some kind of naming convention for the IDs of these forms, which will allow you to apply changes exclusively to form maker forms.
If you need to customize the handling of form submissions, you can include your own submit handler by using the #submit form attribute. Note: the example in the link adds the submit handler to a field in the form, but in most cases I add the submit handler to the form array itself. (i.e. $form['#submit'] = array("submit_callback"))
Edit in response to comments
You do not need to reference a form_id when invoking hook_form_alter(). Instead, you can invoke it globally, which will cause drupal to call your hook on every form. As you can see from the documentation link I provided, the $form_id is passed to the function, and you can use that to conditionally modify the form. Please look at the comments on the documentation page for usage examples.
Unless these forms are being embedded using a third party service, if this module is actually legitimate it will be hooking its forms through the form API. Implement hook_form_alter(), and print off the form_id's that are passed to the function. Go to a page that has one of this forms on it, and see what gets printed.
(Also, [form_id]_form_alter() is not the correct syntax for invoking this hook on specific forms. Should be hook_form_[form_id]_alter(), where hook is your module name.)

How do you handle multiple forms in WordPress plugin development?

I'm transitioning from CodeIgniter to WordPress. Still trying to wrap my head around WordPress plugin development... seems pretty easy, but one thing I can't figure out is: where do I send the form data? In CodeIgniter, you could simply create another method in the controller and point the form action there, but it seems WordPress plugins are a little different.
Depends on how your plugin is generating the form. Typically you do it by registering a shortcode, something like [my_form] that you can insert into a page or post where you want the form to appear.
In your plugin code the form action can be blank -- simply reload the same Wordpress page. Your form info. will be in the global $_POST and your plugin can then process the data.
So your plugin shortcode function would have something like:
if(isset($_POST['my_plugin_form_field'])){
/* code to process the form info and generate a message on screen
}
else{
/* the form hasn't been submitted yet so here is the code to display it
}

Contact form with Wordpress wp mail

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.

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.

Categories