Creating a simple form WordPress plugin involves several steps. Below is a basic outline of how to create a simple form plugin:
Plan Your Plugin:
Determine the purpose and functionality of your form plugin.
Decide what fields your form will include (e.g., name, email, message).
Set Up Your Development Environment:
Install WordPress locally or on a server for testing and development.
Set up a code editor for writing your plugin code.
Create a New Directory for Your Plugin:
Choose a unique name for your plugin (e.g., "Simple Form Plugin").
Create a new directory in the wp-content/plugins directory of your WordPress installation (e.g., simple-form-plugin).
Create the Main Plugin File:
Inside your plugin directory (simple-form-plugin), create a main PHP file with the same name as your plugin directory and a .php extension (e.g., simple-form-plugin.php).
Add necessary plugin header information at the top of your main PHP file, including the plugin name, description, version, author, etc.
Add Form Functionality:
Define a function to output the form HTML code.
Use WordPress hooks (e.g., add_shortcode) to integrate your form into WordPress.
Use WordPress functions to handle form submission and processing (e.g., wp_mail to send email notifications).
Here's an example of a simple form plugin:
php
Copy code
<?php
/*
Plugin Name: Simple Form Plugin
Description: A simple form plugin for WordPress.
Version: 1.0
Author: Your Name
*/
// Define shortcode to display the form
function simple_form_shortcode() {
ob_start(); ?>
<form action="<?php echo esc_url( admin_url('admin-post.php') ); ?>" method="post">
<input type="hidden" name="action" value="process_simple_form">
<label for="name">Name:</label>
<input type="text" name="name" id="name" required><br>
<label for="email">Email:</label>
<input type="email" name="email" id="email" required><br>
<label for="message">Message:</label>
<textarea name="message" id="message" required></textarea><br>
<input type="submit" value="Submit">
</form>
<?php
return ob_get_clean();
}
add_shortcode('simple_form', 'simple_form_shortcode');
// Process form submission
function process_simple_form() {
if ( isset($_POST['name']) && isset($_POST['email']) && isset($_POST['message']) ) {
$name = sanitize_text_field( $_POST['name'] );
$email = sanitize_email( $_POST['email'] );
$message = sanitize_textarea_field( $_POST['message'] );
// Send email notification
$to = 'your@email.com';
$subject = 'New Form Submission';
$body = "Name: $name\nEmail: $email\nMessage: $message";
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail($to, $subject, $body, $headers);
// Redirect after form submission
wp_redirect( home_url('/') );
exit;
}
}
add_action('admin_post_process_simple_form', 'process_simple_form');
add_action('admin_post_nopriv_process_simple_form', 'process_simple_form');
Replace 'your@email.com' with your desired email address where you want to receive form submissions.
This is a basic example to get you started. You can expand and customize the form and functionality according to your specific requirements. Remember to test your plugin thoroughly before deploying it on a live site.
Comments
Post a Comment