Useful functions.php snippets

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,"<"."?php")!==false){          ob_start();          eval("?".">".$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 '