Wordpress入门插件在刷新浏览器后阻止保存到数据库

时间:2015-03-29 20:43:17

标签: php wordpress caching browser plugins

我有两个问题:

1.在脚本中我从表单发送帖子数据。之后,数据将保存在数据库中。一切正常,但当用户在浏览器中按“刷新”按钮时,脚本会再次保存相同的数据。我发现我可以使用会话令牌,但在整个周末尝试之后,我不知道该怎么做。工作插件网站http://booword.uk4u.pl/ysp/

2.我想为WordPress编写插件,以供每个人使用作为入门脚本。所以我的第二个问题是如果这个脚本写得正确,你会怎么做。

<?php
/*
Plugin Name: Yoolek Starter Plugin
Plugin URI: http://yoolek.com/
Description: Yoolek Starter Plugin - simple starter plugin to use with shortcode [yoolek-starter-plugin]
Version: 1.0
Author:
Author URI: http://yoolek.com/
*/
defined( 'ABSPATH' ) or die( 'Plugin file cannot be accessed directly.' );

if ( ! class_exists( 'YoolekStarterPlugin' ) ) 
    {
    class YoolekStarterPlugin {



        protected $tag = 'yoolek-starter-plugin';

        protected $yoo_name;
        protected $yoo_year;

        protected $admin_option1;
        protected $admin_option2;

        public function __construct()
        {       
            /* we will use shortcode [yoolek-starter-plugin] in post or page */
            add_shortcode( $this->tag, array($this, 'run_yoolek' ) );

            add_action( 'admin_menu', array( $this, 'admin_menu' ) );
        }



        public function run_yoolek()
        {
            ob_start();/*to keep content correctly (not under shortcode) look down <?php return ob_get_clean();?>*/


            $this->create_form();
            $this->result();
            return ob_get_clean();
        } 


        /**********************************************************/
        private function result(){
            $this->admin_option1 = get_option("yoolek-starter-plugin-option1");/*admin option*/
            $this->admin_option2 = get_option("yoolek-starter-plugin-option2");/*admin option*/
            echo "admin option 1= ".$this->admin_option1."<br/>";
            echo "admin option 2= ".$this->admin_option2."<br/>";
            global $wpdb; /* save data to database and display*/
            $table_name = $wpdb->prefix . 'yoolek_starter_plugin';


            if (isset($_POST['yoo_submit'])) {
            $this->yoo_name = $_POST["yoo_name"];/* option*/
            $this->yoo_year = $_POST["yoo_year"];/* option*/
            $wpdb->insert($table_name, array(
                "time" => date("Y-m-d H:i:s"),
               "yoo_name" => $this->yoo_name,
               "yoo_year" => $this->yoo_year
            ));
            }
            $result = $wpdb->get_results("SELECT * FROM ".$table_name." ORDER BY time DESC");

                echo '<table class="table table-striped table-bordered table-condensed">';
                    echo '<thead>';
                        echo '<tr>';
                            echo '<th>ID</th>';
                            echo '<th>Time</th>';
                            echo '<th>Name</th>';
                            echo '<th>Year</th>';
                        echo '</tr>';
                    echo '</thead>';
                    echo '<tbody>';
                    foreach($result as $row){
                        echo '<tr>';
                            echo '<td>'.$row->id.'</td>';
                            echo '<td>'.$row->time.'</td>';
                            echo '<td>'.$row->yoo_name.'</td>';
                            echo '<td>'.$row->yoo_year.'</td>';
                        echo '</tr>';
                        };
                    echo '</tbody>';
                echo '</table>';         

        }
        /**********************************************************/

        /**********************************************************/
        private function create_form()
        {               
            ?><form class="form-horizontal" action="" method="post">
                <fieldset>


                <legend>Yoolek Starter Plugin Form</legend>

                <!-- Select Basic -->
                <br><br>

                <!-- Text input-->
                <div class="form-group">
                  <label class="col-xs-5 col-sm-3 control-label" for="yoo_name">Name:</label>  
                  <div class="col-xs-7 col-sm-7">
                  <input id="yoo-name" name="yoo_name" placeholder="your name" class="form-control input-md" required="" type="text" value="">

                  </div>
                </div>
                <div class="form-group">
                  <label class="col-xs-5 col-sm-3 control-label" for="yoo-year">Your lucky year:</label>
                  <div class="col-xs-7 col-sm-7">
                    <select id="yoo-year" name="yoo_year" class="form-control">
                        <option value="1991">1991</option>
                        <option value="1992" selected>1992</option>
                        <option value="1993">1993</option>
                    </select>
                  </div>
                </div>


                <!-- Button -->
                <div class="form-group">
                  <label class="col-xs-5 col-sm-4 control-label" for="submit"></label>
                  <div class="col-xs-7 col-sm-6">
                    <button id="submit" name="yoo_submit" class="btn btn-block btn-success">Submit</button>
                  </div>
                </div>

                </fieldset>
            </form>
            <br><br>
            <?php 



        }/*html_form*/ 


        /**********************************************************/
        /*ADMIN,PLUGIN HOOKS AND FUNCTIONS*/
        function settings_page() {


            /*update*/
                if (isset($_POST['option_update'])) {
                    check_admin_referer();//Tests if the current request was referred from an admin page
                    $variable1 = $_POST['field_option1'];
                    update_option("yoolek-starter-plugin-option1", $variable1);//update option
                    $variable2 = $_POST['field_option2'];
                    update_option("yoolek-starter-plugin-option2", $variable2);//update option
                    echo "<div class='updated'><p><strong>Yoolek Starter Plugin has been updated</strong></p></div>";
                }

            /**/
        ?><form method="post" action=" <?php admin_url('options-general.php?page=yoolek-starter-plugin.php')?> ">
                <div class="wrap">
                    <h2>YOOLEK STARTER PLUGIN SETTINGS<sup style='color:#D54E21;font-size:12px;'><?php //echo $yoolek_starter_plugin_version; ?></sup></h2>
                <div>
                    <label for="yoo_option1"  style="width:200px; line-height:23px; float:left;">option1</label>
                    <select name="field_option1" id="option1" width="200" style="width: 200px">
                        <option value="option nr1" <?php if (get_option("yoolek-starter-plugin-option1")== "option nr1") { ?>selected="true" <?php }; ?>>option nr1</option>
                        <option value="option nr2" <?php if (get_option("yoolek-starter-plugin-option1")== "option nr2") { ?>selected="true" <?php }; ?>>option nr2</option>
                        <option value="option nr3" <?php if (get_option("yoolek-starter-plugin-option1")== "option nr3") { ?>selected="true" <?php }; ?>>option nr3</option>    
                    </select>
                    <span class="setting-description"> Select option1.</span>
                    </div>
                    <div>
                    <label for="yoo_option2"  style="width:200px; line-height:23px; float:left;">option2</label>
                    <input type="text" width="200" style="width: 200px" size="16" maxlength="12" name="field_option2" id="option2" value="<?php echo get_option("yoolek-starter-plugin-option2"); ?>" />
                    <span class="setting-description"> Enter option2.</span>
                    </div>
                    <p class="submit">
                        <input name="option_update" value="Save Changes" type="submit" class="button-primary" />
                    </p>


                </div>
            </form><?php
        }
        function admin_menu () {
            add_options_page( 'Yoolek Starter Plugin Page', 'Yoolek Starter Plugin Menu', 'manage_options', 'yoolek-starter-plugin', array( $this, 'settings_page' ) );
        }


        static function plugin_activation() {

            /* create table in data base*/
            global $wpdb;
            $table_name = $wpdb->prefix . 'yoolek_starter_plugin';/* name z _*/
            $charset_collate = $wpdb->get_charset_collate();
            $sql = "CREATE TABLE $table_name (
                id mediumint(9) NOT NULL AUTO_INCREMENT,
                time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
                yoo_name tinytext NOT NULL,
                yoo_year text NOT NULL,
                UNIQUE KEY id (id)
            ) $charset_collate;";
            require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
            dbDelta( $sql );

            /*create_options*/
            /*add_option( $option, $value, $deprecated, $autoload );*/
            add_option('yoolek-starter-plugin-option1', 'option1Value1', '', 'yes');/*<----------------------------------- Creates new database field */
            add_option('yoolek-starter-plugin-option2', 'option1Value2', '', 'yes');/*<----------------------------------- Creates new database field */
        }

        static function plugin_deactivation() {

                    global $wpdb;
                    $table = $wpdb->prefix . 'yoolek_starter_plugin';
                    $wpdb->query("DROP TABLE IF EXISTS $table");

                    delete_option('yoolek-starter-plugin-option1');/*<----------------------------------------------------------- Deletes the database field */
                    delete_option('yoolek-starter-plugin-option2');/*<----------------------------------------------------------- Deletes the database field */
                }

    }/*class*/  
    new YoolekStarterPlugin;

    register_activation_hook( __FILE__, array( 'YoolekStarterPlugin', 'plugin_activation' ) );/*<------------------------------ Runs when plugin is activated */
    register_deactivation_hook( __FILE__, array( 'YoolekStarterPlugin', 'plugin_deactivation' ) );/*<---------------------------- Runs on plugin deactivation */
}/*if*/?>

1 个答案:

答案 0 :(得分:1)

第一眼引起我注意的快速事物:

1-您的插件缺少textdomain

2-你正在使用“eager init”,我推荐'lazy init'。在此处阅读更多内容:http://hardcorewp.com/2013/initializing-singleton-classes-used-in-wordpress-plugins/

3-您应该使用wpdb-&gt; prepare(特别是当您使用变量插入数据时)在Codex上阅读更多内容。

4-为了便于阅读,您的班级名称可能是“Yoolek_Starter_Plugin”。

5-您正在使用大量硬编码的“yoolek_ *”字符串。这通常很好,但是如果你想将它用作复制和开发多个插件的通用骨架插件,你必须考虑更通用的方法。

可能还有更多,这些都是我的头脑。