How to Add a Custom Column with the Currently Active Page Template in WordPress

Home » Snippets » How to Add a Custom Column with the Currently Active Page Template in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Do you ever find yourself needing to identify the active page template in WordPress? Being able to easily distinguish which template is currently being used can be helpful for various customization purposes. In this article, we will guide you through the process of creating a custom column in the WordPress admin dashboard that displays the currently active page template, making it easier for you to manage and customize your WordPress site.

					function wpturbo_add_template_column($columns) {
    $columns['template'] = __( 'Template', 'wpturbo' );
    return $columns;
}
add_filter( 'manage_pages_columns', 'wpturbo_add_template_column' );

function wpturbo_display_template_column($column_name, $post_id) {
    if ( $column_name === 'template' ) {
        $template = get_page_template_slug( $post_id );
        echo esc_html( $template );
    }
}
add_action( 'manage_pages_custom_column', 'wpturbo_display_template_column', 10, 2 );
				

The code snippet provided allows you to add a custom column to the admin page listing in WordPress that displays the currently active page template for each page.

First, we define a function called wpturbo_add_template_column($columns) that takes the existing array of columns as a parameter. Inside this function, we add a new element to the columns array with the key 'template' and the value of __( 'Template', 'wpturbo' ). This sets the label for our custom column to "Template" and makes it translatable using the __() function.

Next, we hook our function into the manage_pages_columns filter, which allows us to modify the columns displayed on the pages list in the admin area. By returning the modified columns array from our function, we add our custom column to the list.

The second part of the code snippet defines another function called wpturbo_display_template_column($column_name, $post_id). This function is responsible for displaying the content of our custom column for each page.

In the function, we first check if the current column being rendered is our custom column 'template' by comparing the $column_name parameter. If it matches, we proceed to retrieve and display the page template.

To get the active page template, we use the get_page_template_slug($post_id) function. This function accepts the $post_id parameter, which represents the ID of the current page being rendered. It returns the slug of the page template file associated with the page.

To output the page template slug, we use echo esc_html($template) inside an if statement that checks if the column name is the one we want. This ensures that the page template will only be displayed in the custom column, not for other columns.

Finally, we hook our wpturbo_display_template_column function into the manage_pages_custom_column action with a priority of 10 and a function argument count of 2 (representing the column name and post ID).

By following these steps, you will be able to add a custom column to the admin page listing in WordPress that shows the currently active page template for each page.

Register an account to save your snippets or go Pro to get more features.