【问题标题】:Add editor to Wordpress theme page将编辑器添加到 Wordpress 主题页面
【发布时间】:2014-07-10 18:10:04
【问题描述】:

我添加了以下函数来在我的主题页面中呈现一个 Wordpress 编辑器:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<?php wp_editor( $content, 'editor', $settings = array() ); ?>


<!-- scripts -->
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
</body>
</html>

好处是它为页面添加了一个工作编辑器。但是“添加媒体”按钮不起作用。我尝试了一些方法:为编辑器添加额外的自定义 Wordpress JS,但仍然没有结果。

Javascript 控制台输出:

Uncaught TypeError: Cannot read property 'audio' of undefined (media-views.min.js?ver=3.9.1:1)
Uncaught TypeError: Cannot read property 'id' of undefined (media-audiovideo.min.js?ver=3.9.1:1)

【问题讨论】:

    标签: javascript php jquery wordpress wordpress-theming


    【解决方案1】:

    将此代码添加到您主题的functions.php 文件中:

    function add_media_upload() {
        if ( is_admin() ) {
             return;
           }
        wp_enqueue_media();
    }
    add_action('wp_enqueue_scripts', 'add_media_upload');
    

    这将导致媒体上传器所需的资源加载到前端。

    如果您只需要在一个特定页面上加载它,您可以在 wp_enqueue_media() 周围使用 if_page() 语句来仅在特定页面上加载它。

    【讨论】:

      【解决方案2】:

      解决办法是在functions.php中加入如下代码:

      wp_enqueue_script(array('jquery', 'editor', 'thickbox', 'media-upload'));
      

      【讨论】:

        【解决方案3】:

        我喜欢@michaelrmcneill 解决方案。虽然您可以删除条件逻辑并简单地将其添加到 admin_enqueue_scripts 直接使用:

        add_action('admin_enqueue_scripts', function()
        {
            wp_enqueue_media();
        });
        

        【讨论】:

          猜你喜欢
          • 2014-12-09
          • 1970-01-01
          • 1970-01-01
          • 2013-09-10
          • 1970-01-01
          • 2016-05-24
          • 2016-10-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多