Support » Fixing WordPress » How to create custom conatct form plugin

  • Hi,

    I am new on wordpress, i want to create a conact form which shown on front end.So how can i define the form action to other page ? Below my code Please check

    <?php
    defined('ABSPATH') or die("restricted access");
    ?>
    <div class="signup-content clearfix">
        	<h4>Let's Get Started</h4>
             <form name="signup" id="signup" method="post" action="<?php echo plugins_url('frontend/form-process.php',_FILE_); ?>">       
                <?php wp_nonce_field( 'rstar_contact_form_action','rstar_contact_form' ); ?>
                
                <div class="signup-field"><input name="first_name" id="first_name_signup" type="text" value="<?php echo $first_name;?>" placeholder="First Name *"></div><br>
                <div><input name="submit" type="submit" id="signup_button" value="Submit"></div>
            </form>
        </div>

    [Moderator note: code fixed. Please wrap code in the backtick character or use the code button.]

    • This topic was modified 3 years, 11 months ago by t-p.
    • This topic was modified 3 years, 11 months ago by t-p.
Viewing 5 replies - 1 through 5 (of 5 total)
  • You can just submit it to the same page with leaving the action field empty
    and doing that above the form
    <?php
    if(isset($_POST[‘submit’])){
    then you can look into the wp_mail function here https://developer.wordpress.org/reference/functions/wp_mail/
    }
    ?>
    if you want to do it with a plugin i recommend you to use contact form 7

    Thread Starter Rupam Hazra

    (@rupamhazra)

    @omarkasem

    Hi if i want to put below code on another page then what should be the form action because i need that.

    <?php
    if(isset($_POST[‘submit’])){
    then you can look into the wp_mail function here https://developer.wordpress.org/reference/functions/wp_mail/
    }
    ?>

    Please help me.

    Moderator bcworkz

    (@bcworkz)

    Hi Rupam,

    If you want the form data to go to another page, then the action attribute should be the permalink of whatever page is to process the data. If you do not wish to hardcode the permalink, you will need to know something about the page in order to get its permalink. Its ID, slug, something. From the standpoint of a plugin developer, it’s not that simple to know what page this might be, which is in part why omarkasem suggested submitting to the same page. The same page is something you can be assured of in any context. Other pages, not so much.

    The one thing you should not do is submit directly to a PHP code page by filename. While this seems logical and is commonly done outside of WP environments, doing so makes it impossible to properly initiate the WP environment. If your page makes no use of any WP resources, submitting to a filename may work. If you need WP resources, this is the wrong approach. If you do not like the permalink approach nor the same page approach, you are left with two possibilities. One is to use Ajax, where the action attribute is ignored. The other is to submit through /wp-admin/admin-post.php. This last option is similar to Ajax in a way, except you do not use JavaScript or jQuery. Your form will then need a hidden field named “action” where the value is used in part to define an action hook tag to which you add the form handler callback function.

    Thread Starter Rupam Hazra

    (@rupamhazra)

    @bcworkz,

    Thank you for your suggestion.i like the ajax way to submit the form .

    Could you say about ajax.

    Thanks.

    Moderator bcworkz

    (@bcworkz)

    You picked probably the most complex method, but it also makes for a very nice user experience, so it’s well worth learning how to set this up in WP. It’s somewhat different than the way it’s set up on static sites. I suggest you first code a very simple generic routine to ensure you have the basics working before throwing in more complicating factors. You may want to change the form’s submit button to a static HTML element. This prevents the form from being submitted conventionally when Submit is clicked. A conventional submit can be stopped by jQuery, but I prefer to not let it happen to begin with.

    I recommend you start working through the explanation in the Plugin Handbook. For anyone else following along, the same code will work in a child theme as well.

    To see the initial content the examples use, go back one page to the jQuery explanation. You’ll see it’s still a form, but not at all like yours. All that matters is the element IDs and names are used properly. The form is not even required except to get the radio buttons to work correctly.

    Once you get a basic round trip of data working, you can start adding in your actual functionality. I’d only add a little at a time before testing so when something goes wrong, there’s not too far to dig to find the error. Note that I said “when” not “if”. Errors will happen. Work in a way that makes them easier to find and correct. Apparently some coders will work up a huge routine, which inevitably has a bunch of errors. They then work on resolving each error until it all works. I don’t know how anyone can work that way! It’s one small step at a time for me. YMMV.

    Getting Ajax working for the first time can be frustrating. There are many elements that need to come together. It’s also very rewarding when you do get it working. If you get stuck, you know where to get help 🙂 Coding questions should be posted in the Developing with WordPress forum. Good luck and enjoy!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to create custom conatct form plugin’ is closed to new replies.