微信咨询

微信咨询

13610*910*9

服务热线 7*24小时

电话咨询

wordpress 开发简单的留言板插件

iamdu2024-08-29 21:05:34 浏览: 697975
<?php
/*
Plugin Name: 留言板
Plugin URI: https://www.example.com/message-board
Description: 自动生成留言板的WordPress插件
Version: 1.0
Author: DU
Author URI: https://www.example.com
*/

// 当插件激活时,调用该函数创建数据库表格
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 );
}


// 注册短代码
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();
}

// 处理留言提交
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;
    }
}

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

?>

可以通过在页面中添加短代码”[message_board]”来展示留言板。如果首页index.php 则是 echo echo display_message_board()