alexanderdejong.com

Wordpress Developer

  • Home
  • Blog
  • Projects
  • Contact
You are here: Home / Archives for Genesis

Changing and Adding Custom Classes in Genesis HTML 5

Posted on January 4, 2015 Leave a Comment

Sometimes you need to add some more classes to a site element in Genesis. It took me ages to find a proper list and how to do it so here are a few examples and the complete list of filters. Happy coding!

Why would you want to use these? Well you could use them to add Bootstrap classes, in my latest update of SpectacularStays I used it for implementing the Aesop Story Engine into my Genesis Theme.

Adding a class to the header

//* Add custom classes to navigation menu
add_filter( 'genesis_attr_site-header', 'atp_new_header_class' );
function atp_new_header_class( $attributes ) {
 $attributes['class'] = 'site-header new-class-header clearfix fixed';
return $attributes;
}

Adding a class to the post

//* Add custom classes to posts
add_filter( 'genesis_attr_entry', 'atp_post_class' );
function atp_post_class( $attributes ) {
 $attributes['class'] = 'atp-entry-content clearfix';
return $attributes;
}
Context/HTML Genesis Filter
body genesis_attr_body
site-header genesis_attr_site-header
site-title genesis_attr_site-title
site-description genesis_attr_site-description
header-widget-area genesis_attr_header-widget-area
nav-primary genesis_attr_nav-primary
nav-secondary genesis_attr_nav-secondary
nav-header genesis_attr_nav-header
structural-wrap genesis_attr_structural-wrap
content genesis_attr_content
entry genesis_attr_entry
entry-image genesis_attr_entry-image
entry-image-widget genesis_attr_entry-image-widget
entry-image-grid-loop genesis_attr_entry-image-grid-loop
entry-author genesis_attr_entry-author
entry-author-link genesis_attr_entry-author-link
entry-author-name genesis_attr_entry-author-name
entry-time genesis_attr_entry-time
entry-modified-time genesis_attr_entry-modified-time
entry-title genesis_attr_entry-title
entry-content genesis_attr_entry-content
entry-meta-before-content genesis_attr_entry-meta-before-content
entry-meta-after-content genesis_attr_entry-meta-after-content
archive-pagination genesis_attr_archive-pagination
entry-pagination genesis_attr_entry-pagination
adjacent-entry-pagination genesis_attr_adjacent-entry-pagination
comments-pagination genesis_attr_comments-pagination
entry-comments genesis_attr_entry-comments
comment genesis_attr_comment
comment-author genesis_attr_comment-author
comment-author-link genesis_attr_comment-author-link
comment-time genesis_attr_comment-time
comment-time-link genesis_attr_comment-time-link
comment-content genesis_attr_comment-content
author-box genesis_attr_author-box
sidebar_primary genesis_attr_sidebar_primary
sidebar-secondary genesis_attr_sidebar-secondary
site-footer genesis_attr_site-footer

Genesis WordPress Framework adding custom classes to markup This gist was originally created as an example of what I perceived to be an inconsistent behavior, my error was failing to attach my code to action `genesis_setup`. Corrected version now appears below. 20131027 – merged Gary Jones’s fork with corrections and refactoring

<?php
/* 
 * Examples to add custom classes to Genesis WordPress Framework Markup when using HTML5 output
 */
add_action( 'genesis_setup', 'srf_add_cust_classes', 15 ); // Priority 15 ensures it runs after Genesis itself has setup.
function srf_add_cust_classes() {
    add_filter( 'genesis_attr_site-inner', 'srf_attr_site_inner' );
    add_filter( 'genesis_attr_content-sidebar-wrap', 'srf_attr_content_sidebar_wrap' );
    add_filter( 'genesis_attr_content', 'srf_attr_content' );
    add_filter( 'genesis_attr_sidebar-primary', 'srf_attr_sidebar_primary' );
} // Don't add a closing marker comment here - it just clutters the code

// Don't nest functions, move them outside of the hooked in function. While nested functions work, if the outer function is called again for whatever reason, PHP will throw a wobbly when it tries to redefine an existing function.

// Just for fun, I've refactored the common code into one function, and improved it with sanitization.

function srf_add_class( $attr, $class ) {
    $attr['class'] .= ' ' . sanitize_html_class( $class );
    return $attr;
}

// Now the rest of the functions are one line each, and the name of the called function tells us what's happening (add a class).

