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
}
Related
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.
I'm using Drupal 6 and i want to create a block, that shows a form which also can be found on a subpage.
To do that, i created a drupal form API Form form() with its validationhandler form_validate() and its submithandler form_submit(). Then, I added a form API form widget_form(), which is basically the same as form() - just minor style adjustments, with the validation/submithandler set to form_validate() and form_submit(), also I changed the action to go to the subpage where form() is displayed.
This works fine, but if there is an validationerror (when using the widget_form()), drupal marks the block-formular, not the one on the subpage.
This seems to work as designed, but isn't the required behaviour i want. So, any way to change that?
(I want the error shown on the form() - form, not the widget_form())
Thanks!
Edit: I tried to hide the block containing the widget_form()-Form on the page displaying the form() form, no error is displayed.
I think the approach used is not quite right. What you need to create is just one form and one set of validation and submit function. For the block use hook_block and create a block and inside the block view op you can call drupal_get_form and display the form inside the block and then theme the block using CSS or custom template files. Then from the block configuration page you can decide to not show the block at the URL (menu) where the full form is loaded. You have got that part right.
I have created a page template in WordPress to display a form with action="". Very simply, it will not post any of the form data upon submission. I have tried var_dump($_POST) but it just gives me array(0) {} every single time. I've tried examples provided by some websites for creating a form, but I can't reproduce the same results.
I have thoroughly search for answers to this problem but nothing seems to work. It must be something internal or very advanced. I just want to know why my form data won't post and where it might be going.
Check your data using a non-WP page, meaning put a php file in web-root, and have the form action point to it, such as ...action="/formtest.php", and on that page:
<?php
var_dump($_POST);
?>
This will test if you really aren't getting the POST vars. Then, since you'll 99% likely find they ARE being posted, the likely trouble is that your page IS being redirected somewhere by the theme as mentioned elsewhere (e.g. my theme includes a hook on 'template_redirect'...).
If you are theme savvy and just looking for a quick fix- at the expense of losing it on theme upgrade- you can create an exception just for your temp.php page, but the way that many developers handle it is via Plugins, or handle your form via Ajax. Many others have encountered this same issue, and so, either tacking on code to a Plugin, or performing in-page Ajax (since you are submitting to SELF anyway...).
This page will give you a leg-up on WP Ajax, it's a reputable and well written doc.
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.
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)