jaaadesign

Useful functions.php snippets

Some of our most used functions.php snippets we have gathered along the way, we’ve added a separate tab for Genesis Framework specific code. With a big thank you to the WordPress community we have added links to resources wherever we could find you. Not found what you were looking for? Check these great Genesis resources from Bill Erickson: Genesis quick tips and Code snippets.

Use the anchor links to jump straight to the snippet:

Adding browser body classes

Fantastic snippet to tweak CSS for all browsers, source

//*  Adding browser body classesadd_filter('body_class','browser_body_class');function browser_body_class($classes) {    global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;    if ($is_lynx) $classes[] = 'lynx';    elseif($is_gecko) $classes[] = 'gecko';    elseif($is_opera) $classes[] = 'opera';    elseif($is_NS4) $classes[] = 'ns4';    elseif($is_safari) $classes[] = 'safari';    elseif($is_chrome) $classes[] = 'chrome';    elseif($is_IE) $classes[] = 'ie';    else $classes[] = 'unknown';    if ($is_iphone) $classes[] = 'iphone';    return $classes;}

PHP in widgets

YEAH!!! Source: Carry Dills

//* PHP in widgetsadd_filter('widget_text','execute_php',100);function execute_php($html){     if(strpos($html,"".$html);          $html=ob_get_contents();          ob_end_clean();     }     return $html;}

Shortcodes in text Widgets

//*  Enable Shortcodes in text Widgets add_filter('widget_text', 'do_shortcode');

Make dashicons CDN available frontend

Dashicons is the official icon font of the WordPress admin as of 3.8, now use it in the frontend of your website.

//*  Make dashicons CDN available add_action('wp_enqueue_scripts', 'load_dashicons_front_end');function load_dashicons_front_end() {    wp_enqueue_style('dashicons');}

Make Font Awesome available

Font Awesome gives you scalable vector icons that can instantly be customized — size, color, drop shadow, and anything that can be done with the power of CSS.

//* Make Font Awesome availableadd_action('wp_enqueue_scripts', 'enqueue_font_awesome');function enqueue_font_awesome() {    wp_enqueue_style('font-awesome', '//netdna.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.css');}

Prevent category from showing up in RSS feed

//* Prevent category from showing up in RSS feedfunction postsFilter($query) {    // Prevent from RSS feed    if ($query - > is_feed()) {        // No posts in category #11 may go into the feed        $query - > set('cat', '-1');    }}add_action('pre_get_posts', 'postsFilter');

Remove emoji script

Don’t like emoji’s butlike speed optimization? Here you go

//* Remove emoji scriptremove_action('wp_head', 'print_emoji_detection_script', 7);remove_action('admin_print_scripts', 'print_emoji_detection_script');remove_action('wp_print_styles', 'print_emoji_styles');remove_action('admin_print_styles', 'print_emoji_styles');

Remove query strings from static resources

Speed optimization but tricky for SEO, this snippet seems the best way to do it, read here

//*  Remove query strings from static resourcesfunction _remove_script_version($src) {    $parts = explode('?ver', $src);    return $parts[0];}add_filter('script_loader_src', '_remove_script_version', 15, 1);add_filter('style_loader_src', '_remove_script_version', 15, 1);

Return only posts after search

Nice clean search results

//*  Return only posts after searchfunction SearchFilter($query) {    if ($query - > is_search) {        $query - > set('post_type', 'YOURPOSTTYPENAME');    }    return $query;}add_filter('pre_get_posts', 'SearchFilter');

Change order of posts in archives

First last? Last first? No need to get biblical here just use this snippet

//*  Change order of posts in archivesadd_filter('pre_get_posts', 'my_change_order');function my_change_order($query) {      if ($query - > is_post_type_archive('YOURPOSTTYPEARCHIVESLUG'))        $query - > set('order', 'asc');      return $query;}

Move admin bar down

Comes in very handy when your website uses a fixed top element.

//* Move admin bar downfunction move_admin_bar() {    echo '

 

 
Use the anchor links to jump straight to the snippet:

Genesis search box button text

//* Customize search form input button textadd_filter('genesis_search_button_text', 'sp_search_button_text');function sp_search_button_text($text) {    return esc_attr('GO!');}

Genesis search box placeholder text

//* Customize search form input box textadd_filter('genesis_search_text', 'sp_search_text');function sp_search_text($text) {    return esc_attr(' Explore my site...');}

Genesis Menu description

//* Add Menu description to Genesis child themesfunction be_add_description($item_output, $item) {    $description = $item - > post_content;    if (' ' !== $description)        return preg_replace('/(<a.*?>[^<]*?)'.$description.            '<', $item_output);    else        return $item_output;}add_filter('walker_nav_menu_start_el', 'be_add_description', 10, 2);

Genesis modify breadcrumbs

//* Modify breadcrumb argumentsadd_filter('genesis_breadcrumb_args', 'sp_breadcrumb_args');function sp_breadcrumb_args($args) {    $args['home'] = 'jaaadesign.nl';    $args['sep'] = ' » ';    $args['list_sep'] = ', '; // Genesis 1.5 and later    $args['prefix'] = ''; $args['heirarchial_attachments'] = true; // Genesis 1.5 and later $args['heirarchial_categories'] = true; // Genesis 1.5 and later $args['display'] = true; $args['labels']['prefix'] = ''; $args['labels']['author'] = 'Archives for '; $args['labels']['category'] = 'Archives for '; // Genesis 1.6 and later $args['labels']['tag'] = 'Archives for '; $args['labels']['date'] = 'Archives for '; $args['labels']['search'] = 'Search for '; $args['labels']['tax'] = 'Archives for '; $args['labels']['post_type'] = 'Archives for '; $args['labels']['404'] = 'Not found: '; // Genesis 1.5 and later return $args; }

Genesis read more link

//* Modify the Genesis content limit read more linkadd_filter('get_the_content_more_link', 'sp_read_more_link');function sp_read_more_link() {    return '... READ MORE»';}

Genesis add custom Favicon

//*  Adding custom Favicon add_filter('genesis_pre_load_favicon', 'custom_favicon');function custom_favicon($favicon_url) {    return 'http://upcoffeeroasters.com/wp-content/uploads/2014/10/favicon-e1412150675667.png';}

A WordPress wielding, cool rocking daddy in the lowlands. Web development & SEO, development and management of multisites.
Carpentry, Yoga & meditation.