WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Dec 07 2011

How to Add a Default Custom Header to Your Genesis Child Theme

Recently, I needed to add a header that the client may want to be able to change later. So the obvious solution is a custom header that defaulted to whatever I needed to make it. So here's what I did:

[php]
// Add Custom Header
define( 'HEADER_TEXTCOLOR' , 'ffffff'); //change the number to whatever you want
define( 'HEADER_IMAGE' , CHILD_URL . '/images/logo.png');
define( 'HEADER_IMAGE_WIDTH' , 960 ); //change the number to whatever you want
define( 'HEADER_IMAGE_HEIGHT' , 200 ); //change the number to whatever you want

// gets included in the site header
function wps_header_style() {
?><style type="text/css">
#header {
background: url(<?php header_image(); ?>);
}
</style><?php
}

// gets included in the admin header
function wps_admin_header_style() {
?><style type="text/css">
#headimg {
width: <?php echo HEADER_IMAGE_WIDTH; ?>px;
height: <?php echo HEADER_IMAGE_HEIGHT; ?>px;
background: no-repeat;
}
</style><?php
}

add_custom_image_header( 'wps_header_style', 'wps_admin_header_style' );
[/php]

If you do not want them to be able to change the color of the text then...

[php]
// Add Custom Header
define( 'NO_HEADER_TEXT' , true );
define( 'HEADER_TEXTCOLOR' , '');
define( 'HEADER_IMAGE' , CHILD_URL . '/images/logo.png');
define( 'HEADER_IMAGE_WIDTH' , 960 ); //change the number to whatever you want
define( 'HEADER_IMAGE_HEIGHT' , 200 ); //change the number to whatever you want

// gets included in the site header
function wps_header_style() {
?><style type="text/css">
#header {
background: url(<?php header_image(); ?>);
}
</style><?php
}

// gets included in the admin header
function wps_admin_header_style() {
?><style type="text/css">
#headimg {
width: <?php echo HEADER_IMAGE_WIDTH; ?>px;
height: <?php echo HEADER_IMAGE_HEIGHT; ?>px;
background: no-repeat;
}
</style><?php
}

add_custom_image_header( 'wps_header_style', 'wps_admin_header_style' );
[/php]

Written by Travis Smith · Categorized: Genesis

Dec 06 2011

How to Automatically Add a Class (like 'first' or 'last') to a WordPress Menu

So recently, I needed a way to dynamically add a 'first' class to all the first child nav menu items of a particular parent nav menu item item. On a simple Google, one tutorial by @WPBeginner suggests that we use a filter.
[php]
//Filtering a Class in Navigation Menu Item
add_filter( 'nav_menu_css_class' , 'special_nav_class' , 10 , 2 );
function special_nav_class( $classes, $item ){
if( is_single() && $item->title == 'Blog' ){
$classes[] = 'current-menu-item';
}
return $classes;
}
[/php]
However great this filter is, this approach won't and cannot be very dynamic.

So I wrote a function (and generalized it) that would accomplish this easily and nicely. This uses a function that I wrote about previously called get_nav_menu_item_children() in a post called How to Get All the Children of a Specific Nav Menu Item that I have modified (12/6).

