Using WordPress ’embed_html’ PHP filter

The embed_html WordPress PHP filter allows you to modify the default embed HTML output for a given post.

Usage

add_filter('embed_html', 'your_function_name', 10, 4);
function your_function_name($output, $post, $width, $height) {
    // your custom code here
    return $output;
}

Parameters

  • $output (string) – The default iframe tag to display embedded content.
  • $post (WP_Post) – Current post object.
  • $width (int) – Width of the response.
  • $height (int) – Height of the response.

More information

See WordPress Developer Resources: embed_html

Examples

Change embed width and height

Modify the default width and height of the embedded content.

add_filter('embed_html', 'adjust_embed_dimensions', 10, 4);
function adjust_embed_dimensions($output, $post, $width, $height) {
    $new_width = 800;
    $new_height = 450;
    $output = preg_replace('/width="(\d+)"/i', 'width="' . $new_width . '"', $output);
    $output = preg_replace('/height="(\d+)"/i', 'height="' . $new_height . '"', $output);
    return $output;
}

Add a custom CSS class to iframe

Add a custom CSS class to the embedded iframe.

add_filter('embed_html', 'add_custom_iframe_class', 10, 4);
function add_custom_iframe_class($output, $post, $width, $height) {
    $output = str_replace('<iframe', '<iframe class="custom-iframe-class"', $output);
    return $output;
}

Remove frameborder attribute

Remove the frameborder attribute from the iframe.

add_filter('embed_html', 'remove_frameborder_attribute', 10, 4);
function remove_frameborder_attribute($output, $post, $width, $height) {
    $output = str_replace('frameborder="0"', '', $output);
    return $output;
}

Add a custom wrapper around the iframe

Wrap the iframe with a custom div element.

add_filter('embed_html', 'wrap_iframe_with_div', 10, 4);
function wrap_iframe_with_div($output, $post, $width, $height) {
    $output = '<div class="custom-iframe-wrapper">' . $output . '</div>';
    return $output;
}

Disable the embedded content for a specific post type

Disable the embedded content for a specific custom post type, such as ‘product’.

add_filter('embed_html', 'disable_embed_for_product', 10, 4);
function disable_embed_for_product($output, $post, $width, $height) {
    if ($post->post_type == 'product') {
        return '';
    }
    return $output;
}