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

时间:2013-11-13 21:26:31

标签: wordpress

提前感谢解释在管理面板中的Wordpress帖子编辑器中创建多个内容块的想法。我在尝试之前尝试搜索类似的线程,但找不到任何答案。 我需要的是创建一个额外的内容字段和默认的内容字段。我需要实现哪些功能?我在wordpress插件库中找到了一个插件“Multiple Content Blocks”,但我相信这个简单的任务需要更少的代码。我希望我已经很好地解释了我需要的东西。再次感谢!

2 个答案:

答案 0 :(得分:1)

首先将内容编辑器添加到Wordpress编辑页面要比听起来要难得多,所以如果您不熟悉保存/更新周期和元数据库,那么我建议使用插件。我喜欢“高级自定义字段”,但我确信“多个内容块”也很好。

无论如何,我在这里概述了一般的Custom Meta Box解决方案。所以我们走了:

wp_editor()函数是我们用来创建编辑器实例的函数。 http://codex.wordpress.org/Function_Reference/wp_editor

但是,我会在元框内调用它 http://codex.wordpress.org/add_meta_box

以下是一些示例代码,用于创建包含内容编辑器的元框。

此插件将内容编辑器的值存储在名为_hurtigtech_extra_content的自定义字段中,该字段在更新帖子/页面时保存。

您可以将此插件放入插件文件夹/wp-content/plugins/并在其中播放。如果你需要帮助,请随意发表评论,我知道这是很多代码所以插件可能是最好的,但如果你有信心,这也是一个很好的基线。

<?php
/**
 * Plugin Name: Extra Metabox Content Editor
 */

/**
 * Adds a box to the main column on the Post and Page edit screens.
 */
function hurtigtech_add_custom_box() {

    $screens = array( 'post', 'page' );

    foreach ( $screens as $screen ) {

        add_meta_box(
            'hrutigtech_extra_content_section',
            __( 'My Post Extra Content', 'hurtigtech_translations' ),
            'hurtigtech_inner_custom_box',
            $screen
        );
    }
}
add_action( 'add_meta_boxes', 'hurtigtech_add_custom_box' );

/**
 * Prints the box content.
 * 
 * @param WP_Post $post The object for the current post/page.
 */
function hurtigtech_inner_custom_box( $post ) {

  // Add an nonce field so we can check for it later.
  wp_nonce_field( 'hurtigtech_inner_custom_box', 'hurtigtech_inner_custom_box_nonce' );

  /*
   * Use get_post_meta() to retrieve an existing value
   * from the database and use the value for the form.
   */
  $value = get_post_meta( $post->ID, '_hurtigtech_extra_content', true );
  echo '<br />';
  wp_editor( $value, "hurtigtech_extra_content_editor");
}

/**
 * When the post is saved, saves our custom data.
 *
 * @param int $post_id The ID of the post being saved.
 */
function hurtigtech_save_postdata( $post_id ) {

  /*
   * We need to verify this came from the our screen and with proper authorization,
   * because save_post can be triggered at other times.
   */

  // Check if our nonce is set.
  if ( ! isset( $_POST['hurtigtech_inner_custom_box_nonce'] ) )
    return $post_id;

  $nonce = $_POST['hurtigtech_inner_custom_box_nonce'];

  // Verify that the nonce is valid.
  if ( ! wp_verify_nonce( $nonce, 'hurtigtech_inner_custom_box' ) )
      return $post_id;

  // If this is an autosave, our form has not been submitted, so we don't want to do anything.
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) 
      return $post_id;

  // Check the user's permissions.
  if ( 'page' == $_POST['post_type'] ) {

    if ( ! current_user_can( 'edit_page', $post_id ) )
        return $post_id;

  } else {

    if ( ! current_user_can( 'edit_post', $post_id ) )
        return $post_id;
  }

  /* OK, its safe for us to save the data now. */

  // Sanitize user input.
  $mydata = sanitize_text_field( $_POST['hurtigtech_extra_content_editor'] );

  // Update the meta field in the database.
  update_post_meta( $post_id, '_hurtigtech_extra_content', $mydata );


}
add_action( 'save_post', 'hurtigtech_save_postdata' );

注意:内容框背景存在样式问题。这需要添加到editor-style.css文件以修复它。

.hurtigtech_extra_content_editor {
     background: #fff;
}

答案 1 :(得分:1)

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

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

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

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

include __DIR__ . '/my_extra_content.php';

显然你应该搜索&#34;我的_&#34;并用有意义的东西取代 - &#34; extra_content&#34;也可能有点过于通用,所以想出一些更具异国情调的东西。

<?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();