给wordpress额外添加一个编辑器

在制作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

相关推荐

  1. wordpress额外添加一个编辑器

    2024-01-17 10:48:03       56 阅读
  2. 非插件方式为wordpress添加一个额外编辑器

    2024-01-17 10:48:03       38 阅读
  3. 非插件方式为wordpress添加一个额外编辑器

    2024-01-17 10:48:03       41 阅读
  4. 怎么服务器再额外附加一个 IP ?

    2024-01-17 10:48:03       54 阅读
  5. wordpress网站添加瀑布流效果

    2024-01-17 10:48:03       37 阅读
  6. electron 使用两个页面(额外添加一个html文件)

    2024-01-17 10:48:03       36 阅读
  7. wordpress添加限制游客浏览数量功能

    2024-01-17 10:48:03       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-17 10:48:03       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-17 10:48:03       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-17 10:48:03       87 阅读
  4. Python语言-面向对象

    2024-01-17 10:48:03       96 阅读

热门阅读

  1. 传感数据分析中的小波滤波:理论与公式

    2024-01-17 10:48:03       57 阅读
  2. 约瑟夫环问题解决

    2024-01-17 10:48:03       56 阅读
  3. [HarmonyOS]第一课:从简单的页面开始

    2024-01-17 10:48:03       55 阅读
  4. 讲解机器学习中的 K-均值聚类算法及其优缺点

    2024-01-17 10:48:03       54 阅读