Remove Header and Footer in iframe

You could detect the $_SERVER['HTTP_REFERER'] variable to detect whether the page is requested from an external server. If so, then you should load an alternative header and footer using get_header() and get_footer(). You’ll need those calls to make sure all scripts and stylesheets are loaded, but you can remove the other content that make up the actual header and footer elements.

For example you could duplicate your current header.php to header-iframe.php and call the header with:

get_header('iframe');

The same thing goes for the footer. Create a file called footer-iframe.php and call it using:

get_footer('iframe');

Just make sure your header-iframe.php contains everything up to the opening tag, like this:

<html <?php language_attributes(); ?>>
<head>

    <meta charset="<?php bloginfo( 'charset' ); ?>" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="apple-mobile-web-app-capable" content="yes" />

    <title><?php wp_title( '|', true, 'right' ); ?></title>
    <?php wp_head(); ?>

</head>

<body <?php body_class('iframe'); ?>>

Your footer-iframe.php should be stripped of all excess content, like this:

<?php wp_footer(); ?>

</body>
</html>

The above snippets are just an example, but you get the idea. Calling the get_header('iframe'); and get_footer('iframe); functions should be conditional based on the $_SERVER['HTTP_REFERER'] variable. This could be done using preg_match or any other native PHP comparison function.

Leave a Comment