给wordpress额外添加一个编辑器

本文介绍了如何在WordPress模板中为特定文章添加一个独立的编辑器,通过functions.php文件实现元框、显示编辑器、保存数据等功能,以便于分开录入并调用内容。
摘要由CSDN通过智能技术生成

在制作wordpress模板时,有时会用到同一个文章需要分开录入内容,分别调用的情况,这个时候就需要给文章,再添加一个录入额外内容的编辑器。将下面的代码添加到functions.php中,就可以实现。

function wodepress_post_editor_meta_box() {    
   add_meta_box ( 
      'wpkj-post-editor', 
      __('文章顶部内容', 'textdomain') , 
      'wodepress_post_editor', 
      'post' // 需要显示编辑框的文章类型,与下文的两处 $_POST['post'] 对应
   );
 
}
add_action('admin_init', 'wodepress_post_editor_meta_box');
 
//Displaying the meta box
function wodepress_post_editor($post) {          
    $content = get_post_meta($post->ID, 'wodepress_post_editor', true);
 
    //This function adds the WYSIWYG Editor 
    wp_editor ( 
        $content , 
        'wodepress_post_editor', 
        array ( "media_buttons" => true ) 
    );
 
}
 
//This function saves the data you put in the meta box
function wodepress_post_editor_save_postdata($post_id) {
 
    if( isset( $_POST['wodepress_post_editor_nonce'] ) && isset( $_POST['post'] ) ) {
 
        //Not save if the user hasn't submitted changes
        if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return;
        } 
 
        // Verifying whether input is coming from the proper form
        if ( ! wp_verify_nonce ( $_POST['wodepress_post_editor_nonce'] ) ) {
            return;
        } 
 
        // Making sure the user has permission
        if( 'post' == $_POST['post'] ) {
            if( ! current_user_can( 'edit_post', $post_id ) ) {
                return;
            }
        } 
    }
 
    $content = get_post_meta($post_id, 'wodepress_post_editor', true);
    // 如果编辑器中有内容或者之前有数据才保存
    if( $content || !empty( $_POST['wodepress_post_editor'] ) ) {
 
        $data = $_POST['wodepress_post_editor'];
        update_post_meta($post_id, 'wodepress_post_editor', $data);
 
    }
}
add_action('save_post', 'wodepress_post_editor_save_postdata');

添加完了后,在录入文章时,就可以显示出来。在此编辑器中录入内容,在需要的地方调用出来就可以。

<?php
global $post;
$content = get_post_meta( $post->ID, 'wodepress_post_editor', true ); // 获取字段内容
if( $content ) { // 如果有内容
    echo $content;  // 输出内容
}
?>

 原文链接 https://www.zhanyes.com/code/6048.html

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值