Using WordPress ‘load_default_embeds’ PHP filter

The load_default_embeds WordPress PHP Filter allows you to control the loading of default embed handlers.

Usage

add_filter('load_default_embeds', 'my_custom_load_embeds');
function my_custom_load_embeds($maybe_load_embeds) {
    // your custom code here
    return $maybe_load_embeds;
}

Parameters

  • $maybe_load_embeds (bool) – Whether to load the embeds library. Default is true.

More information

See WordPress Developer Resources: load_default_embeds

Examples

Disable default embeds

Prevent loading the default embed handlers for a lightweight website.

add_filter('load_default_embeds', '__return_false');

Load default embeds only for logged-in users

This example allows loading default embed handlers only for logged-in users.

add_filter('load_default_embeds', 'load_embeds_for_logged_in_users');
function load_embeds_for_logged_in_users($maybe_load_embeds) {
    return is_user_logged_in();
}

Load default embeds on specific pages

Load default embed handlers only on specific pages based on their page IDs.

add_filter('load_default_embeds', 'load_embeds_on_specific_pages');
function load_embeds_on_specific_pages($maybe_load_embeds) {
    $allowed_pages = array(10, 25, 42); // Replace with your desired page IDs
    return in_array(get_the_ID(), $allowed_pages);
}

Load default embeds for specific post types

Enable loading default embed handlers only for specific post types.

add_filter('load_default_embeds', 'load_embeds_for_specific_post_types');
function load_embeds_for_specific_post_types($maybe_load_embeds) {
    $allowed_post_types = array('post', 'custom_post_type');
    return in_array(get_post_type(), $allowed_post_types);
}

Load default embeds based on user roles

Allow loading default embed handlers only for users with specific roles.

add_filter('load_default_embeds', 'load_embeds_based_on_user_roles');
function load_embeds_based_on_user_roles($maybe_load_embeds) {
    $allowed_roles = array('administrator', 'editor');
    $user = wp_get_current_user();
    return array_intersect($allowed_roles, $user->roles);
}

Load default embeds for specific categories

Enable loading default embed handlers only for posts in specific categories.

add_filter('load_default_embeds', 'load_embeds_for_specific_categories');
function load_embeds_for_specific_categories($maybe_load_embeds) {
    $allowed_categories = array('technology', 'news');
    $post_categories = wp_get_post_categories(get_the_ID(), array('fields' => 'names'));
    return array_intersect($allowed_categories, $post_categories);
}

Load default embeds on custom post templates

Load default embed handlers only on custom post templates.

add_filter('load_default_embeds', 'load_embeds_on_custom_post_templates');
function load_embeds_on_custom_post_templates($maybe_load_embeds) {
    $allowed_templates = array('template-custom.php', 'template-special.php');
    $current_template = get_page_template_slug();
    return in_array($current_template, $allowed_templates);
}

Load default embeds only on the homepage

Load default embed handlers only on the homepage.

add_filter('load_default_embeds', 'load_embeds_on_homepage');
function load_embeds_on_homepage($maybe_load_embeds) {
    return is_front_page();
}

Load default embeds based on user meta

Enable loading default embed handlers only for users with a specific meta value.

add_filter('load_default_embeds', 'load_embeds_based_on_user_meta');
function load_embeds_based_on_user_meta($maybe_load_embeds) {
    $user_id = get_current_user_id();
    $embeds_allowed = get_user_meta($user_id, 'allow_embeds', true);
    return $embeds_allowed == 'yes';
}

Load default embeds only for mobile devices

Load default embed handlers only when the website is viewed on a mobile device.

add_filter('load_default_embeds', 'load_embeds_on_mobile_devices');
function load_embeds_on_mobile_devices($maybe_load_embeds) {
    $is_mobile = wp_is_mobile();
    return $is_mobile;
}