WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Jan 20 2012

Genesis 1.8 & Genesis Layouts for Specific Custom Post Type Only

So in Genesis 1.8, you will be able to designate layouts for various parts of the site, including various post types. While in Genesis 1.8, this is a bit more convoluted than necessary, it is an appropriate small step towards the right direction to expanding and improving site layouts. To do this you will need to follow these steps:

  1. Register your layouts for that post type
  2. Adjust builtin layouts for that post type
  3. Re-Create the Meta Box for the custom post type

Now, in future versions of Genesis re-creating the metabox won't be necessary (hopefully) and there will be other extensions and features as well.

Register your layouts for that post type

So we first need to register the layout. In this example, we are going to assume that our post type is registered correctly to support, genesis-layouts. If layouts do not appear, you can simply add this line to your functions.php: add_post_type_support( 'my-services', 'genesis-layouts' );.

[php]
add_action( 'init' , 'wps_create_initial_layouts' );
/**
* Registers Genesis custom post type default layouts.
*
*/
function wps_create_initial_layouts() {
genesis_register_layout( 'top-sidebar', array(
'label' => __( 'Top Sidebar', CHILD_DOMAIN ),
'img' => WPS_LAYOUTS . '/top-sidebar.PNG',
'type'    => 'wps_employee',
) );
genesis_register_layout( 'bottom-sidebar', array(
'label' => __( 'Bottom Sidebar', CHILD_DOMAIN ),
'img' => WPS_LAYOUTS . '/bottom-sidebar.PNG',
'type'    => 'wps_employee',
) );
}
[/php]

Adjust builtin layouts for that post type

