QNimate

  • CoursesVideos
  • WP PremiumPlugins
  • DemosLab
  • Home
  • QIdea
  • QTrack
Home Carbon Ads How To Integrate Forms On WordPress

How To Integrate Forms On WordPress

Sometimes we might want to integrate subscription, contact, upload forms etc on WordPress front-end. These forms can make a POST request to the wordpress server. In this post I will show you how to handle POST requests in wordpress.


WordPress Requests

During a wordpress request WordPress takes the request URL and using regular expressions it find what to execute and display. Therefore we can make the change GET request as POST request and handle the POST data at a certain point of time needed and this will not harm the normal flow of wordpress.

POST Request to particular page

Suppose you want to display a email subscription form in a particular page then you can use this method:

Put this code inside the page:

    if(isset($_POST["email"]))
    {
        //do something
        echo "Successfully Subscribed"
    }
    else
    {
        ?>
            <form method="post" action="">
                <input type="email" name="email">  
            </form>
        <?php
    }

Here the page is going to POST back it itself. This method is for a particular URL post. Let’s see how implement POST forms via a plugin i.e., on any page or URL

POST Form via Plugin

Echo the form using shortcode. Pick up the post data using wordpress init action.

shortcode output

    if($form_response != null)
    {
        //do something
        echo "Successfully Subscribed"
    }
    else
    {
        ?>
            <form method="post" action="">
                <input type="email" name="email">  
            </form>
        <?php
    }

plugin file

    $form_response = null;
    function subscribe()
    {
        if(isset($_POST["email"]))
        {
            //do something
            $form_response = true;
        }
    }

    add_action("init", "subscribe");

init action is fired after wordpress loads(i.e., finishes registering actions and filters) and before producing output. This would be a nice place to handle the POST data.

A plugin shortcode could also have output the same code as for single page but I prefer this method as its more profession way of doing it.

Aug 9, 2014Narayan Prusty
Setting Up SEO Friendly WordPress Maintenance ModeAJAX In WordPress

Leave a Reply Cancel reply

To create code blocks or other preformatted text, indent by four spaces:

    This will be displayed in a monospaced font. The first four
    spaces will be stripped off, but all other whitespace
    will be preserved.
    
    Markdown is turned off in code blocks:
     [This is not a link](http://example.com)

To create not a block, but an inline code span, use backticks:

Here is some inline `code`.

For more help see http://daringfireball.net/projects/markdown/syntax

Narayan Prusty

I am a software engineer specialising in Blockchain, DevOps and Go/JavaScript. This is my personal blog where I write about things that I learn and feel interesting to share.

8 years ago WordPress
Share this
0
GooglePlus
0
Facebook
0
Twitter
0
Linkedin
  • WordPress Requests
  • POST Request to particular page
  • POST Form via Plugin
Related Articles
  • Express.js Middleware Tutorial
  • WordPress Settings API Tutorial with Examples
  • Login User into WordPress without Password
  • AJAX In WordPress
  • WordPress “Buy me a Beer” Plugin
Our Sponsor
My Books

2014 - 2015 © QNimate
All tutorials MIT license