添加编辑器到Wordpress主题页面


Add editor to Wordpress theme page

我添加了以下函数来在我的主题页面中呈现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)

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

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

这将导致媒体上传程序所需的资源在前端加载。

如果你只需要它在一个特定的页面上,你可以使用if_page()语句围绕wp_enqueue_media()只加载它在一个特定的页面

解决方案是将以下代码添加到functions.php中:

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

我喜欢@michaelrmcneill的解决方案。尽管您可以去掉条件逻辑,直接使用以下命令将其添加到admin_enqueue_scripts中:

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