Table of Contents
留言板
Home CMS Tutorial WordPress How to develop a WordPress plugin that automatically generates message boards

How to develop a WordPress plugin that automatically generates message boards

Sep 06, 2023 am 09:09 AM
Automatic generated message board wordpress plug-in development

How to develop a WordPress plugin that automatically generates message boards

How to develop a WordPress plug-in that automatically generates message boards

When creating an interactive website, a message board is indispensable. On the WordPress platform, in order to facilitate users to add message functions, we can develop a plug-in that automatically generates message boards. This article will explain how to use WordPress plugin development to achieve this goal, and provide corresponding code examples.

Step 1: Create the plug-in folder and main file

First, we need to create a folder in the WordPress plug-in directory to place our message board plug-in. You can name this folder "message-board".

In this folder, create a main file, such as "message-board.php". This file will be the entry file for the plugin.

Next, add the following code to the "message-board.php" file:

<?php
/*
Plugin Name: 留言板
Plugin URI: https://www.example.com/message-board
Description: 自动生成留言板的WordPress插件
Version: 1.0
Author: Your Name
Author URI: https://www.example.com
*/

// 在这里编写插件的主要代码

?>
Copy after login

This code provides basic information to the plugin and creates a message board named "plug-in.

Step 2: Create a database table

In order to save the message data, we need to create a database table. This can be done by extending WordPress’ database.

In the main code block of the "message-board.php" file, add the following code:

// 当插件激活时,调用该函数创建数据库表格
register_activation_hook( __FILE__, 'create_message_board_table' );

function create_message_board_table() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'message_board';

    $charset_collate = $wpdb->get_charset_collate();

    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        author_name varchar(150) NOT NULL,
        message text NOT NULL,
        submit_date datetime NOT NULL,
        PRIMARY KEY  (id)
    ) $charset_collate;";

    require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
    dbDelta( $sql );
}
Copy after login

This code will call the "create_message_board_table" function when the plug-in is activated, which will create A database table named "wp_message_board". The table includes columns such as id, author_name, message, and submit_date.

Step 3: Create a message board page

Now we need to create a page in WordPress to display the message board. We can achieve this by adding a shortcode function to the plugin’s main file.

In the main code segment in the "message-board.php" file, add the following code:

// 注册短代码
add_shortcode( 'message_board', 'display_message_board' );

// 短代码函数
function display_message_board() {
    ob_start();
    ?>

    <h3 id="留言板">留言板</h3>

    <!-- 留言板表单 -->
    <form id="message_form" method="post" action="<?php echo esc_url( admin_url('admin-post.php') ); ?>">
        <input type="hidden" name="action" value="submit_message">
        <label for="author_name">姓名:</label>
        <input type="text" name="author_name" required>
        <br>
        <label for="message">留言:</label>
        <textarea name="message" cols="30" rows="5" required></textarea>
        <br>
        <input type="submit" value="提交">
    </form>

    <!-- 已提交的留言 -->
    <div id="message_list">
        <?php // 调用函数来展示已提交的留言 ?>
        <?php display_submitted_messages(); ?>
    </div>

    <?php
    return ob_get_clean();
}
Copy after login

In this code, we register a shortcode named "message_board" , and created a page to display the message board. The page contains a form for submitting new comments and a list for displaying submitted comments.

Step 4: Process form submission data

When the user submits a message, we need to save the message data to the database through a back-end processing function.

Continue to add the following code to the main code segment in the "message-board.php" file:

// 处理留言提交
add_action( 'admin_post_nopriv_submit_message', 'handle_message_submission' );
add_action( 'admin_post_submit_message', 'handle_message_submission' );

function handle_message_submission() {
    if ( 'POST' === $_SERVER['REQUEST_METHOD'] ) {
        global $wpdb;
        $table_name = $wpdb->prefix . 'message_board';

        $author_name = sanitize_text_field( $_POST['author_name'] );
        $message = sanitize_textarea_field( $_POST['message'] );

        $data = array(
            'author_name' => $author_name,
            'message' => $message,
            'submit_date' => current_time( 'mysql' )
        );

        $wpdb->insert( $table_name, $data );

        wp_redirect( get_permalink() );
        exit;
    }
}
Copy after login

In this code, we define a function named "handle_message_submission", Used to handle front-end form submissions. The function gets the author's name and message content from the form and inserts the data into the database. Finally, we redirect the page to the message board page.

Step 5: Display the submitted messages

Finally, we need to create a function to display the submitted messages.

Continue to add the following code to the main code segment in the "message-board.php" file:

function display_submitted_messages() {
    global $wpdb;
    $table_name = $wpdb->prefix . 'message_board';

    $results = $wpdb->get_results( "SELECT * FROM $table_name" );

    if ( $results ) {
        foreach ( $results as $result ) {
            echo '<div class="message">';
            echo '<p><strong>作者: </strong>' . esc_html( $result->author_name ) . '</p>';
            echo '<p><strong>留言: </strong>' . esc_html( $result->message ) . '</p>';
            echo '<p><strong>时间: </strong>' . esc_html( $result->submit_date ) . '</p>';
            echo '</div>';
        }
    } else {
        echo '暂时没有留言';
    }
}
Copy after login

