Chandra Patel

Just Another Developer

Post Embed Templates

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.

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.

Following functions would be useful.

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 in header-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

This site uses Akismet to reduce spam. Learn how your comment data is processed.