WP-Mix

A fresh mix of code snippets and tutorials

Break Out of Frames for WordPress

Quick JavaScript snippet to prevent scrapers and turds from framing, defaming, and/or stealing your precious content..

When included in your WordPress theme’s functions.php file, this PHP snippet helps to break out of frames, so your pages are served only from the original domain, not some douchebag’s Google adsense site.

// Break Out of Frames for WordPress
function break_out_of_frames() {
	if (!is_preview() && !is_customize_preview()) {
		echo "\n<script type=\"text/javascript\">";
		echo "\n<!--";
		echo "\nif (parent.frames.length > 0) { parent.location.href = location.href; }";
		echo "\n-->";
		echo "\n</script>\n\n";
	}
}
add_action('wp_head', 'break_out_of_frames');

Note: The if condition checks is_preview() and is_customize_preview() to exclude the script from preview/customize screens in the WordPress Admin Area. That way the front-end pages can be loaded by WordPress inside of the Customizer et al. Thanks to Jim S. for the tip!

Learn more

Digging Into WordPressWordPress Themes In DepthWizard’s SQL Recipes for WordPress