Given a menu id and a parent item id and the optional class name, the function will automatically add that class to the first child nav menu item. So here is the new function:
[php]
/*
* Adds a class to the first menu item.
*
* Uses update_post_meta() to update a nav menu item.
*
* @param int $menu_id Menu ID, defaults to 0.
* @param int $parent_id Menu item ID, defaults to 0.
* @param string $newclass Class to add, defaults to 'first'.
* @param boolean $remove Remove class from other children, defaults to true.
* @return boolean, false: if no $menu_id or $parent_id, else true.
*/
function wps_update_classes( $menu_id = 0 , $parent_id = 0 , $newclass = 'first' , $remove = true ) {

// Validate inputs
$menu_id = intval ( $menu_id );
$parent_id = intval ( $parent_id );

if ( ( $menu_id == 0 ) || ( $parent_id == 0 ) || ( !is_bool ( $remove ) ) || ( !is_string ( $newclass ) ) )
return false;

// Get & filter nav menu items
$nav_menu_items = wp_get_nav_menu_items( $menu_id , array( 'post_status' => 'publish,draft' ) );
$children = get_nav_menu_item_children( $parent_id , $nav_menu_items );
$first_item = true;

// Cycle through child nav menu items
foreach ( $children as $child ) {
if ( $first_item ) {
//found first item
$first_item = false;
$match = false;

// Cycle through to check for first
foreach ( $child->classes as $class ) {
// Match found
if ( $class == $newclass ) {
$match = true;
break;
}
}

// Update Post if match found
if ( ! $match ) {
if ( $child->classes[0] == '' )
$child->classes[0] = $newclass;
else
$child->classes[] = $newclass;

$menu_item_data = array(
'menu-item-classes' => $child->classes,
);
update_post_meta( $child->ID, '_menu_item_classes', $child->classes );
}
continue;
}

// Cycle through and remove $newclass from other children
if ( $remove ) {
foreach ( $child->classes as $key => $class ) {

// If match, remove $newclass from other children
if ( $class == $newclass ) {
$child->classes[$key] = '';
update_post_meta( $child->ID, '_menu_item_classes', $child->classes );
}
}
}
}

return true;
}
[/php]

So to call this function, you can use something like this:
[php]
add_action( 'init' , 'wps_update_menus' );
function wps_update_menus() {
wps_update_classes( 3 , 277 );
wps_update_classes( 3 , 7 , 'test-class' );
}
[/php]
This bit of code adds 'first' to the first child of menu item 277 on menu 3 and 'test-class' to the first child of menu item 7 on menu 3.

If you want to add the ability to add a 'last' class, you can use this:
[php]
/*
* Sorts a multidimensional array of nav menu items.
*
* @link http://php.net/manual/en/function.sort.php
* @param array $array Multidimensional array to be sorted.
* @param string $index Index to determine the sort.
* @param string $order Must be either asc or desc.
* @param boolean $natsort Sort an array using a "natural order" algorithm.
* @param boolean $case_sensitive Sort via case sensitivity.
* @return boolean, false: if no $menu_id or $parent_id, else true.
*/
function wps_sort_nav_menu_items ( $array, $index, $order, $natsort = FALSE, $case_sensitive = FALSE ) {
if( is_array ( $array ) && count( $array ) > 0 ) {
foreach ( array_keys ( $array ) as $key )
$temp[$key] = $array[$key]->$index;
if( ! $natsort ) {
if ( strtolower( $order ) == 'asc' )
asort( $temp );
else
arsort( $temp );
}
else
{
if ( $case_sensitive === true )
natsort( $temp );
else
natcasesort( $temp );
if( strtolower( $order ) != 'asc' )
$temp = array_reverse ( $temp , TRUE );
}
foreach( array_keys ( $temp ) as $key )
if ( is_numeric ( $key ) )
$sorted[] = $array[$key];
else
$sorted[$key] = $array[$key];
return $sorted;
}
return $sorted;
}

