Add /print/ to the end of any post URL to show a clean, printer-friendly version, using a WordPress rewrite endpoint.
add_action( 'init', function (): void {
add_rewrite_endpoint( 'print', EP_PERMALINK );
} );
add_action( 'template_redirect', function (): void {
global $wp_query;
if ( ! is_singular( 'post' ) || ! isset( $wp_query->query_vars['print'] ) ) {
return;
}
$post = get_queried_object();
?>
<!DOCTYPE html>
<html <?php language_attributes(); ?>>
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<meta name="robots" content="noindex">
<title><?php echo esc_html( get_the_title( $post ) ); ?></title>
<link rel="canonical" href="<?php echo esc_url( get_permalink( $post ) ); ?>">
<style>body{font:16px/1.6 Georgia,serif;max-width:42rem;margin:2rem auto;padding:0 1rem;color:#111}img{max-width:100%;height:auto}</style>
</head>
<body onload="window.print()">
<h1><?php echo esc_html( get_the_title( $post ) ); ?></h1>
<p><?php echo esc_html( get_the_date( '', $post ) ); ?> · <?php echo esc_url( get_permalink( $post ) ); ?></p>
<?php echo apply_filters( 'the_content', $post->post_content ); // phpcs:ignore WordPress.Security.EscapeOutput ?>
</body>
</html>
<?php
exit;
} );
Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.
Rewrite endpoints add an extra part to existing URLs, like /my-post/print/, without creating new pages. This example uses one for a printer-friendly version of every post.
add_rewrite_endpoint() teaches WordPress to recognise /print/ after any post permalink and sets a print query variable when it does.
On template_redirect, the snippet checks for that variable on single posts and prints a minimal page with the title, date, URL and content, then opens the print dialog. It’s marked noindex with a canonical link back to the post so search engines don’t see duplicate content.
EP_PAGES or EP_ALL instead of EP_PERMALINK to include pages.?p=123&print.Save your permalinks, then visit any post with print/ added to the URL. You should see a plain version and the print dialog.