Notepad sticky plugin

时间:2016-07-21 作者:mysticalghoul

我有以下代码为前端用户提供一个文本区域,将其用作页面上的注释,以编写要记住的内容。但这段代码只保存了一个便笺,用户可以随时编辑它。我的问题是,是否有办法将内容保存到某个地方,并在每次按提交时都有一个新的内容。或添加新的粘滞按钮。

       require_once( plugin_dir_path( __FILE__ ) . \'wp-ajax.php\' );
        class notepad_stikey extends WP_Ajax {
            var $user;
            var $username;
            function __construct() {
                parent::__construct();
                add_action(\'init\', array( &$this, \'setup\') );
            }
            function setup() {
                $this->user = get_current_user_id();
                $this->username = get_userdata( $this->user )->user_login;
                $this->notes = get_user_meta( $this->user, \'notepad_stikey\', true );
                $this->ph = \' \'; //placeholder
                if (empty( $this->notes )) {
                    $this->notes = $this->ph;
                }
                add_action(\'wp_enqueue_scripts\', array( &$this, \'scripts\') );
            }
            function scripts() {
                wp_enqueue_script( \'notepad_stikey\', plugins_url( \'notepad_stikey.js\', __FILE__ ), array( \'jquery\' ) );
                wp_localize_script( \'notepad_stikey\', \'notepad_stikey\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );
            }
            function an_change_notepad_stikey() {
                $notes = trim( $_POST[\'notes\'] );
                //if notes is empty, delete
                if ( empty($notes) ) {
                    if ( delete_user_meta( $this->user, \'notepad_stikey\' ) )
                        die( \'notes deleted\' );
                }
                //notes are the same, notes = placeholder, or resaving empty notes
                if ( $notes == $this->notes || $notes == $this->ph || ( empty($notes) && $this->notes == $this->ph) )
                    die();
                //update
                if ( update_user_meta( $this->user, \'notepad_stikey\', $notes ) )
                    die( \'updated\' );
                //hopefully, we don\'t get this far. if we do, something is wrong
                die( \'uh oh. notes could not be saved\' );
            }
        }
        global $notepad_stikey;
        $notepad_stikey = new notepad_stikey();
        add_action( \'widgets_init\', \'notepad_stikey_load\' );
        function notepad_stikey_load() {
            register_widget( \'notepad_stikey_Widget\' );
        }
        class notepad_stikey_Widget extends WP_Widget {
            function notepad_stikey_Widget() {
                $widget_ops = array(\'classname\' => \'notepad_stikey\', \'description\' => __( \'notepad_stikey. Only one instance please. Else this will break.\', \'notepad_stikey\' ) );
                $control_ops = array( \'id_base\' => \'notepad_stikey\' );
                parent::WP_Widget( \'notepad_stikey\', __( \'notepad_stikey\', \'notepad_stikey\' ), $widget_ops, $control_ops );
            }
            function widget( $args, $instance ) {
                extract( $args, EXTR_SKIP );
                echo $before_widget;
                global $notepad_stikey;
                $username = $notepad_stikey->username;
                $notes = $notepad_stikey->notes;
                //overwrite title
                $instance[\'title\'] = \'Notepad for \'. $username;
                echo $instance[\'hide_title\'] ? \'\' : $before_title . $instance[\'title\'] . $after_title;
                echo "<div id=\'notepad_stikey\' class=\'$username\' style=\'border: 1px solid #eee; padding: 10px 15px;min-height: 100px;\'>";
                echo $notes;
                echo \'</div><span style="float:left;color:#008;" id="notepad_stikey_response"></span><small style="float:right;">click box above to edit</small>\';
                echo $after_widget;
            } //end widget()
            function update($new_instance, $old_instance) {
                $instance = $old_instance;
                $instance[\'title\'] = esc_attr( $new_instance[\'title\'] );
                $instance[\'hide_title\'] = (bool) $new_instance[\'hide_title\'] ? 1 : 0;
                return $instance;
            } //end update()
            function form( $instance ) {
                $instance = wp_parse_args( (array) $instance, array( \'hide_title\' => 0 ) );
                extract( $instance );
                ?>
                <p>
                    <input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id(\'hide_title\'); ?>" name="<?php echo $this->get_field_name(\'hide_title\'); ?>"<?php checked( $hide_title ); ?> />
                    <label for="<?php echo $this->get_field_id(\'hide_title\'); ?>"><?php _e(\'Hide Title?\', \'notepad_stikey\' );?></label>
                </p>
                <?php
            } //end form()
        }



       if (!class_exists(\'WP_Ajax\')) {
   class WP_Ajax {
function __construct( $ajax_prefix = \'a\', $nopriv_prefix = \'n\' ) {
    $regex = "/^($ajax_prefix)?($nopriv_prefix)?_|^($nopriv_prefix)?  ($ajax_prefix)?_/";
    $methods = get_class_methods( $this );
    foreach ( $methods as $method ) {
        if ( preg_match( $regex, $method, $matches ) ) {
            if ( count( $matches ) > 1 ) {
                $action = preg_replace( $regex, \'\', $method );
                if ( count( $matches ) == 3 ) {
                    add_action( "wp_ajax_$action", array( $this, $method )    );
                    add_action( "wp_ajax_nopriv_$action", array( $this, $method ) );
                } else {
                    if ( $matches[1] == $ajax_prefix ) {
                        add_action( "wp_ajax_$action", array( $this, $method ) );
                    } else {
                        add_action( "wp_ajax_nopriv_$action", array( $this,    $method ) );
                    }
                }
            }
        }
    }
}
   }

   }



     jQuery(document).ready(function($) {
     $(\'#notepad_stikey\').click(function(e) {
       tag = e.target.tagName;
        if ( tag != \'TEXTAREA\' && tag != \'INPUT\' ) {
        contents = $(this).html();
        $(this).html( \'<textarea rows="5" cols="50" style="display:block;width:98%;height:100px;">\' + contents + \'</textarea><input type="submit" class="save" style="position:relative;z-index:99" />\' );
    }
   });
  $(\'#notepad_stikey input.save\').live( \'click\', function() {
    new_contents = $(this).siblings(\'textarea\').val();
    $(\'#notepad_stikey\').html( new_contents );
    change_notepad_stikey( new_contents );
    return false;
   });
    function change_notepad_stikey( notes ) {
    $(\'#notepad_stikey_response\').text( \'...\' );
    $.post(notepad_stikey.ajaxurl,
        {
            \'action\' : \'change_notepad_stikey\',
            \'notes\' : notes
        }, function(response) {
            //if (response != \'\') {
                //alert( response );
                $(\'#notepad_stikey_response\').text( response );
            //}
        }, \'text\' );
    }
    });

1 个回复
最合适的回答,由SO网友:Navin Bhudiya 整理而成

您需要为存储便笺创建单独的表,并将用户便笺保存到该表中,这样您可以管理用户的多个便笺。我想这个主意对你有帮助。

相关推荐

显示作者姓名PHP(自制插件)

我有一个需要帮助的问题,因为我自己找不到解决办法。我接管了一个网站,之前有人在那里创建了一个自制插件。。使用默认插件“Contact Form 7”,用户可以在页面上创建帖子。()https://gyazo.com/c8b20adecacd90fb9bfe72ad2138a980 )关于自行创建的插件“Contact Form 7 extender”,帖子是通过PHP代码在后台生成的(https://gyazo.com/115a6c7c9afafd2970b66fd421ca76a3)其工作原理如下:如果