If you want to include the standard types, you will need to add the following:
[php]
add_action( 'admin_init', 'genesis_fix_initial_layouts' );
/**
* Fixes registered initial layouts to be added to a specific post type.
* Genesis comes with 6 layouts registered by default. These are:
* - content-sidebar (default)
* - sidebar-content
* - content-sidebar-sidebar
* - sidebar-sidebar-content
* - sidebar-content-sidebar
* - full-width-content
*
* @uses global $_genesis_layouts
* @author Travis Smith
*/
function genesis_fix_initial_layouts () {
global $_genesis_layouts;
$post_type = isset ( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
if ( $post_type == 'wps_employee' ) {
// remove from the array as you see appropriate
$_builtin_layouts = array (
'content-sidebar',
'sidebar-content',
'content-sidebar-sidebar',
//'sidebar-sidebar-content',
'sidebar-content-sidebar',
'full-width-content'
);
foreach ( $_builtin_layouts as $layout) {
$_genesis_layouts[$layout]['type'] = 'wps_employee';
}
}

}
[/php]
The code above only changes the builtin layouts to the appropriate custom post type if on that page. If you have multiple custom post types you could do something like this:

[php]
add_action( 'admin_init', 'genesis_fix_initial_layouts' );
/**
* Fixes registered initial layouts to be added to a specific post type.
*
* @uses global $_genesis_layouts
* @author Travis Smith
*/
function genesis_fix_initial_layouts () {
global $_genesis_layouts;
$post_type = isset ( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
switch ( $post_type ) {
case ( 'wps_employee' ) :
// remove from the array as you see appropriate
$_builtin_layouts = array (
'content-sidebar',
'sidebar-content',
'content-sidebar-sidebar',
//'sidebar-sidebar-content',
'sidebar-content-sidebar',
'full-width-content'
);
break;
case ( 'my-services' ) :
// remove from the array as you see appropriate
$_builtin_layouts = array (
'content-sidebar',
'sidebar-content',
//'content-sidebar-sidebar',
//'sidebar-sidebar-content',
//'sidebar-content-sidebar',
//'full-width-content'
);
break;
default:
$_builtin_layouts = array ();
break;
}
foreach ( $_builtin_layouts as $layout) {
$_genesis_layouts[$layout]['type'] = $post_type;
}

}[/php]

Re-Create the Meta Box for the custom post type

Since there are plans to extend the use of type in genesis_layout_selector(), adding a filter, while a great idea and dramatically reduces this code, will most likely deprecate after only one version. So, please bear with me.

[php]
add_action( 'admin_menu', 'genesis_customize_inpost_layout_box' );
/**
* Remove the builtin meta box for specific post types & Register a new meta box to the
* post / page edit screen, so that the user can
* set layout options on a per-post or per-page basis with custom genesis_inpost_layout_box().
*
* @see genesis_inpost_layout_box() Generates the content in the boxes
* @author Travis Smith
* @return null Returns null if Genesis layouts are not supported
*/
function genesis_customize_inpost_layout_box() {

if ( ! current_theme_supports( 'genesis-inpost-layouts' ) )
return;

foreach ( (array) get_post_types( array( 'public' => true ) ) as $type ) {
if ( post_type_supports( $type, 'genesis-layouts' ) ) {
if ( $type == 'wps_employee' ) {
// Remove builtin metabox
remove_meta_box( 'genesis_inpost_layout_box', $type, 'normal' );
// Add custom metabox
add_meta_box( 'genesis_inpost_layout_box', __( 'Layout Settings', 'genesis' ), 'wps_inpost_layout_box', $type, 'normal', 'high' );
}
}
}
}

/**
* Callback for custom in-post layout meta box.
*
* Echoes out HTML.
*
* @author Travis Smith
*/
function wps_inpost_layout_box() {
$post_type = isset ( $_GET['post_type'] ) ? $_GET['post_type'] : 'post';
wp_nonce_field( plugin_basename( __FILE__ ), 'genesis_inpost_layout_nonce' );

$layout = genesis_get_custom_field( '_genesis_layout' );
?>
<div class="genesis-layout-selector">
<p><input type="radio" name="_genesis_layout" id="default-layout" value="" <?php checked( $layout, '' ); ?> /> <label class="default" for="default-layout"><?php printf( __( 'Default Layout set in <a href="%s">Theme Settings</a>', 'genesis' ), menu_page_url( 'genesis', 0 ) ); ?></label></p>

<p><?php genesis_layout_selector( array( 'name' => '_genesis_layout', 'selected' => $layout, 'type' => $post_type ) ); ?></p>
</div>

<br class="clear" />

<p><label for="genesis_custom_body_class"><b><?php _e( 'Custom Body Class', 'genesis' ); ?></b></label></p>
<p><input class="large-text" type="text" name="_genesis_custom_body_class" id="genesis_custom_body_class" value="<?php echo esc_attr( sanitize_html_class( genesis_get_custom_field( '_genesis_custom_body_class' ) ) ); ?>" /></p>

<p><label for="genesis_custom_post_class"><b><?php _e( 'Custom Post Class', 'genesis' ); ?></b></label></p>
<p><input class="large-text" type="text" name="_genesis_custom_post_class" id="genesis_custom_post_class" value="<?php echo esc_attr( sanitize_html_class( genesis_get_custom_field( '_genesis_custom_post_class' ) ) ); ?>" /></p>
<?php
}
[/php]

Now you have specific layouts only available for specific post types!! Enjoy!

Written by Travis Smith · Categorized: Genesis

Jan 10 2012

An Introduction to Genesis 1.8 with Examples

Genesis 1.8 Beta is out today with over 80 fixes, improvements, and enhancements. This is the first release that I was lucky enough to have some direct contributions (previously my contributions were made through the Genesis Development forum, which had to be recommended to the developers via another person). Let's review some of what I believe to be some extremely important updates and improvements. If you are not a ProPlus Member, become one today to get the Beta version.

In short, as Gary aptly describes: Genesis 1.8 is...

A major release which saw the addition of a native color style setting box for child themes, a set of classes that make adding new pages in child themes considerably easier, several bits of functionality to make customising themes easier, a new default look including responsive design, a documentation and code standards overhaul, a rewrite of the admin JavaScript, and a slight change to a less restrictive license.

Here's a review of some of the many changes to be seen:

User Interface Updates

Sometimes it's about the little things. In Genesis 1.8, there are the following:

    • Improved image dimensions dropdown to display and use correct multiplication character, not the letter x.

Genesis Image Sizes Dropdown

    • Improved metabox order on Theme Settings page.

Genesis Theme Settings Order

    • Added setting to enable / disable breadcrumbs on attachment pages.

Genesis Breadcrumbs

    • Added separate custom title and description on term archives

Custom Term Title Description

General Updates (Widgets, Readme, Misc)

Various other updates include improved images (favicon, screenshot), default footer wording credits, updated Readme, improved eNews, Header Right(/Left). The user profile dropdowns now include those who have posts. One of the good upcoming updates is that Genesis will deprecate Genesis Page and Category Menu from core. Genesis 1.8 only includes a warning that this is coming.

Developer Updates

So from a developer's perspective, Genesis 1.8 has made many and bountiful enhancements and improvements making development, even rapid development, extremely easy including many one line coding options and the admin class to make creating admin pages simpler. This is what I am the most excited about.

Genesis Admin Class

First and foremost is the new admin class making developing admin pages simple. All the Genesis admin pages were re-developed according to the class adding new filters and new hooks. If we were to develop a new child theme settings, page we simply just extend whichever class we'd like the Genesis_Admin_Boxes (which displays metaboxes and are used by the theme settings admin page and the SEO settings admin page) or Genesis_Admin_Form, which has no example in Genesis Core. Once could even use the Genesis_Admin_Basic, which the import/export pages are built upon. Furthermore, Admin class makes contextual help as easy as creating a help() method. For more information on the new Genesis Admin Class, see my Metabox Example here: How to Use the New Genesis Admin Class: Metabox Example

New Genesis Filters

There are also a few new filters beside the one previously mentioned. They are:

    • genesis_theme_settings_menu_ops:
    • genesis_settings_field & genesis_seo_settings_field: allows you to change the settings field & SEO settings field constants
    • genesis_edit_post_link: allows you to replace/disable edit link

[php]
// Remove edit link
add_filter ( 'genesis_edit_post_link' , '__return_false' );
[/php]

    • genesis_term_meta & genesis_term_meta_*: allows you to edit term meta data as a whole or by field, which would be a great way to add some default intro text

[php]
// Change term_meta & prevent & changes
add_filter ( 'genesis_term_meta' , 'wps_term_meta' );
function ( $term_meta ) {
return array(
'headline' => '',
'intro_text' => '',
'display_title' => 0, /** vestigial */
'display_description' => 0, /** vestigial */
'doctitle' => '',
'description' => '',
'keywords' => '',
'layout' => '',
'noindex' => 0,
'nofollow' => 0,
'noarchive' => 0,
);
}
[/php]

Or, via per term field:

[php]
// Change term_meta headline & prevent & changes for all terms
add_filter ( 'genesis_term_meta_headline' , 'wps_term_meta_headline' , 10 , 3 );
function wps_term_meta_headline ( $term_meta , $term , $taxonomy ) {
return $taxonomy . ': ' . $term ;
}
[/php]

  • genesis_search_form_label:

[php]
// Adds accessibility label
add_filter ( 'genesis_search_form_label' , 'wps_search_form_label' );
function wps_search_form_label ( $label ) {
return '<label for="s">' . __( 'Search this Site' ) . '</label>';
}
[/php]

  • genesis_custom_header_defaults: allows you to set a default header

[php]
// Set default custom background color & image
add_filter ( 'genesis_custom_header_defaults' , 'wps_search_form_label' );
function wps_search_form_label ( $label ) {
return array(
'width' => 960,
'height' => 80,
'textcolor' => '333333',
'no_header_text' => false,
'header_image' => '%s/images/header.png',
'header_callback' => 'genesis_custom_header_style',
'admin_header_callback' => 'genesis_custom_header_admin_style',
);
}
[/php]

  • genesis_prev_link_text & genesis_next_link_text: allows you to change Previous/Next texts

[php]
// Change Previous Page text
add_filter ( 'genesis_prev_link_text' , 'wps_prev_link_text' );
function wps_prev_link_text ( $text ) {
return g_ent( '« ' ) . __( 'Previous Page', CHILD_DOMAIN );
}

// Change Next Page text
add_filter ( 'genesis_next_link_text' , 'wps_next_link_text' );
function wps_next_link_text ( $text ) {
return g_ent( '« ' ) . __( 'Next Page', CHILD_DOMAIN );
}
[/php]

  • genesis_archive_display_options: allows for more options beyond Excerpt, Content Limit

Genesis Menus Theme Support

Then there is the ability to dynamically register/unregister nav menus in functions.php via add_theme_support(). For example:

[php]
// Default Menus: registers Primary and Secondary
add_theme_support ( 'genesis-menus' , array ( 'primary' => 'Primary Navigation Menu' , 'secondary' => 'Secondary Navigation Menu' ) );

// Changes Default to Primary & Footer
add_theme_support ( 'genesis-menus' , array ( 'primary' => 'Primary Navigation Menu' , 'footer' => 'Footer Navigation Menu' ) );

// Remove Custom Menu support
remove_theme_support ( 'genesis-menus' );
[/php]

Color Switcher

Next, there is the Genesis easy color switcher that adds a body class to build in multiple theme colors. To really see a good example of this, see AgentPress 2's CSS file at the bottom for those who are ProPlus Members.

[php]
/** Create additional color style options */
add_theme_support( 'genesis-style-selector', array( 'agentpress-gray' => 'Gray', 'agentpress-green' => 'Green', 'agentpress-red' => 'Red', 'agentpress-tan' => 'Tan' ) );
[/php]

Genesis Widget Area

Finally, props to Nick_theGeek for this really cool new addition for child theme developers is the new genesis_widget_area(). Instead of using !dynamic_sidebar() or is_active_sidebar(), a child theme developer can use genesis_widget_area() for home page development. For example, instead of...

[php]
if ( is_active_sidebar( 'home' ) || is_active_sidebar( 'home-left' ) || is_active_sidebar( 'home-right' ) ) {

dynamic_sidebar( 'home' );

if ( is_active_sidebar( 'home-left' ) ) {
echo '</pre>
<div id="homepage-left">';
dynamic_sidebar( 'home-left' );
echo '</div>
<pre>
<!-- end #homepage-left -->';
}

if ( is_active_sidebar( 'home-right' ) ) {
echo '</pre>
<div id="homepage-right">';
dynamic_sidebar( 'home-right' );
echo '</div>
<pre>
<!-- end #homepage-right -->';
}

}
else {
genesis_standard_loop();
}
[/php]

you could have...

[php]
if ( ! genesis_widget_area( 'home', array( 'before' => '' , 'after' => '' ) ) && ! genesis_widget_area( 'home-left' ) && ! genesis_widget_area( 'home-right' ) )
genesis_standard_loop();
[/php]

Designer Updates

From a designer's perspective (which is not my forte), responsive design was introduced & menu CSS was drastically reduced. See Brian’s post for a great explanation.

Summary

Genesis 1.8 Beta is out with a force with a massive collection of some excellent upgrades, improvements, and fixes! If you are not a ProPlus Member, become one today to get the Beta version now!

Written by Travis Smith · Categorized: Genesis

Jan 10 2012

How to Use the New Genesis Admin Class: Metabox Example

With Genesis 1.8 or see the Genesis 1.8 Changelog). Genesis Plugins and Child Theme Developers alike can use this class to create option pages and even extend existing option pages. If you are not a ProPlus Member, become one today to get the Beta version.

Before I move into the examples, I’d like to mention that every Genesis Admin page now has a few hooks:

  • genesis_admin_init:
  • Genesis_Admin_Form: $pagehook . '_settings_page_form'
  • Genesis_Admin_Boxes: genesis_admin_before_metaboxes & genesis_admin_after_metaboxes

So, let's create our own child theme settings page. To do this, you will need to create a new file called child-theme-settings.php.

Page Level Documentation

For a good walk through on theme documentation, please read Michael Fields's Theme Documentation post. For our purposes...
[php]

/**
* Child Theme Settings
* Requires Genesis 1.8 or later
*
* This file registers all of this child theme's
* specific Theme Settings, accessible from
* Genesis > Child Theme Settings.
*
* @package WPS_Starter_Genesis_Child
* @author Travis Smith <[email protected]>
* @copyright Copyright (c) 2012, Travis Smith
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 1.0
* @alter 1.1.2012
*
*/
[/php]

Extending the Class

Next, if you are familiar with classes (even widgets), this will seem simple; however, if you are unfamiliar, this is an important piece of code.
[php]
/**
* Registers a new admin page, providing content and corresponding menu item
* for the Child Theme Settings page.
*
* @package WPS_Starter_Genesis_Child
* @subpackage Admin
*
* @since 1.0.0
*/
class Child_Theme_Settings extends Genesis_Admin_Boxes {
[/php]

So what's happening here is that I am creating a new class that is based on the class Genesis_Admin_Boxes, and when you dig deeper, the Genesis_Admin_Boxes class is based on the Genesis_Admin class, which is THE base class that creates menus and settings pages. The Genesis_Admin_Boxes extends the Genesis_Admin classes by preparing the pages for metaboxes.

The Construct Method

In case you don't know a function inside a class is called a method. So now, we need to construct our new Child_Theme_Settings class.
[php]
/**
* Create an admin menu item and settings page.
*
* @since 1.0.0
*/
function __construct() {

// Specify a unique page ID.
$page_id = 'child';

// Set it as a child to genesis, and define the menu and page titles
$menu_ops = array(
'submenu' => array(
'parent_slug' => 'genesis',
'page_title' => 'Genesis - WPS Starter Settings',
'menu_title' => 'WPS Starter Settings',
'capability' => 'manage_options',
)
);

// Set up page options. These are optional, so only uncomment if you want to change the defaults
$page_ops = array(
'screen_icon' => array( 'custom' => WPS_ADMIN_IMAGES . '/staff_32x32.png' ),
// 'screen_icon' => 'options-general',
// 'save_button_text' => 'Save Settings',
// 'reset_button_text' => 'Reset Settings',
// 'save_notice_text' => 'Settings saved.',
// 'reset_notice_text' => 'Settings reset.',
);

// Give it a unique settings field.
// You'll access them from genesis_get_option( 'option_name', CHILD_SETTINGS_FIELD );
$settings_field = CHILD_SETTINGS_FIELD;

// Set the default values
$default_settings = array(
'phone' => '',
'address' => '',
);

// Create the Admin Page
$this->create( $page_id, $menu_ops, $page_ops, $settings_field, $default_settings );

// Initialize the Sanitization Filter
add_action( 'genesis_settings_sanitizer_init', array( $this, 'sanitization_filters' ) );

}
[/php]

Page ID & Menu Page

There is a lot going on here that is fairly well documented. $page_id sets the admin url slug for the page. For this page, the admin url will be: wp-admin/admin.php?page=child. $menu_ops controls the creation of the submenu item. The submenu array controls who is the parent (obviously, if you are creating a Genesis Child Theme, this will be set to 'genesis'). However, the Genesis Admin class does not limit you. If I wanted an admin page to appear under Appearance, I could change 'parent' to themes.php, or if I wanted to add an admin page to Media, I would change the parent to upload.php, etc. For more information about what parent page slugs are, see the WordPress Codex: add_submenu_page.

Next, the page title and the menu title are self-explanatory where one refers to what will be outputted on the page title area, while the other is what appears in the submenu area. Capability can be set to whatever you'd like. If you want to be consistent with Genesis, you want to set this to edit_theme_options.

Next, we have the page options, which are optional. Currently, the screen_icon does not accept custom icons, though a ticket exists for this and may make the final edition of Genesis 1.8. Otherwise, you are limited to the standard WordPress icons. There isn't much about the available screen icon choices, so here is a small list:

  • plugins
  • edit
  • upload
  • index
  • link-manager
  • edit-pages
  • edit-comments
  • themes
  • users
  • tools
  • options-general

Next, we have the settings field, which I have set to a constant CHILD_SETTINGS_FIELD. This really can be whatever you want it to be to refer to your settings (e.g., 'child-settings'). Then we have the default settings that you would like to be created. The settings field and the defaults will be used to create an options field so that essentially add_option ( 'child-settings' , $default_settings ); takes place later. The default settings will also be used to reset theme options. In my child theme, I have no values set, so on Reset, everything will be erased.

Finally, we have everything we need to create the admin page via, what I could coin as the creation call. And as an added bonus, we can validate/sanitize our settings via the genesis sanitization class. So we add the action, which is expecting a reference array action.

The Sanitization Method

While a lot could be said about the sanitization class, since it's not new, let's just review the various options before moving forward. They are:

  • one_zero: Same as true-false
  • no_html: Does not allow for any HTML
  • safe_html: Removes unsafe HTML via wp_kses_post()
  • requires_unfiltered_html: Keeps the option from being updated if the user lacks unfiltered_html capability

So now we will add the sanitization method:
[php]
/**
* Set up Sanitization Filters
*
* See /lib/classes/sanitization.php for all available filters.
*
* @since 1.0.0
*/
function sanitization_filters() {

genesis_add_option_filter( 'no_html', $this->settings_field,
array(
'phone',
'address',
) );
}
[/php]

The Metabox Method

Now that the page has been created and the option defaults have been set and sanitized on save, we can create our metaboxes.
[php]
/**
* Register metaboxes on Child Theme Settings page
*
* @since 1.0.0
*
* @see Child_Theme_Settings::contact_information() Callback for contact information
*/
function metaboxes() {

add_meta_box( 'contact-information', 'Contact Information', array( $this, 'contact_information' ), $this->pagehook, 'main', 'high' );

}
[/php]

No more necessary additions of in-between functions, etc., etc. Simply add the metabox. For more information on the metabox function, see the WordPress Codex: add_meta_box.

The Metabox Callback Method

Since we said we would call a contact_information method, we need to build that method.
[php]
/**
* Callback for Contact Information metabox
*
* @since 1.0.0
*
* @see Child_Theme_Settings::metaboxes()
*/
function contact_information() {

echo '<p>Phone:<br />';
echo '<input type="text" name="' . $this->get_field_name( 'phone' ) . '" id="' . $this->get_field_id( 'phone' ) . '" value="' . esc_attr( $this->get_field_value( 'phone' ) ) . '" size="50" />';
echo '</p>';

echo '<p>Address</p>';
echo '<p><textarea name="' . $this->get_field_name( 'address' ) . '" cols="78" rows="8">' . esc_textarea( $this->get_field_value( 'address' ) ) . '</textarea></p>';
}
[/php]

This is what will go inside your metabox. It is fairly straightforward but there are a couple of functions that I'd like to point out: get_field_name, get_field_id, & get_field_value. These are built in functions that will take information provided in the construct method that will generate proper HTML names, ids, and values based on your $settings_field.

The Help Method

Now my favorite part, and my contribution to the admin class, the help() method. To add contextual help, all you need to do is create a help method.
[php]
/**
* Register contextual help on Child Theme Settings page
*
* @since 1.0.0
*
*/
function help( ) {
global $my_admin_page;
$screen = get_current_screen();

if ( $screen->id != $this->pagehook )
return;

$tab1_help =
'<h3>' . __( 'H3 Heading' , CHILD_DOMAIN ) . '</h3>' .
'<p>' . __( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur in odio lacus. Fusce lacinia viverra facilisis. Nunc urna lorem, tempus in sollicitudin ac, fringilla non lacus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque lacinia, arcu ut porta laoreet, elit justo volutpat augue, commodo condimentum neque sapien a tellus. Fusce tempus elit sodales dui vehicula tempus. Aliquam lobortis laoreet tortor, facilisis blandit sem viverra at. Ut iaculis, metus ac faucibus aliquam, diam tortor commodo felis, sed fermentum velit nunc ac arcu. Ut in libero ante.' , CHILD_DOMAIN ) . '</p>';

$tab2_help =
'<h3>' . __( 'H3 Heading' , CHILD_DOMAIN ) . '</h3>' .
'<p>' . __( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur in odio lacus. Fusce lacinia viverra facilisis. Nunc urna lorem, tempus in sollicitudin ac, fringilla non lacus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque lacinia, arcu ut porta laoreet, elit justo volutpat augue, commodo condimentum neque sapien a tellus. Fusce tempus elit sodales dui vehicula tempus. Aliquam lobortis laoreet tortor, facilisis blandit sem viverra at. Ut iaculis, metus ac faucibus aliquam, diam tortor commodo felis, sed fermentum velit nunc ac arcu. Ut in libero ante.' , CHILD_DOMAIN ) . '</p>';

$tab3_help =
'<h3>' . __( 'H3 Heading' , CHILD_DOMAIN ) . '</h3>' .
'<p>' . __( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur in odio lacus. Fusce lacinia viverra facilisis. Nunc urna lorem, tempus in sollicitudin ac, fringilla non lacus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque lacinia, arcu ut porta laoreet, elit justo volutpat augue, commodo condimentum neque sapien a tellus. Fusce tempus elit sodales dui vehicula tempus. Aliquam lobortis laoreet tortor, facilisis blandit sem viverra at. Ut iaculis, metus ac faucibus aliquam, diam tortor commodo felis, sed fermentum velit nunc ac arcu. Ut in libero ante.' , CHILD_DOMAIN ) . '</p>' .
'<h4>' . __( 'H4 Heading' , CHILD_DOMAIN ) . '</h4>' .
'<p>' . __( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur in odio lacus. Fusce lacinia viverra facilisis. Nunc urna lorem, tempus in sollicitudin ac, fringilla non lacus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque lacinia, arcu ut porta laoreet, elit justo volutpat augue, commodo condimentum neque sapien a tellus. Fusce tempus elit sodales dui vehicula tempus. Aliquam lobortis laoreet tortor, facilisis blandit sem viverra at. Ut iaculis, metus ac faucibus aliquam, diam tortor commodo felis, sed fermentum velit nunc ac arcu. Ut in libero ante.' , CHILD_DOMAIN ) . '</p>';

$screen->add_help_tab(
array(
'id' => $this->pagehook . '-tab1',
'title' => __( 'Tab 1' , CHILD_DOMAIN ),
'content' => $tab1_help,
) );
$screen->add_help_tab(
array(
'id' => $this->pagehook . '-tab2',
'title' => __( 'Tab 2' , CHILD_DOMAIN ),
'content' => $tab2_help,
) );
$screen->add_help_tab(
array(
'id' => $this->pagehook . '-tab3',
'title' => __( 'Tab 3' , CHILD_DOMAIN ),
'content' => $tab3_help,
) );

// Add Genesis Sidebar
$screen->set_help_sidebar(
'<p><strong>' . __( 'For more information:', CHILD_DOMAIN ) . '</strong></p>'.
'<p><a href="' . __( 'http://www.studiopress.com/support', CHILD_DOMAIN ) . '" target="_blank" title="' . __( 'Support Forums', CHILD_DOMAIN ) . '">' . __( 'Support Forums', CHILD_DOMAIN ) . '</a></p>'.
'<p><a href="' . __( 'http://www.studiopress.com/tutorials', CHILD_DOMAIN ) . '" target="_blank" title="' . __( 'Genesis Tutorials', CHILD_DOMAIN ) . '">' . __( 'Genesis Tutorials', CHILD_DOMAIN ) . '</a></p>'.
'<p><a href="' . __( 'http://dev.studiopress.com/', CHILD_DOMAIN ) . '" target="_blank" title="' . __( 'Genesis Developer Docs', CHILD_DOMAIN ) . '">' . __( 'Genesis Developer Docs', CHILD_DOMAIN ) . '</a></p>'
);
}
[/php]

You simply have to try it!

Adding the Child Theme Settings Page

Now that we have the class built, nothing happens until we instantiate or initialize the class. So we create a function to do this after we close the class with a closing bracket. Then we call the class:
[php]
/**
* Add the Theme Settings Page
*
* @since 1.0.0
*/
function wps_add_child_theme_settings() {
global $_child_theme_settings;
$_child_theme_settings = new Child_Theme_Settings;
}
add_action( 'admin_menu', 'wps_add_child_theme_settings', 5 );
[/php]

So here is our page in full:
[php]
<?php

/**
* Child Theme Settings
* Requires Genesis 1.8 or later
*
* This file registers all of this child theme's
* specific Theme Settings, accessible from
* Genesis > Child Theme Settings.
*
* @package WPS_Starter_Genesis_Child
* @author Travis Smith <[email protected]>
* @copyright Copyright (c) 2012, Travis Smith
* @license http://opensource.org/licenses/gpl-2.0.php GNU Public License
* @since 1.0
* @alter 1.1.2012
*
*/

/**
* Registers a new admin page, providing content and corresponding menu item
* for the Child Theme Settings page.
*
* @package WPS_Starter_Genesis_Child
* @subpackage Admin
*
* @since 1.0.0
*/
class Child_Theme_Settings extends Genesis_Admin_Boxes {

/**
* Create an admin menu item and settings page.
*
* @since 1.0.0
*/
function __construct() {

// Specify a unique page ID.
$page_id = 'child';

// Set it as a child to genesis, and define the menu and page titles
$menu_ops = array(
'submenu' => array(
'parent_slug' => 'upload.php',
'page_title' => 'Genesis - WPS Starter Settings',
'menu_title' => 'WPS Starter Settings',
'capability' => 'manage_options',
)
);

// Set up page options. These are optional, so only uncomment if you want to change the defaults
$page_ops = array(
// 'screen_icon' => array( 'custom' => WPS_ADMIN_IMAGES . '/staff_32x32.png' ),
'screen_icon' => 'users',
// 'save_button_text' => 'Save Settings',
// 'reset_button_text' => 'Reset Settings',
// 'save_notice_text' => 'Settings saved.',
// 'reset_notice_text' => 'Settings reset.',
);

// Give it a unique settings field.
// You'll access them from genesis_get_option( 'option_name', CHILD_SETTINGS_FIELD );
$settings_field = CHILD_SETTINGS_FIELD;

// Set the default values
$default_settings = array(
'phone' => '',
'address' => '',
);

// Create the Admin Page
$this->create( $page_id, $menu_ops, $page_ops, $settings_field, $default_settings );

// Initialize the Sanitization Filter
add_action( 'genesis_settings_sanitizer_init', array( $this, 'sanitization_filters' ) );

}

/**
* Set up Sanitization Filters
*
* See /lib/classes/sanitization.php for all available filters.
*
* @since 1.0.0
*/
function sanitization_filters() {

genesis_add_option_filter( 'no_html', $this->settings_field,
array(
'phone',
'address',
) );
}

/**
* Register metaboxes on Child Theme Settings page
*
* @since 1.0.0
*
* @see Child_Theme_Settings::contact_information() Callback for contact information
*/
function metaboxes() {

add_meta_box('contact-information', 'Contact Information', array( $this, 'contact_information' ), $this->pagehook, 'main', 'high');

}

/**
* Register contextual help on Child Theme Settings page
*
* @since 1.0.0
*
*/
function help( ) {
global $my_admin_page;
$screen = get_current_screen();

if ( $screen->id != $this->pagehook )
return;

$tab1_help =
'<h3>' . __( 'H3 Heading' , CHILD_DOMAIN ) . '</h3>' .
'<p>' . __( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur in odio lacus. Fusce lacinia viverra facilisis. Nunc urna lorem, tempus in sollicitudin ac, fringilla non lacus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque lacinia, arcu ut porta laoreet, elit justo volutpat augue, commodo condimentum neque sapien a tellus. Fusce tempus elit sodales dui vehicula tempus. Aliquam lobortis laoreet tortor, facilisis blandit sem viverra at. Ut iaculis, metus ac faucibus aliquam, diam tortor commodo felis, sed fermentum velit nunc ac arcu. Ut in libero ante.' , CHILD_DOMAIN ) . '</p>';

$tab2_help =
'<h3>' . __( 'H3 Heading' , CHILD_DOMAIN ) . '</h3>' .
'<p>' . __( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur in odio lacus. Fusce lacinia viverra facilisis. Nunc urna lorem, tempus in sollicitudin ac, fringilla non lacus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque lacinia, arcu ut porta laoreet, elit justo volutpat augue, commodo condimentum neque sapien a tellus. Fusce tempus elit sodales dui vehicula tempus. Aliquam lobortis laoreet tortor, facilisis blandit sem viverra at. Ut iaculis, metus ac faucibus aliquam, diam tortor commodo felis, sed fermentum velit nunc ac arcu. Ut in libero ante.' , CHILD_DOMAIN ) . '</p>';

$tab3_help =
'<h3>' . __( 'H3 Heading' , CHILD_DOMAIN ) . '</h3>' .
'<p>' . __( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur in odio lacus. Fusce lacinia viverra facilisis. Nunc urna lorem, tempus in sollicitudin ac, fringilla non lacus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque lacinia, arcu ut porta laoreet, elit justo volutpat augue, commodo condimentum neque sapien a tellus. Fusce tempus elit sodales dui vehicula tempus. Aliquam lobortis laoreet tortor, facilisis blandit sem viverra at. Ut iaculis, metus ac faucibus aliquam, diam tortor commodo felis, sed fermentum velit nunc ac arcu. Ut in libero ante.' , CHILD_DOMAIN ) . '</p>' .
'<h4>' . __( 'H4 Heading' , CHILD_DOMAIN ) . '</h4>' .
'<p>' . __( 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur in odio lacus. Fusce lacinia viverra facilisis. Nunc urna lorem, tempus in sollicitudin ac, fringilla non lacus. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Pellentesque lacinia, arcu ut porta laoreet, elit justo volutpat augue, commodo condimentum neque sapien a tellus. Fusce tempus elit sodales dui vehicula tempus. Aliquam lobortis laoreet tortor, facilisis blandit sem viverra at. Ut iaculis, metus ac faucibus aliquam, diam tortor commodo felis, sed fermentum velit nunc ac arcu. Ut in libero ante.' , CHILD_DOMAIN ) . '</p>';

$screen->add_help_tab(
array(
'id' => $this->pagehook . '-tab1',
'title' => __( 'Tab 1' , CHILD_DOMAIN ),
'content' => $tab1_help,
) );
$screen->add_help_tab(
array(
'id' => $this->pagehook . '-tab2',
'title' => __( 'Tab 2' , CHILD_DOMAIN ),
'content' => $tab2_help,
) );
$screen->add_help_tab(
array(
'id' => $this->pagehook . '-tab3',
'title' => __( 'Tab 3' , CHILD_DOMAIN ),
'content' => $tab3_help,
) );

// Add Genesis Sidebar
$screen->set_help_sidebar(
'<p><strong>' . __( 'For more information:', CHILD_DOMAIN ) . '</strong></p>'.
'<p><a href="' . __( 'http://www.studiopress.com/support', CHILD_DOMAIN ) . '" target="_blank" title="' . __( 'Support Forums', CHILD_DOMAIN ) . '">' . __( 'Support Forums', CHILD_DOMAIN ) . '</a></p>'.
'<p><a href="' . __( 'http://www.studiopress.com/tutorials', CHILD_DOMAIN ) . '" target="_blank" title="' . __( 'Genesis Tutorials', CHILD_DOMAIN ) . '">' . __( 'Genesis Tutorials', CHILD_DOMAIN ) . '</a></p>'.
'<p><a href="' . __( 'http://dev.studiopress.com/', CHILD_DOMAIN ) . '" target="_blank" title="' . __( 'Genesis Developer Docs', CHILD_DOMAIN ) . '">' . __( 'Genesis Developer Docs', CHILD_DOMAIN ) . '</a></p>'
);
}

/**
* Callback for Contact Information metabox
*
* @since 1.0.0
*
* @see Child_Theme_Settings::metaboxes()
*/
function contact_information() {

echo '<p>Phone:<br />';
echo '<input type="text" name="' . $this->get_field_name( 'phone' ) . '" id="' . $this->get_field_id( 'phone' ) . '" value="' . esc_attr( $this->get_field_value( 'phone' ) ) . '" size="50" />';
echo '</p>';

echo '<p>Address</p>';
echo '<p><textarea name="' . $this->get_field_name( 'address' ) . '" cols="78" rows="8">' . esc_textarea( $this->get_field_value( 'address' ) ) . '</textarea></p>';
}

}

/**
* Add the Theme Settings Page
*
* @since 1.0.0
*/
function wps_add_child_theme_settings() {
global $_child_theme_settings;
$_child_theme_settings = new Child_Theme_Settings;
}
add_action( 'admin_menu', 'wps_add_child_theme_settings', 5 );
[/php]

Written by Travis Smith · Categorized: Genesis

Jan 06 2012

Developing a Custom Post Type and a Custom Taxonomy in Genesis with Custom Pages, Part 5

Now, in Developing a Custom Post Type and a Custom Taxonomy in Genesis with Custom Pages, Part 1, we have the registration of the custom post type, custom taxonomy and the addition of the metaboxes. In Part 2, we discussed the Single Page Template for the custom post type. In Part 3, we discussed the Taxonomy Template for the custom taxonomy. In Part 4, we discussed the a page template for the custom post type.

Now this tutorial will discuss Creating Columns in the Edit Custom Post Page. Justin Tadlock has an excellent post on this and is a must read: Custom columns for custom post types. For those, however, who don't like coding, there is a good plugin that will do this work for you. Pippin's Post Type Column Editor from Code Canyon (affil link) is only $12 and worth the money, if you just lovehate coding.

Column Editor

Before, I move forward to how to do this via php and functions.php, let me highlight Pippin's plugin (so if you care about the code skip this and the pictures). With post Type Column Editor you can easily customize the dashboard columns for all your post types. So if you wanted to add thumbnails and excerpts to the columns of posts and/or pages (if pages supports excerpts), then you can easily add it with a few clicks. This plugin gives you a really easy to use way to modify and manage the table columns for your post types. You can display post type entry titles, categories, tags, excerpts, authors, custom meta fields, thumbnails, and custom taxonomies. You can customize the columns for each built-in and custom post type separately with a straight forward drag-and-drop interface.

So when you first register a custom post type, here is what you'll see:

Custom Post Type with No Columns
Initial Custom Post Type

Then when you use the Post Type Column Editor, you see your various options.

Post Type Column Editor Options
Post Type Column Editor Options

For a demonstration check out this Screenr:

This obviously works well with his Easy Content Types plugin which outputs PHP code for you to embed into your plugin and/or theme.

Sometimes it becomes necessary to display more than the usual Title and Date of our post types. In this case, we want to add a new column to the edit post page to allows us to easily see which axle size our disc brakes belong to. There are some great examples of how to do that but below is an example of how we can add our axle size to the page.

First we must add the column. Use the code below and add it to our wps-admin-functions.php file.

[php]
// Add Columns for Disc Brakes Edit Posts Page
add_filter( 'manage_edit-wps_discbrakes_columns', 'wps_discbrakes_columns' ) ;
function wps_discbrakes_columns($column) {
$column = array(
'cb' => '<input type="checkbox" />',
'title' => __( 'Disc Brakes' ),
'axlesizes' => __( 'Axle Sizes' ),
'date' => __( 'Date' )
);
return $column;
}
[/php]

If we save this file and upload it, we will now see a new column called Axle Sizes. This is great but there is no content in this column. Now we need to go get the content and display here. Use this code below to do just that.

[php]
// Gets the Taxonomy Terms and retunrs them on the Disc Brakes Edit Posts Page
add_action( 'manage_wps_discbrakes_posts_custom_column', 'manage_wps_discbrakes_columns', 10, 2 );
function manage_wps_discbrakes_columns( $column, $post_id ) {
global $post;
switch( $column ) {
case 'axlesizes' :
$terms = get_the_terms( $post_id, 'wps_axlesizes' );
if ( !empty( $terms ) ) {
$out = array();
foreach ( $terms as $term ) {
$out[] = sprintf( '<a href="%s">%s</a>', esc_url( add_query_arg( array( 'post_type' => $post->post_type, 'wps_axlesizes' => $term->slug ), 'edit.php' ) ), esc_html( sanitize_term_field( 'name', $term->name, $term->term_id, 'wps_axlesizes', 'display' ) ) );
}

echo join( ', ', $out );
} else {
_e( 'No Axle Sizes' );
}

break;

default :
break;
}
}
[/php]

The code above allows us to select one of the axle sizes and it will only display the disc brakes for that size.
Custom Column for Custom Post Type

Written by Travis Smith · Categorized: Custom Post Types, Genesis

Jan 05 2012

Developing a Custom Post Type and a Custom Taxonomy in Genesis with Custom Pages, Part 4

Now, in Developing a Custom Post Type and a Custom Taxonomy in Genesis with Custom Pages, Part 1, we have the registration of the custom post type, custom taxonomy and the addition of the metaboxes. In Part 2, we discussed the Single Page Template for the custom post type. In Part 3, we discussed the Taxonomy Template for the custom taxonomy. For good measure, let's create a basic page template to wrap this all up. This will create a front page of sorts for the custom taxonomy and the single custom post type pages.

A. File Setup

First, create the file as page-{whatever}.php (WordPress Codex). While you can use page-{slug}.php, this can cause some unexpected issues. So naming it page-{whatever}.php forces the user to assign it via the page templates area. So in our case, it will be page-brakes.php.

B. Template Name

The first thing you must enter with any page template is the Template Name.
[php]<?php
/*
*Template Name: Disc Brakes Template
*/
[/php]

B. Menu Move

Now, for this example, I want to use a menu system. So I will be using the secondary menu system to accomplish this, and I want this to appear below the title.

[php]<?php
// Place the secondary navigation menu below the title
remove_action( 'genesis_after_header', 'genesis_do_subnav' );
add_action( 'genesis_after_post_title', 'genesis_do_subnav' );

// Enable the secondary navigation menu for single post type
add_filter('genesis_options', 'wps_define_genesis_setting' , 10, 2);
function wps_define_genesis_setting( $options, $setting ) {
if( $setting == GENESIS_SETTINGS_FIELD ) {
$options['subnav'] = 1;
}
return $options;
}
[/php]

If you notice, I programmatically turn on the secondary menu. However, again, in Genesis > Theme Settings, site-wide, I have the secondary navigation system turned off. Now, for this page template, I have selectively turned it on.

If you use the secondary navigation for your site, simply register a new navigation system and then add it. See the previous tutorial for this information.

C. Include the Genesis Framework **VERY IMPORTANT

[php]<?php
genesis();
[/php]

So here is our finished product: page-brakes.php
[php]
<?php

/*
*Template Name: Disc Brakes Template
*/

// Place the secondary navigation menu below the title
remove_action( 'genesis_after_header', 'genesis_do_subnav' );
add_action( 'genesis_after_post_title', 'genesis_do_subnav' );

// Enable the secondary navigation menu for single post type
add_filter('genesis_options', 'wps_define_genesis_setting' , 10, 2);
function wps_define_genesis_setting( $options, $setting ) {
if( $setting == GENESIS_SETTINGS_FIELD ) {
$options['subnav'] = 1;
}
return $options;
}

genesis();
[/php]

Written by Travis Smith · Categorized: Custom Post Types, Genesis

  • « Previous Page
  • 1
  • …
  • 25
  • 26
  • 27
  • 28
  • 29
  • …
  • 60
  • Next Page »

Need Help?

Please let us know how we can help you!

Get Help

Recommendations

Genesis WordPress Framework
Sucuri Security
Gravity Forms
GetSoliloquy
Get Envira
Scribe SEO
BackupBuddy
WordPress Video User Manuals

Recent Posts

  • Solving WordPress 5XX Server Errors on SiteGround
  • Hiding an User in the WordPress Admin
  • Custom Rewrite Rules for Custom Post Types and Taxonomies
  • WordPress JavaScript Manager Native Functions
  • Causes of WordPress Site Performance Slowdown

About Travis

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.

  • Twitter
  • Facebook
  • LinkedIn
  • Google+
  • RSS

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