function srf_attr_site_inner( $attr ) {
    return srf_add_class( $attr, 'example-class-1' );
}

function srf_attr_content_sidebar_wrap( $attr ) {
    return srf_add_class( $attr, 'example-class-2' );
}

function srf_attr_content( $attr ) {
    return srf_add_class( $attr, 'example-class-3' );
}

function srf_attr_sidebar_primary( $attr ) {
    return srf_add_class( $attr, 'example-class-4' );
}

Filed Under: Genesis

Useful Genesis Snippets

Posted on November 14, 2014 Leave a Comment

Add the following snippets to your functions.php.

Remove page title from specific page

This snippet is super useful, it removes page titles from specific pages. You could also use the Genesis title toggle to have a button on each page

add_action( 'get_header', 'remove_titles_home_page' );
function remove_titles_home_page() {
    if ( is_home() ) {
        remove_action( 'genesis_entry_header', 'genesis_do_post_title' );
    }
}

Add jQuery BXslider to your project

This is how you can quickly add the wonderful jQuery plugin BXslider. You target the jQuery function to the div container with a list.

add_action( 'genesis_setup', 'genesischild_theme_setup' );
function genesischild_theme_setup() { 
	add_action( 'genesis_after','genesischild_js_slider' );
}

//Jquery BXslider
function genesischild_js_slider() {
	echo 	"<script> jQuery(document).ready(function(){
  	jQuery('.bxslider').bxSlider({ auto:true });
					});
		</script>";
}

function bxslider_scripts_style(){
	wp_enqueue_script( 'bxslider',  get_stylesheet_directory_uri() . '/js/jquery.bxslider.min.js', array( 'jquery' ), '4.1.2', true );
	wp_enqueue_style( 'bxslider-css',  get_stylesheet_directory_uri() . '/css/jquery.bxslider.css' );
}
add_action( 'wp_enqueue_scripts', 'bxslider_scripts_style');

Useful WooCommerce Billing/Shipping snippets

Simplify your checkout by removing non-essential fields and changing the order. It does wonders with your conversion rate.

// WooCommerce Checkout Fields Hook
add_filter( 'woocommerce_checkout_fields' , 'custom_wc_checkout_fields' );
// Change order comments placeholder and label, and set billing phone number to not required.
function custom_wc_checkout_fields( $fields ) {
	$fields['billing']['billing_phone']['required'] = false;
	$fields['shipping']['shipping_phone']['required'] = false;
	$address_fields['shipping_state']['required'] = false;
	$address_fields['billing_state']['required'] = false;
return $fields;
}

// Change Order Checkout Fields
add_filter("woocommerce_checkout_fields", "order_fields");
function order_fields($fields) {
    $order = array(
        "billing_first_name", 
        "billing_last_name", 
        "billing_address_1", 
        "billing_address_2", 
	"billing_city",
        "billing_postcode",
	"billing_state",
        "billing_country",
        "billing_email", 
        "billing_phone"
    );
    foreach($order as $field)
    {
        $ordered_fields[$field] = $fields["billing"][$field];
    }
    $fields["billing"] = $ordered_fields;
    return $fields;
} 

// Change Order Shipping Fields
add_filter("woocommerce_checkout_fields", "order_shipping_fields");
function order_shipping_fields($fields) {
    $order = array(
        "shipping_first_name", 
        "shipping_last_name",  
        "shipping_address_1", 
        "shipping_address_2", 
	"shipping_city",
        "shipping_postcode",
	"shipping_state",
        "shipping_country", 
	"shipping_company",
    );
    foreach($order as $field)
    {
        $ordered_fields[$field] = $fields["shipping"][$field];
    }
    $fields["shipping"] = $ordered_fields;
    return $fields;
} 

Add FontAwesome to your search button

//* Customize search form input button text
add_filter( 'genesis_search_button_text', 'atp_search_button_text' );
function atp_search_button_text( $text ) {
 
	return esc_attr( '&#xf002;' );
 
}

Add a custom menu in your footer

