


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

Enable comments on your WordPress website to provide visitors with a platform to participate in discussions and share feedback. To do this, follow these steps: Enable Comments: In the dashboard, navigate to Settings > Discussions, and select the Allow Comments check box. Create a comment form: In the editor, click Add Block and search for the Comments block to add it to the content. Custom Comment Form: Customize comment blocks by setting titles, labels, placeholders, and button text. Save changes: Click Update to save the comment box and add it to the page or article.

How to copy WordPress subsites? Steps: Create a sub-site in the main site. Cloning the sub-site in the main site. Import the clone into the target location. Update the domain name (optional). Separate plugins and themes.

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

Enable comments in WordPress website: 1. Log in to the admin panel, go to "Settings" - "Discussions", and check "Allow comments"; 2. Select a location to display comments; 3. Customize comments; 4. Manage comments, approve, reject or delete; 5. Use <?php comments_template(); ?> tags to display comments; 6. Enable nested comments; 7. Adjust comment shape; 8. Use plugins and verification codes to prevent spam comments; 9. Encourage users to use Gravatar avatar; 10. Create comments to refer to

You can install the FTP plug-in through WordPress, configure the FTP connection, and then upload the source code using the file manager. The steps include: installing the FTP plug-in, configuring the connection, browsing the upload location, uploading files, and checking that the upload is successful.

How to copy WordPress code? Copy from the admin interface: Log in to the WordPress website, navigate to the destination, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code. Copy from a file: Connect to the server using SSH or FTP, navigate to the theme or plug-in file, select the code and press Ctrl C (Windows)/Command C (Mac) to copy the code.

WordPress Error Resolution Guide: 500 Internal Server Error: Disable the plug-in or check the server error log. 404 Page not found: Check permalink and make sure the page link is correct. White Screen of Death: Increase the server PHP memory limit. Database connection error: Check the database server status and WordPress configuration. Other tips: enable debug mode, check error logs, and seek support. Prevent errors: regularly update WordPress, install only necessary plugins, regularly back up your website, and optimize website performance.

How to turn off a comment in WordPress? Specific article or page: Uncheck Allow comments under Discussion in the editor. Whole website: Uncheck "Allow comments" in "Settings" -> "Discussion". Using plug-ins: Install plug-ins such as Disable Comments to disable comments. Edit the topic file: Remove the comment form by editing the comments.php file. Custom code: Use the add_filter() function to disable comments.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Chinese version
Chinese version, very easy to use

Notepad++7.3.1
Easy-to-use and free code editor