/*
* Adds a class to first/last menu item.
*
* Uses update_post_meta() to update a nav menu item.
*
* @param int $menu_id Menu ID, defaults to 0.
* @param int $parent_id Menu item ID, defaults to 0.
* @param string $newclass Class to add, defaults to 'first'.
* @param boolean $remove Remove class from other children, defaults to true.
* @return boolean, false: if no $menu_id or $parent_id, else true.
*/
function wps_update_classes( $menu_id = 0 , $parent_id = 0 , $newclass = 'first' , $firstlast = 'first' , $remove = true ) {

// Validate inputs
$menu_id = intval ( $menu_id );
$parent_id = intval ( $parent_id );

if ( ( $menu_id == 0 ) || ( $parent_id == 0 ) || ( !is_bool ( $remove ) ) || ( !is_string ( $newclass ) ) || ( !is_string ( $firstlast ) ) )
return false;

// Get & filter nav menu items
$nav_menu_items = wp_get_nav_menu_items( $menu_id , array( 'post_status' => 'publish,draft' ) );
$children = get_nav_menu_item_children( $parent_id , $nav_menu_items );
if ( strtolower( $firstlast ) == 'last' )
$children = wps_sort_nav_menu_items( $children , 'menu_order' , 'desc' );
$first_item = true;

// Cycle through child nav menu items
foreach ( $children as $child ) {
if ( $first_item ) {
//found first item
$first_item = false;
$match = false;

// Cycle through to check for first
foreach ( $child->classes as $class ) {
// Match found
if ( $class == $newclass ) {
$match = true;
break;
}
}

// Update Post if match found
if ( ! $match ) {
if ( $child->classes[0] == '' )
$child->classes[0] = $newclass;
else
$child->classes[] = $newclass;

$menu_item_data = array(
'menu-item-classes' => $child->classes,
);
update_post_meta( $child->ID, '_menu_item_classes', $child->classes );
}
continue;
}

// Cycle through and remove $newclass from other children
if ( $remove ) {
foreach ( $child->classes as $key => $class ) {

// If match, remove $newclass from other children
if ( $class == $newclass ) {
$child->classes[$key] = '';
update_post_meta( $child->ID, '_menu_item_classes', $child->classes );
}
}
}
}

return true;
}
[/php]

So to call this function, you can use something like this:
[php]
add_action( 'init' , 'wps_update_menus' );
function wps_update_menus() {
wps_update_classes( 3 , 277 );
wps_update_classes( 3 , 7 , 'last' , 'last' );
}
[/php]
This bit of code adds 'first' to the first child of menu item 277 on menu 3 and 'last' to the last child of menu item 7 on menu 3.

Written by Travis Smith · Categorized: Tutorials, WordPress

Nov 29 2011

Conditionally Remove Post Meta in Genesis

So as I was working through a site, I just wanted to randomly remove some post meta based on the site layout. So I thought, though unique, this may be helpful to someone sometime somewhere sooner or later.

First, by default, the post meta appears in the genesis_after_post_content hook. So you need to add an action for any hook before that hook to run the test to tell WordPress/Genesis to remove post meta or not.

Second, for the check, I wanted to remove it if it were for any other post types other than post and for any posts that was full width.

Then I remove the action that calls the post meta.

[php]
add_action ( 'genesis_post_content' , 'wps_post_meta_check' );
function wps_post_meta_check() {
global $post;

if ( ( $post->post_type != 'post' ) || ( genesis_site_layout() == 'full-width-content' ) )
remove_action( 'genesis_after_post_content', 'genesis_post_meta' );
}
[/php]

Written by Travis Smith · Categorized: Genesis

Nov 23 2011

Using Genesis Featured Widget Amplified Plugin: An In-Depth Developer's Look with Examples

One of my all time favorite Genesis-only plugins is Nick Croft's (AKA @Nick_theGeek) Genesis Featured Widget Amplified. It's a framework sort of widget that allows you to extend it without having to hack it's core. Nick's design around this plugin is incredible.

So let me walk you through some of the advantages of this incredible plugin. Even though this is in the FAQ of the plugin, I thought it would be beneficial to review the hooks and filters.

The Hooks

Genesis Featured Widget Amplified has several hooks. They are as follows:

  1. gfwa_before_loop - before the query is formulated
  2. gfwa_before_post_content - before the content
  3. gfwa_post_content - standard content output
  4. gfwa_after_post_content - after content
  5. gfwa_endwhile - after the endwhile but before the endif
  6. gfwa_after_loop - after the loop endif
  7. gfwa_list_items - within additional list item loop
  8. gfwa_print_list_items - outside the additional list item loop, where list items are output
  9. gfwa_category_more - within the archive more link conditional block
  10. gfwa_after_category_more - after the archive more conditional block
  11. gfwa_form_first_column* within the widget form, end of first column
  12. gfwa_form_second_column* within the widget form, end of second column

*Note: These are misspelled in the code. On the FAQ form, they are spelled correctly; however, the hooks themselves are missing the 'n' at the end of column

