php - 如何使自定义元框支持 WordPress 中的可视化编辑器?

标签 php wordpress meta-boxes

我正在为所有的 WordPress 帖子和页面使用可视化编辑器。但是我想在帖子编辑器的屏幕下制作一些自定义元框。其实我已经做了这些领域。但现在我想让这些字段在视觉 Composer 中可用。实际上我想在可视化编辑器中添加这些字段。我怎样才能做到这一点?请用您宝贵的知识帮助我。

这是我的元框代码

<?php

function myplugin_add_meta_box() {

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

foreach ( $screens as $screen ) {

    add_meta_box(
        'myplugin_sectionid',
        __( 'My Post Section Title', 'myplugin_textdomain' ),
        'myplugin_meta_box_callback',
        $screen
    );
 }
}
add_action( 'add_meta_boxes', 'myplugin_add_meta_box' );

function myplugin_meta_box_callback( $post ) {
wp_nonce_field( 'myplugin_save_meta_box_data', 'myplugin_meta_box_nonce'   );
$value = get_post_meta( $post->ID, '_my_meta_value_key', true );

echo '<label for="myplugin_new_field">';
_e( 'Description for this field', 'myplugin_textdomain' );
echo '</label> ';
echo '<input type="text" id="myplugin_new_field" name="myplugin_new_field" value="' . esc_attr( $value ) . '" size="25" />';
}

function myplugin_save_meta_box_data( $post_id ) {

if ( ! isset( $_POST['myplugin_meta_box_nonce'] ) ) {
    return;
}

// Verify that the nonce is valid.
if ( ! wp_verify_nonce( $_POST['myplugin_meta_box_nonce'], 'myplugin_save_meta_box_data' ) ) {
    return;
}

if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
    return;
}

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

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

} else {

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

/* OK, it's safe for us to save the data now. */

// Make sure that it is set.
if ( ! isset( $_POST['myplugin_new_field'] ) ) {
    return;
}

// Sanitize user input.
$my_data = sanitize_text_field( $_POST['myplugin_new_field'] );

// Update the meta field in the database.
update_post_meta( $post_id, '_my_meta_value_key', $my_data );
}
add_action( 'save_post', 'myplugin_save_meta_box_data' );

最佳答案

我看到您正在回应您的字段作为输入。您需要使用 wp_editor()代替功能。它将为您创建所见即所得(可视化编辑器)字段。

关于php - 如何使自定义元框支持 WordPress 中的可视化编辑器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32270907/

相关文章:

php - WordPress:如何在任何管理页面上显示元数据框?

php - 如何在 laravel 4 中检测客户所在国家/地区和地区

php - 从 php 代码向 MySQL 数据库添加元素

php - 浏览 phpMyAdmin fatal error : Failed opening Message. class.php

php - Wordpress SQL 选择错误

Php 创建 zip 文件(来自帖子附件 wordpress)

wordpress - 如何从前端保存带有元框的自定义帖子类型

php - 在 Woocommerce 单一产品页面的简短描述下显示自定义字段

php - 不回显先前值中记录的项目

php - WordPress 最近帖子的 MySql 查询需要链接到各个帖子