使用可内容属性的WordPress主题(或插件)

时间:2012-11-01 作者:N2Mystic

我正在做一个实验,寻找一个利用contenteditable html5属性允许在live网站上进行上下文编辑(针对经过身份验证的管理员用户)。

有什么例子吗?

2 个回复
最合适的回答,由SO网友:Bainternet 整理而成

我不知道主题是什么,但我不久前写了一篇“概念教授”的文章,它简单地将内容包装在一个可编辑的div中,并将数据与nonce一起发送到一个隐藏的文本区域:

if (!class_exists(\'contentEditable\')){
    /**
    * contentEditable class
    * @author Ohad Raz
    */
    class contentEditable
    {
        public $nonce;
        public $nonce_action;

        /**
         * class constructor
         * @author Ohad Raz
         */
        function __construct()
        {
            if(!is_admin()){
                $this->nonce = \'ce__wpnonce\';
                $this->nonce_action = \'ce_editor\';
                $this->all_checks = false;
                add_filter(\'the_content\',array($this,\'editable_content\'),10);
                add_action(\'get_header\',array($this,\'check_submit\'));
            }

        }

        /**
         * check_submit
         * function to check if the form has been submited
         * if so call the update post function
         * @author Ohad Raz
         * @return void
         */
        public function check_submit(){
            if (isset($_POST[\'content_editable_Save\']) && $_POST[\'content_editable_Save\'] == "submit"){
                $this->updated_post();
            }
        }

        /**
         * editable_content
         * function that adds the form to the content
         * @author Ohad Raz 
         * @param  string $content 
         * @return string
         */
        public function editable_content($content){
            global $post;
            //early exit if not needed
            if (!current_user_can(\'edit_post\', $post->ID))
                return $content;

            wp_enqueue_script(\'jquery\');
            add_action(\'wp_footer\',array($this,\'submit_js\'));
            return \'<form action="contentEditable_submit" method="POST" accept-charset="utf-8">
                        <div id="editable" contenteditable="true">\'
                            .$content
                        .\'</div>
                        <textarea id="h_content" name="content" style="display:none;"></textarea>
                        <input type="hidden" name="pid" value="\'.$post->ID.\'">
                        <input type="hidden" name="content_editable_Save" value="submit">\'
                        .wp_nonce_field($this->nonce_action, $this->nonce,true, false)
                        .\'<input type="submit" id="submit_editable" name="save" value="\'.__(\'Save changes\').\'">
                    </form>\';
        }

        /**
         * updated_post
         * the function that actually updates the post and redirects to the newly modified post
         * @author Ohad Raz
         * @return VOID
         */
        public function updated_post(){
            if ($this->_verify_nonce()){

                $p[\'ID\'] = intval($_POST[\'pid\']);
                $p[\'post_content\'] = $_POST[\'content\'];
                $n = wp_update_post($p);
                wp_redirect(get_permalink($n));
                exit;
            }
        }

        /**
         * _verify_nonce 
         * nonce check
         * @author Ohad Raz
         * @return boolean
         */
        public function _verify_nonce()
        {
            $n = isset($_REQUEST[$this->nonce]) ? $_REQUEST[$this->nonce]: \'\';
            if (\'\' === $n) return false;
            if (!function_exists(\'wp_verify_nonce\'))
                require_once(ABSPATH .\'wp-includes/pluggable.php\');
            return wp_verify_nonce($n, $this->nonce_action);
        }

        /**
         * submit_js
         * some jQuery magic
         * @author Ohad Raz
         * @return void
         */
        public function submit_js(){
            ?>
            <script type="text/javascript">
                jquery(document).ready(function($){
                    $("#submit_editable").click(function(){
                        $("#h_content").val($("#editable").html());
                        return true;
                    });
                });
            </script>
            <?php
        }

    }//end class
}//end if
new contentEditable();

SO网友:mrwweb

签出插件。。。等待它。。。WPContentEditable. 我简单地浏览了源代码,它似乎确实使用了contenteditable属性,但我认为有一个后备方法。我想这应该由你来解决:)

结束

相关推荐

是否将ADD_EDITOR_STYLE定义为特定帖子类型?

我想知道是否可以为特定的帖子类型定义add\\u editor\\u样式?目前我正在使用此功能。。// EDITOR STYLE add_editor_style(\'editor-style.css\'); 但我想有多种编辑风格的各种职位类型。所以我猜可能是这样的。。。if ( ... ) { // EDITOR STYLE POSTS & PAGES add_editor_style(\'editor-style.css\'); }