如何将记录从我的自定义管理页面中的自定义表单插入到自定义表格中?

时间:2015-07-05 作者:NickC

我正在努力解决这一问题,而我搜索到的所有相关的抄本、文章和论坛帖子(堆栈或其他)的链接都没有帮助。

我可以将我的问题描述为试图复制原生的“添加新用户”Wordpress功能。一、 e.从管理页面呈现表单,从此表单插入新记录(相当于添加新用户)并更新表。

到目前为止,我已经:

Add Episodes Form

目前的插件代码如下所示:

<?php
    /*

    Plugin Name: Episodes

    Description: The episodes plugin

    Author: Nick Courage

    Version: 1.0

    */

    function activate_episodes(){

        global $wpdb;

        $table_name = $wpdb->prefix . \'episodes\';

        if($wpdb->get_var(\'SHOW TABLES LIKE\' . $table_name) != $table_name){

            $sql = \'CREATE TABLE \' . $table_name . \'(

                episode_id INT NOT NULL AUTO_INCREMENT,

                episode_title VARCHAR(45) NOT NULL,

                episode_desc VARCHAR(45) NOT NULL,

                air_date DATE NOT NULL,

                img_url VARCHAR(255) NOT NULL,

                PRIMARY KEY (episode_id)

                )\';

    require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');

    dbDelta($sql);

    add_option(\'episode_database_version\', \'1.0\');

    }
    }

    register_activation_hook(__FILE__,\'activate_episodes\');

    function episodes_add_page(){

        ?>
        <h1>Add Episodes</h1>
        <form method="$_POST" action="">
            <label for="episode_title">Episode Title:</label>
            <input type="text" name="episode_title" id="episode_title" />
            <label for="episode_desc">Episode Description:</label>
            <input type="text" name="episode_desc" id="episode_desc" />
            <label for="air_date">Air date:</label>
            <input type="text" name="air_date" id="air_date" />
            <input type="submit" value="Submit"/>
        </form>


        <?php

    }

    function episodes_plugin_menu(){

        add_menu_page(\'Episodes Page\', \'Episodes\', \'manage_options\',\'episodes-plugin\',\'episodes_add_page\');

    }

    add_action(\'admin_menu\',\'episodes_plugin_menu\');




    function add_episode(){

        global $wpdb;

        $table_name = $wpdb->prefix . \'episodes\';

        $wpdb->insert($table_name, array(\'episode_title\'=>$_POST[\'episode_title\'],

            \'episode_desc\'=>$POST[\'episode_desc\'],

            \'air_date\'=>$_POST[\'air_date\'],

            \'img_url\'=>$_POST[\'img_url\']

            )

        );

    }

    ?>

2 个回复
SO网友:Milo

使用admin-post action hook 拦截POST请求并处理表单。这个admin_url() 函数可用于输出正确的URL,并使用wp_redirect() 处理后重定向回插件页面。

帮你自己和你的用户一个忙——当本机表和API可以使用时,不要重新发明轮子和添加表。

SO网友:Mario Burga
    <?php
    /*

    Plugin Name: Episodes

    Description: The episodes plugin

    Author: Nick Courage

    Version: 1.0

    */

function activate_episodes()
{

    global $wpdb;

    $table_name = $wpdb->prefix . \'episodes\';

    if ($wpdb->get_var(\'SHOW TABLES LIKE\' . $table_name) != $table_name) {

        $sql = \'CREATE TABLE \' . $table_name . \'(

                episode_id INT NOT NULL AUTO_INCREMENT,

                episode_title VARCHAR(45) NOT NULL,

                episode_desc VARCHAR(45) NOT NULL,

                air_date DATE NOT NULL,

                img_url VARCHAR(255) NOT NULL,

                PRIMARY KEY (episode_id)

                )\';

        require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');

        dbDelta($sql);

        add_option(\'episode_database_version\', \'1.0\');
    }
}

register_activation_hook(__FILE__, \'activate_episodes\');

function episodes_add_page()
{
    global $wpdb;
    if (
        !empty($_POST)
        && $_POST[\'episode_title\'] != \'\'
        && $_POST[\'episode_desc\'] != \'\'
        && $_POST[\'air_date\'] != \'\'
    ) {

        $table_name = $wpdb->prefix . \'episodes\';
        $episode_title = sanitize_text_field($_POST[\'episode_title\']);
        $episode_desc = sanitize_text_field($_POST[\'episode_desc\']);
        $air_date = sanitize_text_field($_POST[\'air_date\']);

        $wpdb->insert(
            $table_name,
            array(
                \'episode_title\' => $episode_title,
                \'episode_desc\' => $episode_desc,
                \'air_date\' => $air_date
            )
        );
    }
    ?>

    <h1>Add Episodes</h1>
    <form method="post" action="?page=episodes-plugin">
        <label for="episode_title">Episode Title:</label>
        <input type="text" name="episode_title" id="episode_title" />
        <label for="episode_desc">Episode Description:</label>
        <input type="text" name="episode_desc" id="episode_desc" />
        <label for="air_date">Air date:</label>
        <input type="text" name="air_date" id="air_date" />
        <input type="submit" value="Submit" />
    </form>

<?php
}



function episodes_plugin_menu()
{


    add_menu_page(
        "Paginas episodios",
        "Episodios",
        "manage_options",
        "episodes-plugin",
        "episodes_add_page",
        "dashicons-video-alt3",
        75
    );
}



add_action(\'admin_menu\', \'episodes_plugin_menu\');
结束