WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Jan 07 2013

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' );
}
view raw gs_constants.php hosted with ❤ by GitHub

Written by Travis Smith · Categorized: WordPress

StudioPress Premium WordPress Themes     WP Engine Managed WordPress Hosting

What can I do for you!?

Custom Development

We develop plugins by determining both business/functional and technical requirements, following WordPress development best practices, and using agile methodology to ensure you get the best solution.

Consulting

Have questions? Need a reliable developer to consult? Please contact us today!

Customized Theme

We can customize your theme or child theme, or create a child theme for you based on your needs while enhancing the performance of every individual attribute.

Customized Plugin

We can customize your plugins, extend plugins (e.g., Gravity Forms, Jetpack, Soliloquy) based on your needs ensuring security, performance, and positive business impact.

Contact Us

About Travis Smith

As a WordPress enthusiast, developer, and speaker, Travis writes about what he learns in WordPress trying to help other WordPress travelers, beginners and enthusiasts with tutorials, explanations, & demonstrations.

Comments

  1. Bruce Munson says

    January 8, 2013 at 9:27 am

    This is a stroke of genius! One less thing to have to think about!

    Thanks for this!

    Reply
  2. Michael Rapino says

    August 8, 2014 at 6:56 pm

    Travis,

    For some reason, this code isn’t working for me. I think it has something to do with the genesis_init hook. When I pull the code out and don’t use the hook, it works. As soon as I wrap a function around it, and call it using the genesis_init action hook, it stops working.

    Any ideas?

    Cheers!

    Reply
  3. Michael Rapino says

    August 8, 2014 at 7:14 pm

    Travis,

    I ended up having to place your code at the very top of the functions.php file, before:

    //* Start the engine
    include_once( get_template_directory() . ‘/lib/init.php’ );

    For some reason it didn’t work when it was below this line.

    Any thoughts?

    Cheers!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

Copyright © 2025 � WP Smith on Genesis on Genesis Framework � WordPress � Log in