wordpress add custom form in a page

Have you ever wished to include a custom form inside a page of your wordpress site ?
Well, its pretty simple.

In this tutorial, we will try to include a small contact form in a page, that when submitted, sends out an email to the administrator of the site. The form contains 2 fields, address and phone number, which are filled by the end user.

To proceed, create or edit a page in your site, and add the below form code to it. Assuming that jquery support is available for your site.

Now create a new page in your backend, that maps to a custom php template file. Make sure that the slug for the page is same as the one defined as the action for our form (custom_contact_form).
The idea is that, when the form in the page is submitted, it will be processed by our custom php template page.

Below is the sample code of the custom php template file, that parses the form fields and sends out an email to the site admin:

Done :)

FacebookTwitterGoogle+LinkedInEmailShare

drupal 7 ajax custom javascript

In Drupal 7, we can pass the values from response object to a custom javascript function, after an ajax call is successfully executed.
For an example, lets consider that there are two dropdown fields and the values of second dropdown are dependent on the selected value of the first dropdown.

When an onchange event occurs on the first dropdown, we reload the second dropdown field, using the value from the first dropdown.

Module file code:

As you can see in the above function, we are passing the values of the selected first field, and the options of the second field, to the javascript function. Now we can access these values from the javascript callback function, that we define in the JS file:

FacebookTwitterGoogle+LinkedInEmailShare

Node hooks in Drupal

We have several hooks for nodes in Drupal. Here i will discuss few of the hooks that can be used with view, add/edit and delete of the node.

FacebookTwitterGoogle+LinkedInEmailShare

Altering forms in drupal

In Drupal, we can alter the forms of content creation, using the hooks: hook_form_alter(), and hook_form_form_id_alter().

Normally, if you know the ID of the form that you want to alter, you can use hook_form_form_id_alter().
Or you can use the generic hook_form_alter() and check if the page contains the form that you actually want to alter.

Examples of usage:
Suppose you have an “employee” content type and you want to customize the fields of the form (add/edit). You can use any of the below hooks in your module file:

FacebookTwitterGoogle+LinkedInEmailShare

Recommended directory structure for drupal modules

In Drupal, we can classify the modules as 3 types:

1. Core modules
2. Contributed modules
3. Custom modules

Below are the recommended locations for storing the modules:

Core modules:

Contributed modules:

Custom modules:

The above approach will be useful when you upgrade Drupal or any of its modules to a new version.

FacebookTwitterGoogle+LinkedInEmailShare

WordPress curl post ajax request

Suppose you want to make a CURL POST to a WordPress function that accepts AJAX calls.
Below code makes a PHP CURL POST request to a wordpress AJAX function, “ajaxTester”:

Now you can receive the CURL POST parameters in your wordpress function and send a response:

Note: An example of running an AJAX request via CURL in WordPress, from Command Line:

You can observe the output for more info.

FacebookTwitterGoogle+LinkedInEmailShare

Use where clause at the end in joins

The result of the below 2 queries are different:

SELECT u.user_name FROM user u left join sales_people sp on u.id=sp.user_id where sp.profit=” and u.user_name like ‘%test%’ GROUP BY u.id

SELECT u.user_name FROM user u left join sales_people sp on u.id=sp.user_id and sp.profit=” where u.user_name like ‘%test%’ GROUP BY u.id

In the first query, we have the sequence: join, on, where, and
In the second query, we have the sequence: join, on, and, where

It is always recommended to use the second method of forming join queries, putting where clause at the last.
This way, we will get correct results.

FacebookTwitterGoogle+LinkedInEmailShare

Use in clause at the end in sub queries

The query (0.0020 sec):

SELECT u.user_name FROM user u where u.user_name like ‘%test%’ and u.id in (select user_id from user_role) GROUP BY u.id

is faster than the below query (0.0060 sec):

SELECT u.user_name FROM user u where u.id in (select user_id from user_role) and u.user_name like ‘%test%’ GROUP BY u.id

Reason could be due to the filtration of users by username, before doing the in() check.
So better put ‘in’ condition at the last.

FacebookTwitterGoogle+LinkedInEmailShare