Using WordPress ‘print_default_editor_scripts’ PHP action

The print_default_editor_scripts WordPress PHP action fires when the editor scripts are loaded for later initialization, after all scripts and settings are printed.

Usage

add_action('print_default_editor_scripts', 'your_custom_function');
function your_custom_function() {
    // your custom code here
}

Parameters

  • None

More information

See WordPress Developer Resources: print_default_editor_scripts

Examples

Enqueue custom editor script

Enqueue a custom script to enhance the editor functionality.

add_action('print_default_editor_scripts', 'enqueue_custom_editor_script');
function enqueue_custom_editor_script() {
    wp_enqueue_script('custom-editor-script', get_template_directory_uri() . '/js/custom-editor.js', array(), '1.0.0', true);
}

Load Google Fonts in the editor

Load Google Fonts in the editor to match the front-end styling.

add_action('print_default_editor_scripts', 'load_google_fonts_in_editor');
function load_google_fonts_in_editor() {
    wp_enqueue_style('google-fonts', 'https://fonts.googleapis.com/css?family=Roboto:400,700&display=swap', array(), null);
}

Add custom CSS to the editor

Inject custom CSS styles directly into the editor.

add_action('print_default_editor_scripts', 'add_custom_editor_styles');
function add_custom_editor_styles() {
    $custom_css = '.editor-styles-wrapper { background-color: #f5f5f5; }';
    wp_add_inline_style('wp-edit-blocks', $custom_css);
}

Register and enqueue a custom block

Register and enqueue a custom block script for the editor.

add_action('print_default_editor_scripts', 'register_custom_block');
function register_custom_block() {
    wp_register_script('custom-block', get_template_directory_uri() . '/js/custom-block.js', array('wp-blocks', 'wp-element', 'wp-editor'), '1.0.0', true);
    wp_enqueue_script('custom-block');
}

Add custom script translations

Add translations for custom scripts in the editor.

add_action('print_default_editor_scripts', 'load_custom_script_translations');
function load_custom_script_translations() {
    wp_set_script_translations('custom-editor-script', 'your-textdomain', plugin_dir_path(__FILE__) . 'languages');
}