Home  >  Article  >  CMS Tutorial  >  How to develop a WordPress plugin that automatically generates message boards

How to develop a WordPress plugin that automatically generates message boards

PHPz
PHPzOriginal
2023-09-06 09:09:151187browse

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
*/

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

?>

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 );
}

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>留言板</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();
}

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;
    }
}

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 '暂时没有留言';
    }
}

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:
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