I have updated my Genesis Hooks plugin to include these. So if you have Genesis Featured Widget Amplified and the Genesis Hooks plugins installed, you should be able to see all the Genesis Featured Widget Amplified's hooks.

The Filters

Genesis Featured Widget Amplified has a couple filters. They are as follows:

  1. gfwa_exclude_post_types - used to prevent post types from appearing in the post type list in the widget form
  2. gfwa_exclude_taxonomies - used to prevent taxonomies and related terms from appearing in the terms and taxonomies list in the widget form

Widget Instance Identification

Much like the traditional conditionals work, you can locate and identify whether you are dealing with a specific widget instance or not with Genesis Featured Widget Amplified's custom identification field. This is a newly added field at the bottom.

Extending the Plugin

Now for those who love to "improve" plugins, rather, customize it more to one's needs, Nick has included two great hooks to enable this. They are: gfwa_form_first_column and gfwa_form_second_column. These two hooks will allow you to do whatever you'd like to the plugin, extending it beyond any needs that you may have.

So, if I wanted to ask the user if they wanted to display the number of comments for a particular post. I would first need to add it to the form:
[php]
//NOTE: gfwa_form_first_colum is the spelling of the widget-plugin in 0.7.2 (and backwards probably). This should be corrected to gfwa_form_first_column in the next version most likely.
add_action( 'gfwa_form_first_colum' , 'wps_gfwa_add_comments_to_form' );
function wps_gfwa_add_comments_to_form( $instance ) {
global $wp_widget_factory;

if ( ! isset( $instance['wps_comments'] ) )
$instance['wps_comments'] = '';

?>
<div style="background: #f1f1f1; border: 1px solid #DDD; padding: 10px 10px 0px 10px; margin-top: 10px; <?php gfwa_display_option( $instance, 'post_type', 'post', false ); ?>">

<p style="<?php gfwa_display_option( $instance, 'post_type', 'post', false ); ?>">
<input class="widget-control-save" id="<?php echo 'widget-' . $wp_widget_factory->widgets['Genesis_Featured_Widget_Amplified']->id_base . '-' . $wp_widget_factory->widgets['Genesis_Featured_Widget_Amplified']->number . '-wps_comments'; ?>" type="checkbox" name="<?php echo 'widget-' . $wp_widget_factory->widgets['Genesis_Featured_Widget_Amplified']->id_base . '[' . $wp_widget_factory->widgets['Genesis_Featured_Widget_Amplified']->number . '][wps_comments]'; ?>" value="1" <?php checked( 1, $instance['wps_comments'] ); ?> />
<label for="<?php echo 'widget-' . $wp_widget_factory->widgets['Genesis_Featured_Widget_Amplified']->id_base . '-' . $wp_widget_factory->widgets['Genesis_Featured_Widget_Amplified']->number . '-wps_comments'; ?>"><?php _e( 'Show Comments', 'wps-domain' ); ?></label>
</p>

</div>
<?php
}
[/php]

Then I would need to add it to the output via a hook:
[php]
add_action( 'gfwa_after_post_content' , 'wps_gfwa_do_add_comments' );
function wps_gfwa_do_add_comments( $instance ) {
global $post;
if ( isset( $instance['wps_comments'] ) ) {
echo '<span class="wps-comments"> Comments: ' . get_comments_number( $post->ID ) . '</span>';
}
}
[/php]
And as you can see it is outputted:

Other Examples

Example #1:

Take for instance, you want to add some customized post meta (not the way the plugin does it but the way you want to do it) to each one except one. First, without the hooks and the custom field, how could you easily do this?

So I first will unhook the standard post meta function and then I will add my own.
[php]
remove_action( 'gfwa_after_post_content', 'gfwa_do_post_meta', 10, 1 );
add_action( 'gfwa_after_post_content', 'wps_do_post_meta' );
[/php]

Next in my function, I will check to see if the widget is a specific widget to which I do not want to add any post meta, with a simple if-then statement: if ( $instance['custom_field'] == 'digital-edition' ) return;. Then I will add my own post meta using the standard Genesis shortcodes (post_categories & post_tags), customizing the attributes.