In this code, we define a function named "display_submitted_messages", Used to retrieve submitted comments from the database and display them on the page.

So far, we have completed a WordPress plug-in that automatically generates message boards. After activating this plug-in in the plug-in management of the WordPress backend, you can display the message board by adding the short code "[message_board]" to the page.

I hope this article will be helpful to you in developing WordPress plug-ins!

The above is the detailed content of How to develop a WordPress plugin that automatically generates message boards. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to automatically generate directory page numbers for wps directory How to automatically generate directory page numbers for wps directory Feb 27, 2024 pm 04:01 PM

WPS is a powerful office software that can help us complete various office tasks efficiently. Among them, automatically generating table of contents page numbers is a very practical function. It can greatly improve the work efficiency of users, so the editor of this website will bring you this article to introduce in detail how to use WPS to automatically generate directory page numbers. I hope it can help everyone in need. How to automatically generate table of contents page numbers for a wps directory. First, open the wps group document, enter the content of the table of contents to be generated in the blank space, and then select the styles of title 1, title 2, and title 3 in the start menu bar. 2. Then after setting it up, we click the [Reference] function. After clicking, in the reference toolbar, here we click [Directory]; 3. Finally click

How to automatically generate a directory. How to set the format of the automatically generated directory. How to automatically generate a directory. How to set the format of the automatically generated directory. Feb 22, 2024 pm 03:30 PM

Select the style of the catalog in Word, and it will be automatically generated after the operation is completed. Analysis 1. Go to Word on your computer and click to import. 2After entering, click on the file directory. 3 Then select the style of the directory. 4. After the operation is completed, you can see that the file directory is automatically generated. Supplement: The table of contents of the summary/notes article is automatically generated, including first-level headings, second-level headings and third-level headings, usually no more than third-level headings.

How to Develop an Autoresponder WordPress Plugin How to Develop an Autoresponder WordPress Plugin Sep 05, 2023 am 08:49 AM

How to Develop an Auto-Reply WordPress Plugin With the popularity of social media, people’s demand for instant replies is also increasing. If you are a WordPress user, you may have experienced being unable to respond to messages or comments on your site in a timely manner. In order to solve this problem, we can develop an automatic reply WordPress plug-in, so that it can automatically reply to users' messages or comments on our behalf. This article will introduce how to develop a simple but practical autoresponder plug-in and provide code examples to help you understand

How to use PHP for automatic generation of API documentation How to use PHP for automatic generation of API documentation Jun 06, 2023 am 08:01 AM

With the continuous development of Internet technology, API has become an important way to realize data interaction between applications. In the process of writing APIs, document writing and maintenance inevitably become an important issue. However, the traditional way of manually writing and maintaining API documentation is inefficient and error-prone, and is not suitable for projects with continuous iteration. Using PHP to automatically generate API documents can effectively improve efficiency and reduce errors. This article will introduce how to use PHP to automatically generate API documents. Manual

How to automatically generate equals() and hashCode() methods using Records class in Java 14 How to automatically generate equals() and hashCode() methods using Records class in Java 14 Jul 31, 2023 pm 01:52 PM

How to automatically generate equals() and hashCode() methods using Records class in Java14 In Java programming, we often need to write equals() and hashCode() methods for our classes. These two methods are very important when dealing with equality and hash codes of objects. To simplify this process, Java14 introduces a new Records class. The Records class provides a way to simplify writing equals() and hashCode

How to develop a WordPress plugin that automatically generates e-books How to develop a WordPress plugin that automatically generates e-books Sep 05, 2023 am 08:01 AM

How to develop a WordPress plug-in that automatically generates e-books. With the popularity of social media and e-readers, e-books have become one of the important ways for people to obtain and share knowledge. As a WordPress developer, you may be faced with the need to create and publish e-books. To simplify this process, we can develop a WordPress plugin that automatically generates e-books. This article will teach you how to develop such a plug-in and provide code examples for reference. Step 1: Create the basic file structure of the plugin first

How to automatically generate document number in excel How to automatically generate document number in excel Mar 20, 2024 am 09:51 AM

The form that only adds data is the most basic form. Some document forms with fixed content will be more complicated. In the documents produced by Excel, some contents must automatically generate some data for the convenience of recording for continuity and authenticity. We often see The invoices and machine-printed invoice numbers are automatically consecutive to prevent tax evasion. How are these document numbers automatically generated? Let’s listen to the explanation below. For example, when generating the number of an outbound order, a combination of English letters and numbers is usually used. The English letter part of the number may use the capital form of the first letter of the document type. For example, the outbound document can be prefixed with CK. Method 1: Use the simplest rand function, which can generate a random number between 0-1. So our encoding

How to add custom widgets in WordPress plugin How to add custom widgets in WordPress plugin Sep 05, 2023 am 10:49 AM

How to Add Custom Widgets in WordPress Plugin WordPress is a powerful and flexible content management system (CMS) that is widely used in various types of websites such as blogs, news websites, and e-commerce websites. One very useful feature is to add custom widgets for displaying various features and content in the sidebar, footer, or other areas of your website. This article will introduce how to add custom widgets in WordPress plugins. Here is a simple step and code example to help you better

See all articles