Home >Backend Development >PHP Tutorial >Develop custom WordPress plugins using PHP

Develop custom WordPress plugins using PHP

王林
王林Original
2023-05-26 11:40:542046browse

With the development of WordPress, more and more users need to customize the functions of WordPress websites. To meet this need, developing your own WordPress plugin is a good option. In this article, we will discuss how to develop custom WordPress plugins using PHP.

First of all, let us first understand the structure of WordPress plug-in. In WordPress, plugins are implemented through a folder and must contain a specified file. This file is plugin-name.php, where "plugin-name" is the name of the plugin. In this file, we can define the name, version number, author and other information of the plug-in.

In addition to the plugin-name.php file, we can create other PHP files in the plugin folder. These files can contain code for our plugin functionality. Within our main files, we can use the add_action() and add_filter() functions to link the functionality in these files with WordPress-specific events and hooks.

Below we will use a simple plug-in example to demonstrate this process. In this plugin, we will create a simple contact form and add it to a WordPress page.

The first step is to create the plug-in folder

First create a folder named "my-contact-form" in the wp-content/plugins/ directory. This will be our plug-in the root directory.

The second step is to create the main file plugin-name.php

Create the file plugin-name.php in the my-contact-form folder. In this file, we will define the basic information of the plugin (such as name, version, author), etc. and will connect our functional code.

The following is the basic content of plugin-name.php:

<?php
/*
Plugin Name: My Contact Form
Plugin URI: http://mywebsite.com/
Description: A simple contact form plugin
Version: 1.0
Author: John Doe
Author URI: http://mywebsite.com/
*/

// Our plugin's code will go here.

Next, we will add some functional code, including the ability to add a contact form to the WordPress page.

When our plug-in is loaded by WordPress, the init function is executed. So we need to add the init function to the main file and add an add_shortcode function to it for adding the contact form to the WordPress page.

Here is the final plugin-name.php file:

<?php
/*
Plugin Name: My Contact Form
Plugin URI: http://mywebsite.com/
Description: A simple contact form plugin
Version: 1.0
Author: John Doe
Author URI: http://mywebsite.com/
*/

function my_contact_form_shortcode() {
    // Code to create the contact form
}

add_shortcode( 'my_contact_form', 'my_contact_form_shortcode' );

Now that we have created a simple contact form functionality, the way to add it to your WordPress page is to use a shortcode " [my_contact_form]".

The third step is to create the contact form function code

Now, let us create a PHP file containing the contact form. Create a file named "contact-form.php" in the my-contact-form folder and add the following code to this file:

<?php
function my_contact_form_shortcode() {

    $output = '';

    // Check if the submitted form isset
    if( isset( $_POST['my_contact_submit_button'] ) ) {
        // Sanitize the submitted data
        $name = sanitize_text_field( $_POST['my_contact_name'] );
        $email = sanitize_email( $_POST['my_contact_email'] );
        $message = esc_textarea( $_POST['my_contact_message'] );

        // Send the email
        $to = 'myemail@example.com';
        $subject = 'New Contact Form Submission';
        $body = "Name: $name 

Email: $email 

Message: $message";
        $headers = array('From: ' . $name . ' <' . $email . '>');
        wp_mail( $to, $subject, $body, $headers );

        // Set a confirmation message
        $output = '<p style="color: green;">Your message has been sent.</p>';
    }
    else {
        // Display the contact form
        $output .= '<form method="post">
            <p>
                <label for="my_contact_name">Name</label><br/>
                <input type="text" name="my_contact_name" required>
            </p>
            <p>
                <label for="my_contact_email">Email</label><br/>
                <input type="email" name="my_contact_email" required>
            </p>
            <p>
                <label for="my_contact_message">Message</label><br/>
                <textarea name="my_contact_message" required></textarea>
            </p>
            <p>
                <input type="submit" name="my_contact_submit_button" value="Submit">
            </p>
        </form>';
    }

    return $output;

}

This code implements a simple contact form , when the user clicks the "Submit" button, it sends the name, email, and message to the specified email address. If the submission is successful, a confirmation message will be displayed on the page.

Now, we need to edit the main file again to connect the function code in contact-form.php with WordPress. We can use the require_once() function in the init function to link them to the main file.

Here is the final plugin-name.php file:

<?php
/*
Plugin Name: My Contact Form
Plugin URI: http://mywebsite.com/
Description: A simple contact form plugin
Version: 1.0
Author: John Doe
Author URI: http://mywebsite.com/
*/

function my_contact_form_shortcode() {
  // Code to create the contact form
}

add_shortcode( 'my_contact_form', 'my_contact_form_shortcode' );

function my_contact_form_scripts() {
  wp_enqueue_style( 'my-contact-form', plugins_url( 'my-contact-form/css/style.css' ) );
}
add_action('wp_enqueue_scripts', 'my_contact_form_scripts');

require_once( plugin_dir_path( __FILE__ ) . 'contact-form.php' );

Now, we have successfully added the contact form functionality to WordPress. If you follow the above steps exactly, you will definitely see the new plugin in WordPress and be able to add a contact form on your page using shortcodes.

In short, developing your own WordPress plug-in can not only help you add and customize functions in WordPress, but it is also a good way to learn PHP. Hope this article helps you get started writing your own WordPress plugin!

The above is the detailed content of Develop custom WordPress plugins using PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Interface design in PHPNext article:Interface design in PHP