[php]
function wps_do_post_meta( $instance ) {
if ( $instance['custom_field'] == 'digital-edition' )
return;

// Add Post Meta
$post_meta = '[ post_categories before="Posted in "]<br /> [ post_tags before="Tags: "]'; //no space after initial bracket
printf( '<p class="post-meta">%s</p>', do_shortcode( $post_meta ) );
}
[/php]

Now this particular example could also be accomplished within the widget form by simply placing [ post_categories before="Posted in "]
[ post_tags before="Tags: "] inside the Post Meta information; however, there is a bug in the code that prevents this. I've already attempted to notify Nick regarding this, which is a simple fix. When he does fix this, this example just demonstrates the amazing customization flexibility.

Example #2:

Now, if on the admin side, I didn't want my client to use a specific post type in the Genesis Featured Widget Amplified for whatever reason, OR, I want to be able to include attachments in the widget, I would need to access the filter gfwa_exclude_post_types.

To include attachments:
[php]add_filter( 'gfwa_exclude_post_types' , 'wps_add_attachments_to_gfwa' );
function wps_add_attachments_to_gfwa( ) {
return array( '' );
}[/php]

To remove pages (and add attachments):
[php]add_filter( 'gfwa_exclude_post_types' , 'wps_remove_pages_from_gfwa' );
function wps_remove_pages_from_gfwa( ) {
return array( 'page' );
}[/php]

To remove pages only:
[php]add_filter( 'gfwa_exclude_post_types' , 'wps_remove_pages_attachments_from_gfwa' );
function wps_remove_pages_attachments_from_gfwa( ) {
return array( 'page' , 'attachments' );
}[/php]

Example #3:

An even more powerful use that parallels the previous filter is the filter with gfwa_exclude_taxonomies. This can enable you to have some private taxonomies for organizational reasons. Out of the box, none are filtered (caveat: for some reason, it does filter nav_menu, but I bet that's a simple oversight and really doesn't have any effect on the plugin).

To remove post Formats:
[php]add_filter( 'gfwa_exclude_taxonomies' , 'wps_remove_post_formats_from_gfwa' );
function wps_remove_post_formats_from_gfwa( ) {
return array( 'post_format' );
}[/php]

To remove Tags:
[php]add_filter( 'gfwa_exclude_taxonomies' , 'wps_remove_post_formats_from_gfwa' );
function wps_remove_post_formats_from_gfwa( ) {
return array( 'post_tag' );
}[/php]

To remove post Formats and Tags:
[php]add_filter( 'gfwa_exclude_taxonomies' , 'wps_remove_post_formats_from_gfwa' );
function wps_remove_post_formats_from_gfwa( ) {
return array( 'post_tag' , 'post_format' );
}[/php]

From a Developer's Perspective

Nick has an extensive roadmap for this plugin that includes the inclusion of post formats, the Grid loop and more on post types (plus more!). Nick has also committed to fixing the spelling of the two hooks, fixing the validation and esc_html() use (which was the post meta problem discussed earlier), and to include a post class filter to be able to do something like this:
[php]
add_filter( 'gfwa_post_class_filter' , 'wps_add_post_class_to_gfwa' );
function wps_add_post_class_to_gfwa( $instance ) {
return $instance['custom_field'];
}[/php]

Written by Travis Smith · Categorized: Genesis, Plugins

Nov 20 2011

How to Manually Call Gravity Forms CSS

Here's how to manually call Gravity Forms CSS. For Genesis users, this is important because our widget may call the form up, as in Scribble, but the CSS is not called because the shortcode is never technically called on that page.

[php]
if ( is_home() )
wp_enqueue_style("gforms_css", GFCommon::get_base_url() . "/css/forms.css", null, GFCommon::$version);
[/php]

Written by Travis Smith · Categorized: Genesis, Plugins

  • « Previous Page
  • 1
  • …
  • 29
  • 30
  • 31
  • 32
  • 33
  • …
  • 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