Add meta-information to theme itself

It is not possible to extend the default headers used by WP_Theme:

private static $file_headers = array(
    'Name'        => 'Theme Name',
    'ThemeURI'    => 'Theme URI',
    'Description' => 'Description',
    'Author'      => 'Author',
    'AuthorURI'   => 'Author URI',
    'Version'     => 'Version',
    'Template'    => 'Template',
    'Status'      => 'Status',
    'Tags'        => 'Tags',
    'TextDomain'  => 'Text Domain',
    'DomainPath'  => 'Domain Path',
);

Here’s another post where someone was trying to do the same thing.

I kept digging, and found this post over on Stack Overflow which provided a workaround for being able to hard code and read custom file headers.

I realize that you want to be able to read and write these headers dynamically and display messages based on the info, and this answer doesn’t cover all of that, but it’s a good start.

Assuming this header in the theme:

/*
Theme Name: Dave's Good Stuff 80's Theme
Theme URI: https://www.youtube.com/playlist?list=PL3k1BzkuZjd7F0EE6mp5LPQWxigJ-8_td
Author: Dave Romsey
Author URI: http://daveromsey.com
Description: A cool theme.
Version: 0.1.0
Tags: 
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Text Domain: goto10
Key: 867-5309
*/

This code will be able to pull out custom file header keys:

/**
 * Retrieve metadata from a file.
 *
 * Searches for metadata in the first 8kiB of a file, such as a plugin or theme.
 * Each piece of metadata must be on its own line. Fields can not span multiple
 * lines, the value will get cut at the end of the first line.
 *
 * If the file data is not within that first 8kiB, then the author should correct
 * their plugin file and move the data headers to the top.
 *
 * @param string $file Path to the file
 * @param array $default_headers List of headers, in the format array('HeaderKey' => 'Header Name')
 */

function wpse238644_get_file_data( $file, $default_headers) {
    $fp = fopen( $file, 'r' );
    $file_data = fread( $fp, 8192 );
    fclose( $fp );
    $file_data = str_replace( "\r", "\n", $file_data );
    $all_headers = $default_headers;

    foreach ( $all_headers as $field => $regex ) {
            if (preg_match( '/^[ \t\/*#@]*' . preg_quote( $regex, "https://wordpress.stackexchange.com/" ) . ':(.*)$/mi', $file_data, $match ) 
                    && $match[1])
                    $all_headers[ $field ] = trim(preg_replace("/\s*(?:\*\/|\?>).*/", '', $match[1]));
            else
                    $all_headers[ $field ] = '';
    }

    return $all_headers;
}


function wpse238644_get_theme_data() {
    // Customize these to fit your needs
    $file_headers = array(
        'Name'        => 'Theme Name',
        'ThemeURI'    => 'Theme URI',
        'Description' => 'Description',
        'Author'      => 'Author',
        'AuthorURI'   => 'Author URI',
        'Version'     => 'Version',
        'Template'    => 'Template',
        'Status'      => 'Status',
        'Tags'        => 'Tags',
        'TextDomain'  => 'Text Domain',
        'DomainPath'  => 'Domain Path',
        'Key'         => 'Key',
    );

    $theme_headers = wpse238644_get_file_data( get_template_directory() . '/style.css', $file_headers );

    $theme_key = isset( $theme_headers['Key'] ) ? $theme_headers['Key'] : false;

    // Do something with the key
    //var_dump( $theme_key );
}
add_action( 'init', 'wpse238644_get_theme_data' );

Leave a Comment