Using WordPress ‘enqueue_embed_scripts()’ PHP function

The enqueue_embed_scripts() WordPress PHP function enqueues embed iframe default CSS and JavaScript. It allows for the queuing of scripts for the embed iframe end using wp_enqueue_script(). In addition, it provides a PNG fallback CSS for embed iframes for legacy versions of Internet Explorer. This function is first run in oembed_head().

Usage

Here’s a simple usage example:

add_action( 'wp_enqueue_scripts', 'enqueue_embed_scripts' );

In this example, we are hooking the enqueue_embed_scripts() function to the wp_enqueue_scripts action, which is the proper way to enqueue scripts in WordPress.

Parameters

  • This function does not accept any parameters.

More information

See WordPress Developer Resources: enqueue_embed_scripts

Examples

Enqueueing Scripts for the Frontend

// In your theme's functions.php file
add_action( 'wp_enqueue_scripts', 'enqueue_embed_scripts' );

This code hooks the enqueue_embed_scripts() function to the wp_enqueue_scripts action, which is used to safely add/enqueue a CSS or JS file to the WordPress generated page.

Enqueueing Scripts for the Admin Area

// In your plugin's main file
add_action( 'admin_enqueue_scripts', 'enqueue_embed_scripts' );

This code is used to enqueue scripts for the WordPress admin area.

Enqueueing Scripts for the Login Page

// In your theme's functions.php file
add_action( 'login_enqueue_scripts', 'enqueue_embed_scripts' );

This code enqueues scripts for the WordPress login page.

Enqueueing Scripts for the Customizer

// In your theme's functions.php file
add_action( 'customize_controls_enqueue_scripts', 'enqueue_embed_scripts' );

This code is used to enqueue scripts for the WordPress theme customizer.

Enqueueing Scripts for the Embed Iframe

// In your theme's functions.php file
add_action( 'embed_head', 'enqueue_embed_scripts' );

This code hooks the enqueue_embed_scripts() function to the embed_head action, which is specifically used for the embed iframe.