How I Built a Simple Contact Form Plugin for WordPress from Scratch

👋 Introduction
Hey everyone! 👋

Today I’d like to share how I built a simple contact form plugin for WordPress from scratch. This is a perfect step-by-step guide for anyone who wants to get started developing plugins and understand how the magic behind…


This content originally appeared on DEV Community and was authored by danielfilemon

Image description👋 Introduction
Hey everyone! 👋

Today I’d like to share how I built a simple contact form plugin for WordPress from scratch. This is a perfect step-by-step guide for anyone who wants to get started developing plugins and understand how the magic behind WordPress really works.

This project is designed both for those who want to learn and for those who want to customize their own websites.

✨ What will we build?
✅ A simple contact form plugin
✅ Fields: name, email, message
✅ Sends the message directly to the WordPress site administrator’s email
✅ Easy to install and activate
✅ You can place the form anywhere using a shortcode

🗂️ Plugin structure
The project structure looks like this:

my-contact-form-plugin/
├── my-contact-form-plugin.php
└── readme.md
🧩 Plugin code
Inside my-contact-form-plugin.php, add:

<?php
/*
Plugin Name: My Contact Form
Description: A simple contact form plugin with shortcode
Version: 1.0
Author: Your Name
*/

// Prevent direct access
if ( ! defined( 'ABSPATH' ) ) exit;

/**

  • Render the form */ function mcf_render_form() { ob_start(); ?> Name: Email: Message: Send <?php return ob_get_clean(); }

/**

  • Handle the form submission
    */
    function mcf_handle_post() {
    if ( isset($_POST['mcf_submit']) ) {
    $name = sanitize_text_field($_POST['mcf_name']);
    $email = sanitize_email($_POST['mcf_email']);
    $message = sanitize_textarea_field($_POST['mcf_message']);

    $to      = get_option('admin_email');
    $subject = "New contact message from $name";
    $body    = "Name: $name\nEmail: $email\nMessage:\n$message";
    $headers = ["From: $name <$email>"];
    
    wp_mail($to, $subject, $body, $headers);
    
    // Simple confirmation
    add_action('wp_footer', function() {
        echo "<script>alert('Message sent successfully!');</script>";
    });
    

    }
    }
    add_action('init', 'mcf_handle_post');

// Register the shortcode
add_shortcode('my_contact_form', 'mcf_render_form');
🛠️ How to install
1️⃣ Upload the plugin folder to wp-content/plugins/
2️⃣ Activate it from the WordPress dashboard
3️⃣ In any page or post, insert this shortcode:

plaintext
Copiar
Editar
[my_contact_form]
✅ Done! The form will be working for your visitors.

🚀 How could it be improved?
This is a basic starter plugin, but you can expand it:

Add prettier styles with CSS

Improve validation (with regex, for example)

Add anti-spam protection (like Google reCAPTCHA)

Save messages to the WordPress database

Display submitted messages in the WordPress admin panel

👨‍💻 Code on GitHub
If you want to check the repository, contribute, or make suggestions:

👉 [danielfilemon]

🤝 Conclusion
Building plugins for WordPress may sound intimidating, but starting with something small — like a contact form — is an amazing way to understand the structure and the connection between WordPress and PHP.

I hope this tutorial helps you take your first steps! If you’d like to share ideas or collaborate, feel free to reach out here or on GitHub. 🚀


This content originally appeared on DEV Community and was authored by danielfilemon


Print Share Comment Cite Upload Translate Updates
APA

danielfilemon | Sciencx (2025-06-30T18:30:07+00:00) How I Built a Simple Contact Form Plugin for WordPress from Scratch. Retrieved from https://www.scien.cx/2025/06/30/how-i-built-a-simple-contact-form-plugin-for-wordpress-from-scratch/

MLA
" » How I Built a Simple Contact Form Plugin for WordPress from Scratch." danielfilemon | Sciencx - Monday June 30, 2025, https://www.scien.cx/2025/06/30/how-i-built-a-simple-contact-form-plugin-for-wordpress-from-scratch/
HARVARD
danielfilemon | Sciencx Monday June 30, 2025 » How I Built a Simple Contact Form Plugin for WordPress from Scratch., viewed ,<https://www.scien.cx/2025/06/30/how-i-built-a-simple-contact-form-plugin-for-wordpress-from-scratch/>
VANCOUVER
danielfilemon | Sciencx - » How I Built a Simple Contact Form Plugin for WordPress from Scratch. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/06/30/how-i-built-a-simple-contact-form-plugin-for-wordpress-from-scratch/
CHICAGO
" » How I Built a Simple Contact Form Plugin for WordPress from Scratch." danielfilemon | Sciencx - Accessed . https://www.scien.cx/2025/06/30/how-i-built-a-simple-contact-form-plugin-for-wordpress-from-scratch/
IEEE
" » How I Built a Simple Contact Form Plugin for WordPress from Scratch." danielfilemon | Sciencx [Online]. Available: https://www.scien.cx/2025/06/30/how-i-built-a-simple-contact-form-plugin-for-wordpress-from-scratch/. [Accessed: ]
rf:citation
» How I Built a Simple Contact Form Plugin for WordPress from Scratch | danielfilemon | Sciencx | https://www.scien.cx/2025/06/30/how-i-built-a-simple-contact-form-plugin-for-wordpress-from-scratch/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.