【问题标题】:How to properly use wp_editor() from frontend如何从前端正确使用 wp_editor()
【发布时间】:2017-05-08 07:49:53
【问题描述】:

我正在尝试为我的 wordpress 主题的自定义页面模板创建一个简单的表单,但问题是我无法获得 tinymce 编辑器的工具栏。我已经搜索并找到了一些资源,但对我没有任何帮助。这是我的代码

            <?php
        $content = 'Start typing to create.';
        $editor_id = 'ic_colmeta_editor';
        $settings =   array(
            'wpautop' => true,
            'media_buttons' => true,
            'textarea_name' => $editor_id, 
            'textarea_rows' =>get_option('default_post_edit_rows', 10), 
            'tabindex' => '',
            'editor_css' => '', 
            'editor_class' => '',
            'teeny' => true,
            'dfw' => true,
            'tinymce' => true,
            'quicktags' => true 
            );
        wp_editor( $content, $editor_id, $settings); ?>

在function.php中

wp_enqueue_script( 'tinymce_js', includes_url( 'js/tinymce/' ) . 'wp-tinymce.php', array( 'jquery' ), false, true );

我还尝试通过添加 wp-config.php 来删除连接脚本

define('CONCATENATE_SCRIPTS', false);

但是结果总是和please view the image一样

工具栏不见了。那么问题来了,我该如何解决呢?

提前致谢。

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    您是否尝试使用 tiny_mce 的依赖项加载脚本?类似的东西

    add_action( 'wp_enqueue_scripts', 'my_scripts' );
    
    function my_scripts() {
        wp_enqueue_script( 'my-script', get_stylesheet_directory_uri() . '/js/custom_script.js', array( 'jquery', 'tiny_mce' ) );
    }
    

    然后在custom_script.js 中,您应该能够使用 TinyMCE 功能(您可以将代码放在此文件中)。

    https://developer.wordpress.org/reference/functions/wp_enqueue_script/

    【讨论】:

    • 是的,我试过了,但没有任何反应,你能帮我提供 TinyMCE 功能的 js 代码吗
    • 尝试添加一个带有类的文本区域,然后在您的脚本中使用 tinyMCE.init(); 在该特定类上启动它。
    【解决方案2】:

    其实就是这么简单:

    比如把普通的textare变成wp_advanced编辑器就不需要3rd方编辑器左右了,只要把普通的html代码改成wp代码如下:

    我们需要改变:

    &lt;textarea rows="5" cols="50" name="taxBtmDesc" id="taxBtmDesc"&gt;&lt;?php echo esc_attr( $term_btm_desc ) ? esc_attr( $term_btm_desc ) : ''; ?&gt;&lt;/textarea&gt;

    到这个简单的代码:

    &lt;?php wp_editor($term_btm_desc, 'taxBtmDesc'); ?&gt;

    而最终的结果会是这样的:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-23
      • 2016-02-03
      • 2015-03-31
      • 2021-05-26
      相关资源
      最近更新 更多