WordPress custom page template

Published on : February 10, 2014

Author:

Category: Wordpress


Hello guys, recently i have needed to add a custom page template for a WordPress site by using plugins. So, when you active this plugins, for a specific page your custom template will be parse, unlike regular theme page template/layout.

For my case it was needed as because i wanted to remove the current activated theme’s header and footer. If you need this things to be done the following code sniped will be help you.


add_filter( 'page_template', 'your_custom_funtion_name' );

function your_custom_funtion_name( $page_template )
{
	if ( is_page( 'your-specific-page-slug' ) ) {
		$page_template = dirname( __FILE__ ) . '/your-custom-page-template.php';
	}
	return $page_template;
}
  • You have to add a filter “page_template” in WordPress to alter the default page template.
  • Replace your page slug with “your-specific-page-slug“, it is helpful when you create a dynamic page while your plugins is activate and you want a custom layout without the theme header footer.
  • Place this file “your-custom-page-template.php” in your own plugins folder and add the following code for test

<html>
	<head>
		<title>Custom template</title>
	</head>
	<body>
		<h1>Custom page template</h1>
	</body>
</html>

if you follow this steps properly then, when you visit page with slug “your-specific-page-slug” you can see your custom page layout which is in “your-custom-page-template.php” file.