alexanderdejong.com

Wordpress Developer

  • Home
  • Blog
  • Projects
  • Contact
You are here: Home / 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