WP Smith

Creating WordPress & Genesis Websites Since 2010

  • Home
  • About
  • Services
  • Blog
  • Contact

Sep 12 2011

How to Add a Login Form to the Primary Navigation Menu in Genesis

Someone contacted me to develop a function that would add a login form to the primary navigation for their Genesis menu, and I would like to share it with you.

Primary Navigation with Login

[php]';
$login .= '';
$login .= '';
$login .= '';
$login .= '';
$login .= '';
$menu .= '

  • ' . $login . '
  • ';
    }
    // if logged in, do logout link
    else {
    global $current_user;
    get_currentuserinfo();
    $logout = '';
    $logout = 'Welcome ' . $current_user->display_name;
    $logout .= 'Logout';
    $menu .= '

  • ' . $logout . '
  • ';
    }

    return $menu;
    }
    [/php]

    Written by Travis Smith · Categorized: Tutorials, WordPress

    Sep 09 2011

    How to Replace Functions Regardless of the Genesis Hook (via Gary Jones)

    Recently, Gary Jones (@GaryJ) posted a bit of code for Jared Atchison (@jaredatch) on Twitter for a plugin that he is working on that will remove functions regardless of where they get hooked. It is a great piece of code. If you find this helpful, please support Gary or tell him how much you appreciate him.

    [php]<?php
    // Run at get_header to catch all customisations in functions.php

    add_action( 'get_header', 'jared_remove_stuff_whichever_hook_they_are_on' );
    /**
    * Replace some genesis_* functions hooked into somewhere for some jared_* functions
    * of the same suffix, at the same hook and priority
    *
    * @author Gary Jones
    *
    * @global array $wp_filter
    */
    function jared_remove_stuff_whichever_hook_they_are_on() {

    global $wp_filter;

    // List of genesis_* functions to be replaced with jared_* functions.
    // We save some bytes and add the ubiquitous 'genesis_' later on.
    $functions = array(
    'do_doctype',
    'do_nav',
    'do_subnav',
    'header_markup_open',
    'header_markup_close',
    'post_info',
    'post_meta',
    'do_loop',
    'footer_markup_open',
    'footer_markup_close'
    );

    // Loop through all hooks (yes, stored under the $wp_filter global)
    foreach ( $wp_filter as $hook => $priority) {

    // Loop through our array of functions for each hook
    foreach( $functions as $function) {

    // has_action returns int for the priority
    if ( $priority = has_action( $hook, 'genesis_' . $function ) ) {

    // If there's a function hooked in, remove the genesis_* function
    // from whichever hook we're looping through at the time.
    remove_action( $hook, 'genesis_' . $function, $priority );

    // Add a replacement function in at the same time.
    add_action( $hook, 'jared_' . $function, $priority );
    }
    }
    }

    }[/php]

    Written by Travis Smith · Categorized: Genesis

    Aug 27 2011

    How to Add Custom Background Support to a Genesis Child Theme and Set an Easily Changed Default Background

    **UPDATE: Since writing this post, I have updated this code to be a plugin with everything. Please feel free to check it out: Genesis Custom Backgrounds.**

    I believe it would be really great if Genesis would support custom backgrounds (forum post). Anyways, the newly developed backgrounds (dark, light), while not part of core, could be easily integrated into ANY child theme.

    While I am not sure that this will become part of the Genesis core, and if I had to place a bet, I would bet against me. However, the benefits of this code, are as follows:

    • The code can easily be unhooked and removed
    • The code makes setting defaults easy, which is not part of WordPress, which is perfect for Child Themes
    • A few filters to customize even further

    To install the code, simply paste the following in your functions.php, or download the file at the end of the post.
    [php]<?php
    /**
    * Enables Genesis support of custom backgrounds
    *
    * @author Travis Smith
    *
    * @uses add_custom_background() calls standard, filterable callback
    * @uses apply_filters() filters callback
    *
    */
    function genesis_custom_background() {
    global $genesis_settings;

    $genesis_settings[ 'cb_default' ] = ''; //assuming this will be placed with the other defaults
    $cb = apply_filters( 'genesis_custom_background_args' , 'genesis_do_theme_background' );
    add_custom_background( $cb );
    }
    add_action( 'init' , 'genesis_custom_background' );

    /**
    * Outputs custom backgrounds inline
    *
    * @author Travis Smith
    *
    * @uses $genesis_settings for custom background default image
    * @uses apply_filters() filters defaults
    *
    */
    function genesis_do_theme_background() {
    global $genesis_settings;

    $defaults = array(
    'default_img' => ( isset( $genesis_settings['cb_default'] ) ) ? $genesis_settings['cb_default'] : '', //url, e.g., PARENT_URL . "/images/bg/gray/gray-1.png
    'bgimage' => get_background_image(),
    'bgcolor' => get_background_color(),
    );
    $defaults = apply_filters( 'genesis_background_defaults' , $defaults );

    extract( $defaults , EXTR_SKIP );

    // begin output
    $output = "<style type='text/css'>n";

    if( !empty( $bgimage ) ) {
    $background_styles = 'background-image: url('' . get_theme_mod( 'background_image' , '' ) . '');'
    . ' background-repeat: ' . get_theme_mod( 'background_repeat' , 'repeat' ) . ';'
    . ' background-position: top ' . get_theme_mod( 'background_position_x' , 'left' ) . ';' . 'background-attachment: '. get_theme_mod( 'background_attachment' , 'scroll' );
    $output .= "body { " . $background_styles . "); } n";
    }

    if( !empty( $bgcolor ) ) {
    $output .= "body { background-color: #" . $bgcolor . "; }; n";
    }

    // for child themes to set a default bg img
    if( !empty( $default_img ) && empty( $bgcolor ) && empty( $bgimage ) ) {
    $output .= "body { background: url('" . $default_img . "'); }n";
    }
    $output .= "</style>";

    echo apply_filters( 'genesis_background_output' , $output , $output, $bgimage , $bgcolor , ( isset( $background_styles ) ) ? $background_styles : '' );
    }
    [/php]

    This would be used by child themes as such:
    [php]<?php
    add_filter( 'genesis_background_defaults' , 'genesis_custom_background_childdefault' );
    function genesis_custom_background_childdefault( $defaults ) {
    //change the URL to whatever you'd like
    $defaults['default_img'] = CHILD_URL . '/images/bg/gray/gray-1.png';

    return $defaults;
    }[/php]

    If you wanted to include the StudioPress backgrounds, they are released to GPL and can be placed in any child theme folder. So feel free to download them (dark, light).

    To download this file, click here: [download id="15"]. Then place it in your child theme folder and include the following in your functions.php file. Assuming you place it in a lib folder within your child theme...
    [php]require_once(CHILD_DIR.'/lib/wps-genesis-custom-backgrounds.php');[/php]

    Written by Travis Smith · Categorized: WordPress

    Aug 26 2011

    How to Add Menu Descriptions & Featured Images to WordPress Menu Items

    Recently I read an awesome post by Bill Erickson about customizing WordPress menus. However, there are two things I wanted this class to do that in its original form it doesn't do.

    So since out of the box, WordPress doesn't intuitively allow you to add descriptions to the menu without requiring some custom code, rather the Walker class extended.

    Since Bill has done an excellent job at this I will extend his code to add the options of having a featured image and/or descriptions applied based on depth.

    Add this to your functions.php file.

    <?php
    class Menu_With_Description extends Walker_Nav_Menu {
    function start_el( &$output, $item, $depth, $args ) {
    global $wp_query;
    $indent = ( $depth ) ? str_repeat( "t", $depth ) : '';
    $class_names = $value = '';
    $classes = empty( $item->classes ) ? array() : (array) $item->classes;
    $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
    $class_names = ' class="' . esc_attr( $class_names ) . '"';
    $output .= $indent . '<li id="menu-item-' . $item->ID . '"' . $value . $class_names . '>';
    $attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) . '"' : '';
    $attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) . '"' : '';
    $attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) . '"' : '';
    $attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) . '"' : '';
    // get user defined attributes for thumbnail images
    $attr_defaults = array(
    'class' => 'nav_thumb',
    'alt' => esc_attr( $item->attr_title ),
    'title' => esc_attr( $item->attr_title )
    );
    $attr = isset( $args->thumbnail_attr ) ? $args->thumbnail_attr : '';
    $attr = wp_parse_args( $attr, $attr_defaults );
    $item_output = $args->before;
    // thumbnail image output
    $item_output .= ( isset( $args->thumbnail_link ) && $args->thumbnail_link ) ? '<a' . $attributes . '>' : '';
    $item_output .= apply_filters( 'menu_item_thumbnail', ( isset( $args->thumbnail ) && $args->thumbnail ) ? get_the_post_thumbnail( $item->object_id, ( isset( $args->thumbnail_size ) ) ? $args->thumbnail_size : 'thumbnail', $attr ) : '', $item, $args, $depth );
    $item_output .= ( isset( $args->thumbnail_link ) && $args->thumbnail_link ) ? '</a>' : '';
    // menu link output
    $item_output .= '<a' . $attributes . '>';
    $item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
    // menu description output based on depth
    $item_output .= ( $args->desc_depth >= $depth ) ? '<br /><span class="sub">' . $item->description . '</span>' : '';
    // close menu link anchor
    $item_output .= '</a>';
    $item_output .= $args->after;
    $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
    }
    }
    view raw Menu_With_Description.php hosted with ❤ by GitHub

     

    All Menus

    To highjack ALL of your menus, enter this code in your functions.php.

    <?php
    add_filter( 'wp_nav_menu_args', 'my_add_menu_descriptions' );
    function my_add_menu_descriptions( $args ) {
    $args['walker'] = new Menu_With_Description;
    $args['desc_depth'] = 0;
    $args['thumbnail'] = true;
    $args['thumbnail_link'] = false;
    $args['thumbnail_size'] = 'nav_thumb';
    $args['thumbnail_attr'] = array( 'class' => 'nav_thumb my_thumb', 'alt' => 'test', 'title' => 'test' );
    return $args;
    }
    view raw my_add_menu_descriptions.php hosted with ❤ by GitHub

    Menus Based on Location

    To highjack a menu based on registered and assigned location, then use this code in your functions.php.

    <?php
    add_filter( 'wp_nav_menu_args', 'my_add_menu_descriptions' );
    function my_add_menu_descriptions( $args ) {
    if ( $args['theme_location'] == 'primary' ) {
    $args['walker'] = new Menu_With_Description;
    $args['desc_depth'] = 0;
    $args['thumbnail'] = true;
    $args['thumbnail_link'] = false;
    $args['thumbnail_size'] = 'nav_thumb';
    $args['thumbnail_attr'] = array( 'class' => 'nav_thumb my_thumb', 'alt' => 'test', 'title' => 'test' );
    }
    return $args;
    }
    view raw my_add_menu_descriptions.php hosted with ❤ by GitHub

     

    Menus via WordPress Custom Menu Widget

    To highjack a custom menu based menu id that is called using the custom menu widget, or even a manual method (if the menu is being called by a location, you must use 'theme_location'), then use this code in your functions.php. This will highjack any menu called by the custom menu widget assuming you only have the standard registered theme locations. The example below assumes a standard theme custom menu registered locations for Genesis. Change 'primary' and/or 'secondary' to whatever your theme adds and add whatever more menus that your theme has to only target the custom menu widget.

    <?php
    add_filter( 'wp_nav_menu_args', 'my_add_menu_descriptions' );
    function my_add_menu_descriptions( $args ) {
    if ( $args['theme_location'] != 'primary' && $args['theme_location'] != 'secondary' ) {
    $args['walker'] = new Menu_With_Description;
    $args['desc_depth'] = 0;
    $args['thumbnail'] = true;
    $args['thumbnail_link'] = false;
    $args['thumbnail_size'] = 'nav_thumb';
    $args['thumbnail_attr'] = array( 'class' => 'nav_thumb my_thumb', 'alt' => 'test', 'title' => 'test' );
    }
    return $args;
    }
    view raw my_add_menu_descriptions.php hosted with ❤ by GitHub

    There is a caveat though. There are certain menu items that may not have a featured image such as custom links, categories, etc. There is a possible work-around that I am trying to work out.

    Here is the result of a sandbox description and featured images for all types (not just pages and posts), which I will be posting soon or writing up into a plugin...

    Sandbox Genesis Menu with Descriptions and Featured Images

    Written by Travis Smith · Categorized: Tutorials, WordPress

    Aug 25 2011

    How to Get Original Image from a Thumbnail Image in WordPress

    Recently, in order to dummy proof a metabox that accepts a file upload via Media Library from a user accidentally selecting something other than Original Image like Small, Medium, or Large, I wrote a validation script to check and correct this.
    Media Library: Insert Into Posts
    This function will change a thumbnail URL (http://domain.com/wp-content/uploads/2011/08/example_2a-158x300.jpg) to the original URL (http://domain.com/wp-content/uploads/2011/08/example_2a.jpg). To use this function, simply:

    [php]$url = wps_get_original_image( $url );[/php]

    Here is the script that can be added to your functions.php:

    [php]<!--?php function wps_get_original_image( $url ) { global $_wp_additional_image_sizes; $new_url = $url; // Get All Image Sizes $builtin_sizes = array( 'large' =--> array(
    'width' => get_option('large_size_w'),
    'height' => get_option('large_size_h')
    ),
    'medium' => array(
    'width' => get_option('medium_size_w'),
    'height' => get_option('medium_size_h')
    ),
    'thumbnail' => array(
    'width' => get_option('thumbnail_size_w'),
    'height' => get_option('thumbnail_size_h')
    )
    );

    $image_sizes = array_merge( $builtin_sizes, $_wp_additional_image_sizes );

    //$url = 'http://webendev.com/review/scheumanndental/wp-content/uploads/2011/08/example_2a-300x158.jpg';

    // Parse URL
    $info = pathinfo( $url );

    // Check to see if it is a post-thumbnail: assuming size < 999x999 so -999x999.jpg = 12chars $pos = strrpos($info['basename'], '-', -1); $length = strlen( $info['basename'] ); if ( ( $length - $pos ) > 12 )
    return $url;

    // Check to see if image is indeed a thumbnail and not example-2.jpg

    // Get image size extensions, e.g. -200x200
    // Get image size width only extensions, e.g., -200x
    // Get image size height only extensions, e.g., x200.
    $image_exts = array();
    $image_exts_width = array();
    $image_exts_height = array();
    foreach ($image_sizes as $image_size) {
    $image_exts[] = '-'.$image_size['width'].'x'.$image_size['height'];
    $image_exts_width[] = '-'.$image_size['width'].'x';
    $image_exts_height[] = 'x'.$image_size['height'];
    }

    // Cycle through image size extensions, e.g. -200x200
    foreach ( $image_exts as $image_ext ) {
    //if found, simply replace with nothing (easy)
    $new_url = str_replace( $image_ext , '' , $url );

    //report changed url
    if ( $new_url != $url )
    break;
    }

    // if a new url hasn't been generated...
    if ( $new_url == $url ) {
    // Cycle through image width only extensions, e.g. -200x
    foreach ( $image_exts_width as $image_ext ) {

    // check for width, e.g., -200x
    $pos1 = strrpos( $info['basename'] , $image_ext , -1 );
    if ( $pos1 ) {
    // strip, & assign new url
    $new_url = $info['dirname'] . '/' . substr( $info['basename'] , 0 , $pos1 ) . '.' . $info['extension'];
    }

    if ( $new_url != $url )
    break;
    }

    // if a new url hasn't been generated...
    if ( $new_url == $url ) {
    // Cycle through image height only extensions, e.g. x200.
    foreach ( $image_exts_height as $image_ext ) {

    // check for height, e.g., x200
    $pos2 = strrpos( $info['basename'] , $image_ext , -1 );

    //example_2a-263x300.jpg -> example_2a.jpg
    if ( $pos2 ) {
    // get position of -, strip, & assign new url
    $pos3 = strrpos( $info['basename'] , '-' , -1 );
    $new_url = $info['dirname'] . '/' . substr( $info['basename'] , 0 , $pos3 ) . '.' . $info['extension'];
    }

    if ( $new_url != $url )
    break;
    }
    }
    }

    if ( $new_url != $url )
    return $new_url;
    else
    return $url;
    }
    [/php]

    Written by Travis Smith · Categorized: Tutorials, WordPress

    • « Previous Page
    • 1
    • …
    • 32
    • 33
    • 34
    • 35
    • 36
    • …
    • 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