Using WordPress ‘print_embed_scripts()’ PHP function

The print_embed_scripts() WordPress PHP function prints the JavaScript in the embed iframe header.

Usage

To use the function, simply call it within your WordPress template file:

print_embed_scripts();

Parameters

  • There are no parameters for this function.

More information

See WordPress Developer Resources: print_embed_scripts()

Examples

Adding the Function to a Custom Template

Create a custom WordPress template to display embed content, and use the print_embed_scripts() function to load the necessary JavaScript.

// custom-embed-template.php
get_header('embed');

// Add the print_embed_scripts function
print_embed_scripts();

// Display the embed content
the_content();

get_footer('embed');

Conditionally Loading the Embed Scripts

Load the embed scripts only if a specific post type is being displayed.

if (get_post_type() == 'custom_post_type') {
    print_embed_scripts();
}

Customizing Embed Scripts

Add a custom JavaScript snippet to the embed scripts.

function my_custom_embed_scripts() {
    print_embed_scripts();
    echo '<script>console.log("Custom embed script loaded");</script>';
}
my_custom_embed_scripts();

Adding Embed Scripts to a Hook

Add the print_embed_scripts() function to a WordPress action hook.

add_action('wp_footer', 'print_embed_scripts', 20);

Using the Function with a Custom Filter

Apply a custom filter to modify the embed scripts before they are printed.

function my_custom_filter($scripts) {
    // Modify the $scripts variable as needed
    return $scripts;
}
add_filter('embed_scripts', 'my_custom_filter');

print_embed_scripts();