Getting headers property from WP_Theme object

Using wp_get_theme returns an object holding the properties of the current theme. The headers property contains any array of information I wish to access.

I can access the other properties using normal notation

wp_get_theme()->theme_root

When I try

wp_get_theme()->headers

I get NULL

How can I access the array within the headers property? Below is a var_dump of wp_get_theme()

object(WP_Theme)#381 (11) {
  ["theme_root":"WP_Theme":private]=>
  string(14) "/path/to/theme"
  ["headers":"WP_Theme":private]=>
  array(11) {
    ["Name"]=>
    string(9) "Site Name"
    ["ThemeURI"]=>
    string(0) ""
    ["Description"]=>
    string(11) "Description"
    ...
  }
  ["headers_sanitized":"WP_Theme":private]=>
  NULL`
  ...

1 Answer
1

You cannot access the $headers property as it is a private property.

Members declared as private may only be accessed by the class that defines the member

For that reason, you get NULL when you try to access the property with wp_get_theme()->headers. You need to make use of the magic __get() method of the class to get the info you are after.

Example: (From the codex page, wp_get_theme())

<?php
$my_theme = wp_get_theme();
echo $my_theme->get( 'TextDomain' );
echo $my_theme->get( 'ThemeURI' );
?>

Leave a Comment