WordPress allows to embed post including custom post type post. Using Embed
block, user can embed post of any WordPress site. I have embedded one of my posts below.
Embed
block append /embed/
in post URL and make request to get post embed content. So, if user visit https://chandra.dev/wp-custom-settings/embed/ URL in browser, user will see post embed content.
While exploring WordPress Core codebase, I found out post embed templates in /wp-includes/theme-compat/ folder. When a post is embedded in an iframe, following files is used to create the output if the active theme does not include post embed templates file.
- wp-includes/theme-compat/embed.php – Post embed base template.
- wp-includes/theme-compat/header-embed.php – Post embed header template.
- wp-includes/theme-compat/embed-content.php – Post embed content template part.
- wp-includes/theme-compat/embed-404.php – If post not found then this template part use to display
404
error message. - wp-includes/theme-compat/footer-embed.php – Post embed footer template.
Above examples shows default layout and style of embedded post by WordPress. Developer can override above templates in theme by keeping same templates file name and have different layout and style.
Add embed.php
, header-embed.php
and footer-embed.php
files at root level of theme folder. Add embed-content.php
and embed-404.php
files in template-parts
folder.
Also, refer embed template hierarchy.
These templates provide following hooks that developer can use to extend or modify post embed content.
- embed_head – Action to prints scripts or data in the embed template head tag.
- embed_thumbnail_id – Filters the thumbnail image ID for use in the embed template.
- embed_thumbnail_image_size – Filters the thumbnail image size for use in the embed template.
- embed_thumbnail_image_shape – Filters the thumbnail shape for use in the embed template.
- embed_content – Prints additional content after the embed excerpt.
- embed_content_meta – Prints additional meta content in the embed template.
- embed_footer – Prints scripts or data before the closing body tag in the embed template.
Following functions would be useful.
- is_embed() – Is the query for an embedded post?
- the_excerpt_embed() – Displays the post excerpt for the embed template.
- the_embed_site_title() – Prints the necessary markup for the site title in an embed template.
- get_post_embed_url() – Retrieves the URL to embed a specific post in an iframe.
- get_post_embed_html() – Retrieves the embed code for a specific post.
Important Notes
- Post embed feature available since WordPress version 4.4.
- Above mentioned embed templates available since WordPress version 4.5.
- WordPress send
X-WP-embed: true
header in response. Code added inheader-embed.php
file. So, if you’re overriding templates then make sure you keep that code. - Also, make sure above-mentioned hooks called in overridden templates.
Leave a Reply