wordpress - 如何在wordpress中创建多个内容 block ?

标签 wordpress

提前感谢您解释在管理面板的 WordPress 帖子编辑器中创建多个内容 block 的想法。我在询问之前尝试搜索类似的线程,但找不到任何答案。 我需要的是创建一个附加的内容字段以及默认的内容字段。请问我需要实现什么功能?我在 WordPress 插件库中找到了一个插件“Multiple Content Blocks”,但我相信这个简单的任务将需要更少的代码。我希望我已经很好地解释了我需要什么。再次感谢!

最佳答案

老问题,但由于这是我的谷歌搜索的第一次点击,我将添加我的方法 - 已接受的答案存在一些主要问题,主要是:

  • 当文档明确指出它对于 wp_editor() 不安全时,使用元框。
  • 使用 sanitize_text_field() 清理 html(这会删除所有 html!)

可以将此文件放入您的主题文件夹或functions.php 的底部,所有帖子和页面都将获得一个额外的编辑器。

如果使用单独的文件,请记住将其包含在您的functions.php中:

include __DIR__ . '/my_extra_content.php';

显然,您应该搜索“my_”并替换为有意义的内容 - “extra_content”也可能有点太通用了,所以想出一些更奇特的东西。

<?php
//Use a class to avoid conflicts
class my_extra_content {
    /**
     * Called on instantiation, this is where we hook functions
     */
    function __construct() {
        /* Using add_meta_box seems like the correct way to do this, but since
         * we're inserting a TinyMCE editor we cannot (should not) - from codex:
         * ---
         * Once instantiated, the WYSIWYG editor cannot be moved around in the
         * DOM. What this means in practical terms, is that you cannot put it in
         * meta-boxes that can be dragged and placed elsewhere on the page.
         * Instead use 'edit_page_form' (for pages) or 'edit_form_advanced'
         * (for other post types).
         */
        add_action( 'edit_page_form', array($this, 'my_extra_content_custom_box') );
        add_action( 'edit_form_advanced', array($this, 'my_extra_content_custom_box') );

        /* This one saves the content */
        add_action( 'save_post', array($this, 'save_postdata' ));
    }

    /**
     * This actually outputs the tinyMCE box
     */
    function my_extra_content_custom_box( $post ) {
        /* Always use a nonce */
        wp_nonce_field( 'my_extra_content_custom_box', 'my_extra_content_custom_box_nonce' );        

        /* Get the content */
        $content = self::get_content($post);

        /* Insert the editor */
        wp_editor( $content, "my_extra_content");
    }

    /**
     * Saves the content
     */
    function save_postdata( $post_id ) {
        /* Check that nonce was sent */
        if ( ! isset( $_POST['my_extra_content_custom_box_nonce'] ) ) {
            return $post_id;
        }
        /* Check that nonce is valid */
        if ( ! wp_verify_nonce( $_POST['my_extra_content_custom_box_nonce'], 'my_extra_content_custom_box' ) ) {
            return $post_id;
        }
        /* Don't try to do anything on autosave (custom fields aren't included) */
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return $post_id;
        }
        /* Check permissions */
        if ( 'page' === get_post_type( $post_id ) ) {
            if ( ! current_user_can( 'edit_page', $post_id ) ) {
                return $post_id;
            }
        } else {
            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                return $post_id;
            }
        }

        /* Sanitize content - we don't use sanitize_text_field() as it strips all
         * HTML, which is clearly not wanted with a wysiwyg - wp_kses_post()
         * should do what we want */
        $sane_content = wp_kses_post( $_POST['my_extra_content'] );

        /* Save content - notice the underscore in the meta name - it hides the
         * field from the "normal" custom field editor */
        update_post_meta( $post_id, '_my_extra_content_content', $sane_content );
    }

    /**
     *  Static function makes it easy to get the value wherever you need it.
     * - for example:
     * $my_extra_content = my_extra_content::get_content()
     */
    static function get_content($post_or_post_id = null) {
        /* First find the post id */
        $post_id = false;
        if ($post_or_post_id === null) {
            /* If nothing was passed, try to get it from global post object */
            global $post;
            $post_or_post_id = $post->ID;
        }
        if (is_a($post_or_post_id, 'WP_Post')) {
            /* If a post object was passed, or we're using the global $post */
            $post_id = $post_or_post_id->ID;
        } elseif (is_numeric($post_or_post_id)) {
            /* If a number (hopefully a post id) was passed */
            $post_id = intval($post_or_post_id);
        }

        /* Try to get the value */
        $value = get_post_meta($post_id, '_my_extra_content_content', true );

        /* If we didn't get a valid string return an empty one */
        if (!is_string($value)) {
            return '';
        }

        return $value;
    }
    /**
     * Static function to very easily output the content in a template
     * - for example:
     * my_extra_content::echo_content()
     */
    static function echo_content( $post_or_post_id = null ) {
        $output = self::get_content($post_or_post_id);

        /* do_shortcode makes sure we support shortcodes (if that is wanted) */
        // $output = do_shortcode($output);

        /* the_content filter will apply all normal filters (including
         * do_shortcode) to the content (not required!) */
        $output = apply_filters( 'the_content', $output);

        /* print it */
        echo $output;
    }
}

/* Instantiate the class - because of the static functions used to fetch the
 * content we won't need to ever use this variable, we just need __construct()
 * to be called, so our hooks are added */
$extra_content_throwaway_var = new my_extra_content();

关于wordpress - 如何在wordpress中创建多个内容 block ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19964695/

相关文章:

javascript - Wordpress Javascript 模态显示不关闭

php - 无法在响应式表格布局中显示表格标题

php - 在管理面板中显示前端的运输方式?

php - 如何从 PHP 文件向 Wordpress 菜单添加项目?

php - 在不同的 php 文件中访问 WordPress 函数?

PHP 5.3.3 - 未加载 Mysql 扩展

javascript - 无法使用javascript设置box-shadow

php - 如何允许用户每天投票一次而不是过去 24 小时?

php - WordPress 搜索框 + CMS 查询

c# - 如何在 UWP 上通过 rss 播放 YouTube