答案实际上是几个步骤,我做了很多实验,但归根结底是以下代码,您需要使用webpack之类的工具进行编译并提取库,等等:
// MediaUploadFilter.js
import { MediaUpload } from \'@wordpress/media-utils\'
const { addFilter } = window.wp.hooks
const replaceMediaUpload = () => MediaUpload
addFilter(
\'editor.MediaUpload\',
\'core/edit-post/components/media-upload/replace-media-upload\',
replaceMediaUpload
)
本质上,我已经剥离了这个类,让您看到它在init hook期间被调用:
<?php
public function __construct()
{
add_action(\'admin_menu\', [$this, \'setAdminMenus\']);
// ...
}
public function setAdminMenus(): void
{
$pageMenuObject = add_menu_page(
\'Classmate Profile\',
\'Classmate Profile\',
\'edit_classmates_profile\',
\'classmate-profile\',
[$this->displayHandler, \'displayPage\'], // a function loading a view
\'dashicons-admin-users\',
21
);
add_action(\'admin_enqueue_scripts\', static function ($hook) use ($pageMenuObject) {
/**
* Make sure that we load the scripts specific to the add to release page
*/
if ($hook === $pageMenuObject) {
$pageMenuData = ClassmateProfileData::getInstance(); // Grabs data for user
$allPageData = [];
if ($pageMenuData) {
$allPageData = $pageMenuData->getPageData() ?: [];
}
static::enqueueLibraryAssets(); // See below abstract function I pasted below
wp_enqueue_script(
\'plugin-classmate-profile\',
mix_plugin_uri(\'/assets/js/ClassmateProfile.js\'),
[\'plugin-media-upload-filter\', \'wp-editor\', \'wp-block-editor\'],
PLUGIN_VERSION,
true
);
wp_localize_script(
\'plugin-classmate-profile\',
\'ClassmateProfileData\',
$allPageData
);
}
});
}
// This is from the parent abstract class for the class I am calling it in
public static function enqueueLibraryAssets(): void
{
wp_enqueue_style(
\'plugin-application\',
mix_plugin_uri(\'assets/css/app.css\'),
[\'wp-edit-blocks\'],
PLUGIN_VERSION
);
wp_enqueue_media(); // MUST HAVE THIS! Loads the files you need...
wp_enqueue_script(
\'plugin-library-manifest\',
mix_plugin_uri(\'assets/js/manifest.js\'),
[\'lodash\', \'react\', \'react-dom\', \'wp-components\', \'wp-i18n\', \'wp-element\', \'wp-editor\'],
PLUGIN_VERSION,
true
);
wp_enqueue_script(
\'plugin-library-vendor\',
mix_plugin_uri(\'assets/js/vendor.js\'),
[\'plugin-library-manifest\'],
PLUGIN_VERSION,
true
);
wp_enqueue_script(
\'plugin-media-upload-filter\',
mix_plugin_uri(\'assets/js/MediaUploadFilter.js\'),
[\'plugin-library-manifest\', \'plugin-library-vendor\'],
PLUGIN_VERSION,
true
);
}