5 replaced http://wordpress.stackexchange.com/ with https://wordpress.stackexchange.com/
source | link

We can therefore add the page templates usage info, directly into the template dropdown, with the theme_page_templates filter, without using javascript or some clever object cache tricks explained here by @MikeSchinkelhere by @MikeSchinkel or here by @gmazzaphere by @gmazzap.

We can therefore add the page templates usage info, directly into the template dropdown, with the theme_page_templates filter, without using javascript or some clever object cache tricks explained here by @MikeSchinkel or here by @gmazzap.

We can therefore add the page templates usage info, directly into the template dropdown, with the theme_page_templates filter, without using javascript or some clever object cache tricks explained here by @MikeSchinkel or here by @gmazzap.

4 added 10 characters in body
source | link

TemplatePage Template Usage Info in WordPress 4.4+

We can therefore add the page templates usage info, directly into the template dropdown, with the theme_page_templates filter, without using javascript or some clever object cache tricks explained here by @MikeSchinkel or here by @gmazzap.

Here's a demo (PHP 5.4+):

Template Usage Info in WordPress 4.4+

We can therefore add the templates usage info, directly into the template dropdown, with the theme_page_templates filter, without using javascript or some clever object cache tricks explained here by @MikeSchinkel or here by @gmazzap:

Page Template Usage Info in WordPress 4.4+

We can therefore add the page templates usage info, directly into the template dropdown, with the theme_page_templates filter, without using javascript or some clever object cache tricks explained here by @MikeSchinkel or here by @gmazzap.

Here's a demo (PHP 5.4+):

3 Updated the answer to reflect the changes in WordPress 4.4
source | link

I tried toUpdate:

Template Usage Info in WordPress 4.4+

In WordPress 4.4 the array_intersect_assoc() was removed from the WP_Theme::get_page_templates() method.

See ticket #13265 and changeset #34995.

We can therefore add the templates usage info, directly into the template dropdown, with the theme_page_templates filter, without using javascript or some clever object cache tricks explained here by @MikeSchinkel or here by @gmazzap:

add_actionadd_filter( 'load-post.php''theme_page_templates', function( $page_templates, $obj, $post )
{
    add_filter(// 'theme_page_templates',Restrict functionto the post.php loading
    if( $page_templates,! $obj,did_action( $post'load-post.php' ) )
    {
    return $page_templates;

    foreach( (array) $page_templates as $key => $template )
        {
            $posts = get_posts( 
                [
                    'post_type'      => 'any',
                    'post_status'    => 'any', 
                    'posts_per_page' => 110,
                    'fields'         => 'ids',
                    'meta_query'     => [
                        [
                            'key'       => '_wp_page_template',
                            'value'     => $key,
                            'compare'   => '=',
                        ]
                ]
    ]
        ]
        ]);

        $count = count( $posts );

            // Let's addAdd the "In use"/"Not in use" partscount to the theme'stemplate name:
           in //the (thisdropdown. couldUse also10+ befor statistics>= instead)10
            $page_templates[$key] = sprintf( 
                '%s (%s)', 
                $template, 
                0 == count($count $posts>= )10 ? __( 'Not in use!' )'10+' : __( 'In use' ) $count
            );          
        }
        return $page_templates;
    }, 10, 3 );
} );

But because of how the get_page_templates() method uses array_intersect_assoc() on the cached values, after the theme_page_template filter, this does not workExample: as excpected ;-)

One way around this would beHere we can see how it could look like, with the usage count info added to update the template dropdown via Javascript or just run this as a PHP function, within the backend.names :

Maybe I will check this again later or someone elsetemplate usage info

Hope you can take it from here, on howadjust this to update the template dropdown with statistics ;-)your needs!

I tried to add the templates usage info, directly into the template dropdown, with the theme_page_templates filter:

add_action( 'load-post.php', function()
{
    add_filter( 'theme_page_templates', function( $page_templates, $obj, $post )
    {
        foreach( $page_templates as $key => $template )
        {
            $posts = get_posts( 
                [
                    'post_type'      => 'any',
                    'post_status'    => 'any', 
                    'posts_per_page' => 1,
                    'fields'         => 'ids',
                    'meta_query'     => [
                        [
                            'key'       => '_wp_page_template',
                            'value'     => $key,
                            'compare'   => '=',
                        ]
                    ]
                ]
            );

            // Let's add the "In use"/"Not in use" parts to the theme's name:
            // (this could also be statistics instead)
            $page_templates[$key] = sprintf( 
                '%s (%s)', 
                $template, 
                0 == count( $posts ) ? __( 'Not in use!' ) : __( 'In use' ) 
            );          
        }
        return $page_templates;
    }, 10, 3 );
} );

But because of how the get_page_templates() method uses array_intersect_assoc() on the cached values, after the theme_page_template filter, this does not work as excpected ;-)

One way around this would be to update the template dropdown via Javascript or just run this as a PHP function, within the backend.

Maybe I will check this again later or someone else can take it from here, on how to update the template dropdown with statistics ;-)

Update:

Template Usage Info in WordPress 4.4+

In WordPress 4.4 the array_intersect_assoc() was removed from the WP_Theme::get_page_templates() method.

See ticket #13265 and changeset #34995.

We can therefore add the templates usage info, directly into the template dropdown, with the theme_page_templates filter, without using javascript or some clever object cache tricks explained here by @MikeSchinkel or here by @gmazzap:

add_filter( 'theme_page_templates', function( $page_templates, $obj, $post )
{
    // Restrict to the post.php loading
    if( ! did_action( 'load-post.php' ) )
        return $page_templates;

    foreach( (array) $page_templates as $key => $template )
    {
        $posts = get_posts( 
            [
                'post_type'      => 'any',
                'post_status'    => 'any', 
                'posts_per_page' => 10,
                'fields'         => 'ids',
                'meta_query'     => [
                    [
                        'key'       => '_wp_page_template',
                        'value'     => $key,
                        'compare'   => '=',
                    ]
                ]
            ]
        );

        $count = count( $posts );

        // Add the count to the template name in the dropdown. Use 10+ for >= 10
        $page_templates[$key] = sprintf( 
            '%s (%s)', 
            $template, 
             $count >= 10 ? '10+' : $count
        );          
    }
    return $page_templates;
}, 10, 3 );

Example:

Here we can see how it could look like, with the usage count info added to the template names :

template usage info

Hope you can adjust this to your needs!

2 added 60 characters in body
source | link
1
source | link