Site icon WP Smith

Create Genesis Constants Based on Theme

In my child themes, this little function that I wrote based on the new wp_get_theme() function (Codex, WP Ticket). Previously, I had to create style.css with all the data and then had to do the same thing in functions.php (or my child init.php). So to save 30 seconds or so, I have no fully automated this entirely, so everything in style.css matters. Everything!

Let's step through the style.css header.

/*
Theme Name: Genesis Child
Theme URI: http://wpsmith.net/
Description: Default Genesis Child Theme.
Author: Travis Smith
Author URI: http://wpsmith.net/
Version: 1.0.0
Text Domain: genesis-child
Tags: black, white, red, one-column, two-columns, three-columns, left-sidebar, right-sidebar, fixed-width, custom-background, custom-header, custom-menu, editor-style, full-width-template, sticky-post, theme-options, threaded-comments, translation-ready
Template: genesis
Template Version: 1.9.0
License: GNU General Public License v3.0 (or later)
License URI: http://opensource.org/licenses/gpl-license.php
*/
view raw style.css hosted with ❤ by GitHub

Obviously, Template and Template version should be set for every child theme.

So Theme Name will determine CHILD_THEME_NAME, which will automatically appear in the Genesis Footer connected with the Theme URI, which is set as CHILD_THEME_URL. WordPress pulls in the Description on the Appearance > Themes page. I have an admin script that I make better use of Author and Author URI besides the basic WordPress listing them in Appearance > Themes (as it also uses Tags). It places it in the footer stating, "Thank you for creating with WordPress & CHILD_THEME_NAME designed by CHILD_DEVELOPER" linked with CHILD_DEVELOPER_URL.

What's even more awesome is that Genesis 1.9 makes use of CHILD_THEME_VERSION if it is set in enqueuing the style. So, it's important, but I also use it elsewhere. Then the even more awesome thing is Text Domain! Text Domain sets two constants: CHILD_DOMAIN (localization) and CHILD_THEME_SETTINGS (admin page). So if Text Domain is set as 'genesis-child' then the CHILD_DOMAIN will be 'genesis-child' and the CHILD_THEME_SETTINGS will be 'genesis-child-settings'.

Now, I don't have to think a single bit for my theme constants. I just use this function in all child themes I create.

<?php
add_action( 'genesis_init', 'gs_constants', 15 );
/**
* This function defines the Genesis Child theme constants
*
* Data Constants: CHILD_SETTINGS_FIELD, CHILD_DOMAIN, CHILD_THEME_VERSION
* CHILD_THEME_NAME, CHILD_THEME_URL, CHILD_DEVELOPER, CHILD_DEVELOPER_URL
* Directories: CHILD_LIB_DIR, CHILD_IMAGES_DIR, CHILD_ADMIN_DIR, CHILD_JS_DIR, CHILD_CSS_DIR
* URLs: CHILD_LIB, CHILD_IMAGES, CHILD_ADMIN, CHILD_JS, CHILD_CSS
*
* @since 1.1.0
*/
function gs_constants() {
$theme = wp_get_theme();
// Child theme (Change but do not remove)
/** @type constant Child Theme Options/Settings. */
define( 'CHILD_SETTINGS_FIELD', $theme->get('TextDomain') . '-settings' );
/** @type constant Text Domain. */
define( 'CHILD_DOMAIN', $theme->get('TextDomain') );
/** @type constant Child Theme Version. */
define( 'CHILD_THEME_VERSION', $theme->Version );
/** @type constant Child Theme Name, used in footer. */
define( 'CHILD_THEME_NAME', $theme->Name );
/** @type constant Child Theme URL, used in footer. */
define( 'CHILD_THEME_URL', $theme->get('ThemeURI') );
// Developer Information, see lib/admin/admin-functions.php
/** @type constant Child Theme Developer, used in footer. */
define( 'CHILD_DEVELOPER', $theme->Author );
/** @type constant Child Theme Developer URL, used in footer. */
define( 'CHILD_DEVELOPER_URL', $theme->{'Author URI'} );
// Define Directory Location Constants
/** @type constant Child Theme Library/Includes URL Location. */
define( 'CHILD_LIB_DIR', CHILD_DIR . '/lib' );
/** @type constant Child Theme Images URL Location. */
define( 'CHILD_IMAGES_DIR', CHILD_DIR . '/images' );
/** @type constant Child Theme Admin URL Location. */
define( 'CHILD_ADMIN_DIR', CHILD_LIB_DIR . '/admin' );
/** @type constant Child Theme JS URL Location. */
define( 'CHILD_JS_DIR', CHILD_DIR .'/js' );
/** @type constant Child Theme JS URL Location. */
define( 'CHILD_CSS_DIR', CHILD_DIR .'/css' );
// Define URL Location Constants
/** @type constant Child Theme Library/Includes URL Location. */
define( 'CHILD_LIB', CHILD_URL . '/lib' );
/** @type constant Child Theme Images URL Location. */
define( 'CHILD_IMAGES', CHILD_URL . '/images' );
/** @type constant Child Theme Admin URL Location. */
define( 'CHILD_ADMIN', CHILD_LIB . '/admin' );
/** @type constant Child Theme JS URL Location. */
define( 'CHILD_JS', CHILD_URL .'/js' );
/** @type constant Child Theme JS URL Location. */
define( 'CHILD_CSS', CHILD_URL .'/css' );
}