简体   繁体   中英

wp_enqueue_media() call causing fatal error

I'm trying to create a popup that's launched from the tinyMCE toolbar.

When I add wp_enqueue_media() call to the content of the popup, I get the following exception with the trace:

[28-May-2015 01:32:00 UTC] PHP Fatal error:  Call to undefined function wp_get_current_user() in /Applications/MAMP/htdocs/devblog/wp-includes/capabilities.php on line 1385
[28-May-2015 01:32:00 UTC] PHP Stack trace:
[28-May-2015 01:32:00 UTC] PHP   1. {main}() /Applications/MAMP/htdocs/devblog/wp-admin/media-upload.php:0
[28-May-2015 01:32:00 UTC] PHP   2. require_once() /Applications/MAMP/htdocs/devblog/wp-admin/media-upload.php:16
[28-May-2015 01:32:00 UTC] PHP   3. require_once() /Applications/MAMP/htdocs/devblog/wp-admin/admin.php:31
[28-May-2015 01:32:00 UTC] PHP   4. require_once() /Applications/MAMP/htdocs/devblog/wp-load.php:37
[28-May-2015 01:32:00 UTC] PHP   5. require_once() /Applications/MAMP/htdocs/devblog/wp-config.php:80
[28-May-2015 01:32:00 UTC] PHP   6. include_once() /Applications/MAMP/htdocs/devblog/wp-settings.php:215
[28-May-2015 01:32:00 UTC] PHP   7. ZipRecipes::zrdn_iframe_content($post_info = *uninitialized*, $get_info = *uninitialized*) /Applications/MAMP/htdocs/devblog/wp-content/plugins/zip-recipes/zip-recipes.php:48
[28-May-2015 01:32:00 UTC] PHP   8. ZipRecipes::view($name = *uninitialized*, $args = *uninitialized*) /Applications/MAMP/htdocs/devblog/wp-content/plugins/zip-recipes/class.ziprecipes.php:1348
[28-May-2015 01:32:00 UTC] PHP   9. include() /Applications/MAMP/htdocs/devblog/wp-content/plugins/zip-recipes/class.ziprecipes.php:1613
[28-May-2015 01:32:00 UTC] PHP  10. wp_enqueue_media($args = *uninitialized*) /Applications/MAMP/htdocs/devblog/wp-content/plugins/zip-recipes/views/create-update-recipe.php:51
[28-May-2015 01:32:00 UTC] PHP  11. get_allowed_mime_types($user = *uninitialized*) /Applications/MAMP/htdocs/devblog/wp-includes/media.php:2910
[28-May-2015 01:32:00 UTC] PHP  12. current_user_can($capability = *uninitialized*) /Applications/MAMP/htdocs/devblog/wp-includes/functions.php:2292

My main plugin file has this (relevant code):

if (strpos($_SERVER['REQUEST_URI'], 'media-upload.php') && strpos($_SERVER['REQUEST_URI'], '&type=z_recipe') && !strpos($_SERVER['REQUEST_URI'], '&wrt='))
{
    ZipRecipes::zrdn_iframe_content($_POST, $_REQUEST);
    exit;
}

and zrdn_iframe_content generates an plain .php file with only html and the following php in it:

<?php
// jQuery
wp_enqueue_script('jquery');
// This will enqueue the Media Uploader script
wp_enqueue_media();
?>

What am I doing wrong to cause this?

You need to move the wp_enqueue_media() to be inside of a function that doesn't get run until WordPress has loaded the necessary files. This is explained further in the WP Codex for the function.

For example:

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

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