REST API & Developer PHP Advanced

Add a Custom URL Endpoint to Posts

Add /print/ to the end of any post URL to show a clean, printer-friendly version, using a WordPress rewrite endpoint.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 31 lines
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;
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

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.

How it works

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.

Things to watch

  • After adding the snippet, go to Settings → Permalinks and click Save once. That rebuilds the rewrite rules. Without it, /print/ gives a 404.
  • Use EP_PAGES or EP_ALL instead of EP_PERMALINK to include pages.
  • Endpoints also work with plain query strings, like ?p=123&print.

How to test it

Save your permalinks, then visit any post with print/ added to the URL. You should see a plain version and the print dialog.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only