简体   繁体   中英

Add editor to Wordpress theme page

I added the following function to render an Wordpress editor in my theme page:

<!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.

Javascript console output:

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:

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.

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

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

I liked @michaelrmcneill solution. Although you can cut out the conditional logic and simply add it to the admin_enqueue_scripts directly using:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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