为什么tinyMCE.editors不能在wordpress的tinyMCE上工作

时间:2014-07-20 07:03:15

标签: wordpress-plugin wordpress

<?php 
/*
Plugin Name: Taggr
Plugin URI: http://example.com/wordpress-plugins/my-plugin
Description: Premium plugin for image and photo tagging
Version: 1.0
Author: Tee Kai Yang
Author URI: http://wrox.com
License: GPLv2
*/


add_action('init', 'register_tagging_post');

//create taggr post type

 function register_tagging_post(){

       $tagging_args = array(
           'public' => true,
           'supports' => array(
               'title',
               'thumbnail'

           ), 
           'query_var' => 'tagging',
           'rewrite' => array(
              'slug' => 'taggr',
              'with_front' => false
           ),
            'labels' => array(
            'name' => 'Taggr Albums',
            'singular_name' => 'Album',
            'add_new' => 'Add New Album',
            'add_new_item' => 'Add New Album',
            'edit_item' => 'Edit Album',
            'new_item' => 'New Album',
            'view_item' => 'View Album',
            'search_items' => 'Search Albums',
            'not_found' => 'No Albums Found',
            'not_found_in_trash' => 'No Albums Found In Trash'
        ),

        'menu_icon' => plugins_url() . '/taggr/images/PhotoAlbum.png',

        'menu_position' => 80, 

       );

       register_post_type('tagging', $tagging_args);


 }

    add_action( 'add_meta_boxes', 'taggr_create_meta' );
 function taggr_create_meta(){

     add_meta_box( 'taggrMeta', 'Upload and edit photos/images', 'taggr_meta_function', 'tagging', 'normal', 'high' );

     function taggr_meta_function(){


     ?>

       <?php wp_editor("Enter your text", "my_editor"); ?>

    <script>
    window.onload = function(){
        alert(tinyMCE.editors[0].getContent());
    }
    </script>

        <?php

    }


    }


?> 

上面是我的代码,我在元数据箱中创建了一个自定义帖子和一个tinyMCE文本编辑器。但是为什么在执行alert(tinyMCE.editors[0].getContent());行时,它不起作用,也不显示编辑器的内容。萤火虫显示TypeError: tinyMCE.editors[0] is undefined

的错误

1 个答案:

答案 0 :(得分:0)

您可以提供编辑textarea的ID来获取tinyMCE的内容,您也应该获取已保存的post meta的内容并将其设置为编辑元素的文本以显示之前保存的值。您可以使用以下代码作为参考,

<?php
function taggr_meta_function($post){
 $my_editor = get_post_meta($post->ID, 'my_editor', true);
 wp_editor( $my_editor, "my_editor", array( 'textarea_name' => "my_editor" ) );
?>
   <script>
    window.onload = function(){
        alert(tinyMCE.get('my_editor').getContent());
    }
   </script>
<?php } ?>