Using WordPress ‘get_embed_template()’ PHP function

The get_embed_template() WordPress PHP function retrieves an embed template path in the current or parent template.

Usage

$template_path = get_embed_template();

This will store the embed template path in the $template_path variable.

Parameters

  • None

More information

See WordPress Developer Resources: get_embed_template

Examples

Basic Usage

Retrieve the embed template path and store it in a variable.

$template_path = get_embed_template();
echo $template_path; // Displays the embed template path

Use with include to load the embed template

Load the embed template by including the template path in the current theme.

$template_path = get_embed_template();
include($template_path); // Includes and loads the embed template

Customize embed template path with a filter

Modify the embed template path using the embed_template filter.

add_filter('embed_template', 'my_custom_embed_template');

function my_custom_embed_template($template) {
    // Replace 'my-custom-embed.php' with your custom embed template file name
    return get_stylesheet_directory() . '/my-custom-embed.php';
}

$template_path = get_embed_template();
echo $template_path; // Displays the custom embed template path

Check if embed template exists

Check if the embed template file exists before using it.

$template_path = get_embed_template();

if (file_exists($template_path)) {
    include($template_path); // Includes and loads the embed template
} else {
    echo 'Embed template not found';
}

Load fallback template if embed template is not found

Load a fallback template if the embed template file does not exist.

$template_path = get_embed_template();

if (file_exists($template_path)) {
    include($template_path); // Includes and loads the embed template
} else {
    include(get_stylesheet_directory() . '/fallback-template.php'); // Includes and loads the fallback template
}