The WPFolks app is out on iPhone and Android. Free, no ads. Download on the App Store Get it on Google Play
Skip to main content
Login Join
Snippet · PHP

Color-Code WordPress Pages by Assigned Template

Shared by Alkesh Miyani · October 28, 2026 · @post_class

18 views
Back to Snippets

Managing pages that use different custom templates can be difficult when a website has many pages.

This snippet applies different background colors to rows in WordPress Admin → Pages → All Pages, based on each page’s assigned template. It helps developers visually distinguish selected templates without opening individual pages.

How to use

  1. Add the code to your child theme’s functions.php file or a PHP snippets plugin.
  2. Replace the three example template paths in $template_classes with the exact filenames assigned to your pages.
  3. Open WordPress Admin → Pages → All Pages.
  4. Each row will receive the background color associated with its template.

Color legend

Important notes

/**
 * Add template-specific CSS classes to Pages admin rows.
 */
add_filter(
	'post_class',
	'alkesh_add_page_template_admin_row_class',
	10,
	3
);

function alkesh_add_page_template_admin_row_class(
	$classes,
	$class,
	$post_id
) {
	if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) {
		return $classes;
	}

	$screen = get_current_screen();

	if (
		! $screen ||
		'edit' !== $screen->base ||
		'page' !== $screen->post_type ||
		'page' !== get_post_type( $post_id )
	) {
		return $classes;
	}

	$template = get_page_template_slug( $post_id );

	// Identify pages using the default template.
	if ( empty( $template ) ) {
		$classes[] = 'alkesh-template-default';
		return $classes;
	}

	/*
	 * Map exact template filenames to CSS class names.
	 * Replace these examples with your actual template filenames.
	 */
	$template_classes = array(
		'templates/landing-page.php' => 'alkesh-template-landing',
		'templates/contact.php'      => 'alkesh-template-contact',
		'templates/full-width.php'   => 'alkesh-template-full-width',
	);

	if ( isset( $template_classes[ $template ] ) ) {
		$classes[] = $template_classes[ $template ];
	} else {
		$classes[] = 'alkesh-template-other';
	}

	return $classes;
}

/**
 * Apply template-specific colors in the Pages admin list.
 */
add_action(
	'admin_head-edit.php',
	'alkesh_style_page_template_admin_rows'
);

function alkesh_style_page_template_admin_rows() {
	$screen = get_current_screen();

	if (
		! $screen ||
		'edit' !== $screen->base ||
		'page' !== $screen->post_type
	) {
		return;
	}
	?>
	<style>
		/* Default page template: amber */
		.wp-list-table tr.alkesh-template-default > th,
		.wp-list-table tr.alkesh-template-default > td {
			background-color: #fff3cd;
		}

		/* Landing page template: blue */
		.wp-list-table tr.alkesh-template-landing > th,
		.wp-list-table tr.alkesh-template-landing > td {
			background-color: #dceeff;
		}

		/* Contact page template: green */
		.wp-list-table tr.alkesh-template-contact > th,
		.wp-list-table tr.alkesh-template-contact > td {
			background-color: #dff3e4;
		}

		/* Full-width template: lavender */
		.wp-list-table tr.alkesh-template-full-width > th,
		.wp-list-table tr.alkesh-template-full-width > td {
			background-color: #eee5ff;
		}

		/* Other custom templates: light gray */
		.wp-list-table tr.alkesh-template-other > th,
		.wp-list-table tr.alkesh-template-other > td {
			background-color: #f0f0f1;
		}
	</style>
	<?php
}

Tested on

WordPress 7.1.3 WordPress Admin

Reported by the author on October 28, 2026. WPFolks has not verified this.

Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments

Proudly supported by