// Show custom menu in Footer
//add_action( 'genesis_footer', 'custom_menu_in_footer',10 );
function custom_menu_in_footer() {
	$class = 'menu genesis-nav-menu menu-footer';
	$args = array(
		'menu'           => 'Footer Menu', // Enter name of your custom menu here
		'container'      => '',
		'menu_class'     => $class,
		'echo'           => 0,
		'depth'           => 1,
	);
	$nav = wp_nav_menu( $args );
	$nav_markup_open = genesis_markup( array(
		'html5'   => '<nav %s>',
		'xhtml'   => '<div id="nav">',
		'context' => 'nav-footer',
		'echo'    => false,
	) );
	$nav_markup_open .= genesis_structural_wrap( 'menu-footer', 'open', 0 );
	$nav_markup_close  = genesis_structural_wrap( 'menu-footer', 'close', 0 );
	$nav_markup_close .= genesis_html5() ? '</nav>' : '</div>';
	$nav_output = $nav_markup_open . $nav . $nav_markup_close;
	echo $nav_output;
}

Useful Role snippets

function remove_roles(){
//Remove obsolete WP roles if you like with this code
//$wp_roles = new WP_Roles();
//$wp_roles->remove_role("customer");
//$wp_roles->remove_role("premium");
}

function show_role(){  
//$roles = $wp_roles->get_names();
//print_r($roles);
}

function add_new_role(){  
$result = add_role(
    'paid',
    __( 'Paid' ),
    array(
        'read'         => true,  // true allows this capability
        'edit_posts'   => false,
        'delete_posts' => false, // Use false to explicitly deny
    )
);
if ( null !== $result ) {
    echo 'Yay! New role created!';
}
else {
    echo 'Oh... the basic_contributor role already exists.';
}
}

add_filter('woocommerce_get_price', 'custom_price_WPA111772', 10, 2);
/**
 * custom_price_WPA111772 
 *
 * filter the price based on category and user role
 * @param  $price   
 * @param  $product 
 * @return 
 */
function custom_price_WPA111772($price, $product) {
    if (!is_user_logged_in()) return $price;

    //check if the product is in a category you want, let say shirts
    if( has_term( 'product', 'product_cat' ,$product->ID) ) {
        //check if the user has a role of dealer using a helper function, see bellow
        if (has_role_WPA111772('free')){
            //give user 100% off
            $price = $price * 0;
        }
    }
    return $price;
}

/**
 * has_role_WPA111772 
 *
 * function to check if a user has a specific role
 * 
 * @param  string  $role    role to check against 
 * @param  int  $user_id    user id
 * @return boolean
 */
function has_role_WPA111772($role = '',$user_id = null){
    if ( is_numeric( $user_id ) )
        $user = get_user_by( 'id',$user_id );
    else
        $user = wp_get_current_user();

    if ( empty( $user ) )
        return false;

    return in_array( $role, (array) $user->roles );
}

Filed Under: Genesis

Recent Posts

  • Migrating a WordPress Website Manually with Phpmyadmin, Search and Replace, DigitalOcean and Runcloud
  • How to add Total Price Calculation to your WooCommerce Product Page with AJAX
  • How to Install Roots Bedrock on a Digital Ocean Droplet running on Serverpilot via SSH
  • Linux SSH Useful Server Commands to Remember: Grep, Permissions and more.
  • How to Install Roots Sage 8.5.1 on a Digital Ocean Droplet running on Serverpilot via SSH

Find more content

Affiliate links
  • cheap shared hosting
  • low priced virtual servers only 5$
Affiliates
  • If someone buys a product then I get a commission.
  • You pay the same price using my affiliate links.
  • I can spend more time making newsletters and tutorials.
  • Thanks for visiting!

Connect with me

Recent Comments

  • Pedro Gómez on Migrating a WordPress Website Manually with Phpmyadmin, Search and Replace, DigitalOcean and Runcloud
  • Tim Delmeire on How to add Total Price Calculation to your WooCommerce Product Page with AJAX
  • Jaehyuk J Yang on How to add Total Price Calculation to your WooCommerce Product Page with AJAX
  • Alexander de Jong on A guide on customising the membership experience with Theme my Login (TML)
  • Judit Sarkany on A guide on customising the membership experience with Theme my Login (TML)

Recent Posts

  • Migrating a WordPress Website Manually with Phpmyadmin, Search and Replace, DigitalOcean and Runcloud
  • How to add Total Price Calculation to your WooCommerce Product Page with AJAX
  • How to Install Roots Bedrock on a Digital Ocean Droplet running on Serverpilot via SSH
  • Linux SSH Useful Server Commands to Remember: Grep, Permissions and more.
  • How to Install Roots Sage 8.5.1 on a Digital Ocean Droplet running on Serverpilot via SSH

Copyright © 2014 | Alexander de Jong | About | Contact | Privacy | Sitemap