简体   繁体   English

将编辑器添加到Wordpress主题页面

[英]Add editor to Wordpress theme page

I added the following function to render an Wordpress editor in my 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>

The good thing is that it adds a working editor to the page. 好处是,它向页面添加了一个有效的编辑器。 But the "add media" button isn't working. 但是“添加媒体”按钮不起作用。 I tried a few things: adding additional custom Wordpress JS for the editor, but still without result. 我尝试了几件事:为编辑器添加其他自定义Wordpress JS,但仍然没有结果。

Javascript console output: 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)

Add this code to your theme's functions.php file: 将此代码添加到主题的functions.php文件中:

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

This will cause the media uploader's required assets to load on the front end. 这将导致媒体上载器所需的资产加载到前端。

If you only need it on one specific page, you can use an if_page() statement around wp_enqueue_media() to only load it on a specific page. 如果你只需要一个特定的页面上,你可以使用一个if_page()周围声明wp_enqueue_media()只加载一个特定的页面上。

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

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

I liked @michaelrmcneill solution. 我喜欢@michaelrmcneill解决方案。 Although you can cut out the conditional logic and simply add it to the admin_enqueue_scripts directly using: 尽管您可以删除条件逻辑并将其简单地直接添加到admin_enqueue_scripts中,但可以使用:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM