WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Class Reference/WP Theme

Role of WP_Theme

WP_Theme is a class defined in wp-includes/class-wp-theme.php that helps developers interact with a theme.

Methods and Properties

This is the formal documentation of WP_Theme. You shouldn't alter the properties directly, but instead use the methods to interact with them.

Properties

Name 
WordPress Theme object.
ThemeURI 
The URI of the theme's webpage.
Description 
The description of the theme
Author 
The theme's author
AuthorURI 
The website of the theme author
Version 
The version of the theme
Template 
(Optional — used in a child theme) The folder name of the parent theme
Status 
If the theme is published
Tags 
Tags used to describe the theme
TextDomain 
The text domain used in the theme for translation purposes
DomainPath 
Path to the theme translation files

Methods

errors() 
Returns WP_Error object with error information. If there isn't any error information then it returns false
exists() 
Boolean for if the theme exists
parent() 
Returns parent WP_Theme object or false if there is no parent theme
cache_delete() 
Clears the cache for the theme

Header Methods

get( $header ) 
Used to get a sanitized but unformatted theme header. Use the get_template() method to find the template, not the 'Template' header. Returns string on success and false on failure
display ( $header, $markup, $translate ) 
Gets the theme header then formats and translates it for display.

Path and URL Methods

get_stylesheet() 
Returns a string with the directory name of the theme's "stylesheet" files, inside the theme root. If this is a child theme it is the directory from the child theme.
get_template() 
Returns a string with the directory name of the theme's "template" file. If this is a child theme it is the directory from the parent theme.
get_stylesheet_directory() 
Returns a string with the absolute path to a theme's stylesheet files. If the theme is a child theme, the path will be to the child theme files.
get_template_directory() 
Returns a string with the absolute path to a theme's template files. If the theme is a child theme, the path will be to the parent theme files.
get_stylesheet_directory_uri() 
Returns a string with the URL to the theme's stylesheet files. If the theme is a child theme, the URL will be to the child theme files.
get_template_directory_uri() 
Returns a string with the URL to the theme's template files. If the theme is a child theme, the URL will be to the parent theme files.
get_theme_root() 
Returns a string with the path to the directory of the theme root. This is typically the absolute path to the wp-content/themes folder.
get_theme_root_uri() 
Returns a string with the URL to the directory of the theme root. This is typically the absolute URL to the wp-content/themes folder.

Files and Images

get_screenshot( $uri ) 
Returns the URL to the screenshot image for the theme. The $uri parameter determines the type of url to return. Passing 'relative' returns a relative URL otherwise it defaults to an absolute URL.
get_files( $type, $depth, $search_parent ) 
Returns an array of files in the theme's directory.
get_page_templates() 
Returns an array of page templates, keyed by the filename, with the value of the translated header name.

Permission Methods

is_allowed() 
Boolean value for if a theme is allowed for the network. Always returns true if called on a single site.
get_allowed( $blog_id ) 
Returns an array of theme names that are allowed on the site or network. The $blog_id defaults to current blog. This method calls both get_allowed_on_network() and get_allowed_on_site( $blog_id ).
get_allowed_on_network () 
Returns an array of theme names that are allowed on the network.
get_allowed_on_site( $blog_id ) 
Returns an array of theme names that are allowed on the site.

Sorting

sort_by_name( $themes ) 
Sorts themes by name

Examples

Get Custom Page Templates

The following code snippets return all the custom Page Templates available to the currently activated theme as an array. Each array item has a key of the Template Name and value of the filename (or folder-name and filename for custom page templates stored in a theme's subdirectory).

 <?php 
    $templates = wp_get_theme()->get_page_templates();
    foreach ( $templates as $template_name => $template_filename ) {
        echo "$template_name ($template_filename)<br />";
    }
 ?>
<?php print_r( wp_get_theme()->get_page_templates() ); ?>

// Outputs:
Array
(
    [My Custom Page Template] => my-custom-page.php
    [A Second Custom Page] => my-page-templates/my-second-page.php
)

Change Log

Since : 3.4.0

Source File

WP_Theme is located in wp-includes/class-wp-theme.php.

See also index of Class Reference and index of Function Reference.