WordPress

How to Create a Custom Contact Form in WordPress

Wordpress plugin monir khan

Are you looking for a way to allow visitors to your WordPress site to contact you? A contact form is an easy way to do this, and plenty of plugins are available to help you create one. This tutorial will show you how to create a simple contact form using a custom plugin.

<?php
/*
Plugin Name: My Contact Form Plugin
Description: A simple contact form plugin for WordPress.
*/

This is the opening PHP tag and the plugin header, which is required for all WordPress plugins. The plugin header specifies the plugin name and description, which are displayed in the WordPress plugin directory and the WordPress dashboard.

function my_contact_form_shortcode() {
  ob_start();
  ?>
  <form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">
    <p>
      <label for="name">Name:</label><br>
      <input type="text" name="name" id="name" required>
    </p>
    <p>
      <label for="email">Email:</label><br>
      <input type="email" name="email" id="email" required>
    </p>
    <p>
      <label for="message">Message:</label><br>
      <textarea name="message" id="message" required></textarea>
    </p>
    <input type="hidden" name="action" value="my_contact_form_submit">
    <p>
      <input type="submit" value="Send Message">
    </p>
  </form>
  <?php
  return ob_get_clean();
}

The my_contact_form_shortcode function generates the HTML code for the contact form and returns it. The function uses the ob_start and ob_get_clean functions to capture the output of the HTML code and return it as a string. This allows us to return the HTML code as the value of the function, which is necessary for using it as a shortcode.

The form includes fields for the visitor’s name, email, and message, as well as a submit button. The form action is set to admin-post.php, which is a special WordPress script that allows plugin developers to handle form submissions. The action hidden field specifies which function should be called when the form is submitted.

add_shortcode( 'my_contact_form', 'my_contact_form_shortcode' );

This line registers the my_contact_form_shortcode function as a shortcode, which allows us to use the [my_contact_form] shortcode to display the contact form on our WordPress site.

function my_contact_form_submit() {
  // Validate form data
  if ( ! isset( $_POST['name'], $_POST['email'], $_POST['message'] ) ) {
    wp_die( 'Error: Invalid form data.' );
  }
  $name = sanitize_text_field( $_POST['name'] );
  $email = sanitize_email( $_POST['email'] );
  $message = sanitize_textarea_field( $_POST['message'] );

The my_contact_form_submit function is called when the form is submitted. It begins by validating the form data to ensure that all required fields are present. If any of the required fields are missing, the function displays an error message and terminates.

Related Posts

Creating a Tabbed Settings Page in a WordPress Plugin Using WPPB 10 Essential WordPress Plugins for WordPressers: A Guide to Optimizing Your Website Installing a WordPress Plugin: A Step-by-Step Guide for Newbies What is the best way to delete all products in WooCommerce? How to fix WordPress There has been a critical error on this website The Best Places to Get Royalty-Free Images for Your WordPress Blog Articles  How To Prevent WordPress Blog Content Scraping For Beginners  How To Plan Your WordPress Posts  How To Place Captions On Images In WordPress  WordPress Blog Email Newsletters: What, Why, and How  Are you new to WordPress and curious about managed WordPress hosting?  What You Can Do To Participate In The WordPress Project  What Is A Web Blog, And How Does It Differ From A Website?  What Effect Do Your Web Host’s PHP Updates Have on WordPress Sites?  What Are The Restrictions Of The WordPress. Com Platform?  The Best Places to Get Royalty-Free Images for Your WordPress Blog Articles  Step-by-Step Guide for Installing and Configuring WP Super Cache for New Users  Do you want your WordPress content to have images next to each other?  How to Select the Ideal Color Scheme for Your WordPress Website  How To Pick The Perfect Domain Name  How to Repair the WordPress Error Establishing a Database Connection  How To Fix Typical Image Problems In WordPress  How to Enlarge Images Without Compromising Quality  How to Download and Install Plugins and Themes for WordPress from GitHub  How to Delete Numbers from URLs Created with WordPress  The Step-By-Step Guide To Uploading PDF Files To Your WordPress Website Want to know how to remove digits from WordPress blog posts and URLs?  The Step-by-Step Guide to Linking to an Email Address in WordPress How Does Changing Your WordPress Theme Affect Things?  Getting Started With WordPress Comment Moderation: A Beginner’s Guide  How to Generate Branded Short URLs for Your WordPress Blog: A Step-by-Step Guide WordPress Categories Vs. Tags: How To Sort Your Content For SEO Step-by-Step Guide for New WordPress Users on How to Correctly Uninstall Plugins in WordPress WordPress Theme Framework: What Is It? All That Is Good, All That Is Bad  Images Theft: 4 Approaches to Protect Them in WordPress 7 Crucial Tips for Using WordPress Shortcodes How to Use File Transfer Protocol (FTP) to Upload Files to WordPress for Newbies 5 Easy Ways to Use WordPress to Get More Facebook Likes  How To Setup A WordPress Theme For Beginners How To Get Free WordPress Training In A Week How To Detect and Stop a DDoS Attack Against WordPress How To Install A WordPress Plugin: A Step-By-Step Guide For Newbies How to Delete All of Your Past DNS Records (Chrome, Mac, Windows) How to Create a Site-Specific WordPress Plugin How to Copyright And Trademark Your Blog Name and Logo How To Conduct An Appropriate Website Speed Test (8 Best Tools) To-Do List: 7 Items Before Going Public Your WordPress Site Fixing A Hacked WordPress Website: A Guide For Complete Beginners Tips For Deleting And Removing A WordPress Theme (Step By Step) How to Merge Two WordPress Websites Without Sacrificing Search Engine Optimization Comparing WordPress.Com With WordPress.Org: Which One Is Better? WordPress and GDPR Compliance: The Complete Guide – Everything You Need to Know Which One Is Better: A WordPress Plugin Or A Functions.php File? The Top 10 WordPress Bugs And How To Fix Them How To Making A Website Logo: Step-By-Step Instructions  11 Essential Pages Every WordPress Site Must Have In 2022 WordPress’s 15 Most Frustrating Issues and How to Fix Them WordPress vs. HTML Site- Which Website Design Is Best for Your Business? WordPress Users: 10 Email Marketing Mistakes to Avoid 5 Essential WordPress SEO Audit Checklist Items To Improve Your Rankings 2 Ways to Stop Users from Deactivating WordPress Plugins How to delete all the WooCommerce products with SQL Query? Pagination in WooCommerce: how to set it up? How to Add WooCommerce Product Categories to the Menu What’s the difference between WordPress.com and WordPress.org? How to apply WooCommerce Shortcodes to theme development How to skip the FTP credentials to install the theme and plugin on AWS EC2