WordPress Theme Development Cheat Sheet

Below are some snippets we use in just about every custom WordPress theme. We’ve posted this theme on here for personal reference and hopefully to help some new developers out. The code below can be put in your theme’s function.php  file.

Add Featured Image to WordPress Pages and/or Posts

if ( function_exists( 'add_theme_support' ) ) {
  add_theme_support( 'post-thumbnails' ,array( 'page' , 'post' , 'your_custom_post_type' ) );
}

Add Excerpt to WordPress Pages and/or Posts

add_action('init', 'yourtheme_add_post_type_support',0);
  function yourtheme_add_post_type_support() {
    add_post_type_support('page','excerpt');
    add_post_type_support('post','excerpt');
  }

Add Custom WordPress Menu(s) Navigation

// add custom menu to be used as primary menu
register_nav_menu( 'main-navigation', 'Main Navigation' );

// to print in theme:
# <? wp_nav_menu('theme_location=main-navigation')?>
// or if you only have one menu registered: simply:
# <? wp_nav_menu(); ?>

Add Custom Sidebar(s) WordPress

register_sidebar(array('name' => 'Footer bar'));

// to print in theme, in your sidebar.php or other file
dynamic_sidebar('Footer bar');

Add stylesheet to TinyMCE

This will add a custom style sheet in WordPress text editor. This gives users a accurate idea of what their post will look like. We use style_tinymce.css instead of style.css as the TinyMCE editor is the <body> of a HTML document (instead of .entry-content), and requires things like background, padding and margins to be more like your content then your site’s body.

add_filter( 'mce_css', 'tdav_css' );

if ( ! function_exists('tdav_css') ) {
  function tdav_css($wp) {
    $wp .= ',' . get_bloginfo('stylesheet_directory').'/style_tinymce.css';
    return $wp;
  }
}

Add favicon to WordPress Admin

For whatever reason, WordPress’s admin isn’t bundled with a favicon for the admin. Below will add a fav icon to your WordPress dashboard. (We used the favicon from wordpress.org, and keep the wp-admin favicon in my theme’s /images/ folder)

// add favicon to admin
add_action('admin_head','add_admin_favicon');
function add_admin_favicon(){
  ?>
  <link rel='shortcut icon' href='<? bloginfo('stylesheet_directory') ?>/images/wp_favicon.ico' />
  <?
}

WordPress Shortcode in Widgets

add_filter('widget_text', 'do_shortcode');

WordPress Get Featured Image

$img = wp_get_attachment_image_src( get_post_thumbnail_id( get_the_ID() ), 'medium' );

// to output:
echo "<img src='{$img[0]}' ... />";

Remove items from WordPress Admin Navigation

With below you can remove (unset) admin menu items, like removing the Links in themes that don’t have links.

add_action('admin_init', 'edit_adminmenu');
function edit_adminmenu() {
  global $menu;
  #unset($menu[2]); //dashboard
  #unset($menu[4]); //sep
  #unset($menu[5]); //posts
  #unset($menu[10]); //media
  unset($menu[15]); //links
  #unset($menu[20]); //pages
  #unset($menu[25]); //comments
  #unset($menu[59]); //sep
  #unset($menu[60]); //appearance
  #unset($menu[65]); //plugins
  #unset($menu[70]); //users
  unset($menu[75]); //tools
  #unset($menu[80]); //settings
  #unset($menu[99]); //sep
}

WordPress Redirect Page Shortcode

This function looks like a shortcode but doesn’t work within the Shortcode API.. To use, within a page add: [redirect to=123] to redirect to the page with the ID 123.

add_action('template_redirect','redirect_shortcode');
function redirect_shortcode( ) {
    global $post;
    //see if shortcode is in content
    $pos = strpos(strtolower($post->post_content), '[redirect');
    if ($pos !== false) {
      $pull_out_arrg = preg_match_all( '|to=.*?(.*?).*?]|i', $post->post_content, $matches );
      $matches = intval( str_replace(array('to=',']','"',"'"),'',$matches[0][0]) );
      //redirect
      if (!empty($matches) && $matches != 0)
        header('Location: '.get_permalink($matches));
    }
}

The Following are Page Templates

The snippets below are to become their own page, and not added to functions.php

WordPress Redirect to Parent Page

redirect-to-parent.php can be used when you’d like the first child page to redirect to the parent page. Useful for less-web-savvy visitors who don’t think to click the parent and instead head straight for the sub-page pop-down/out.

<?
/**
 * Template Name: Redirect to Parent
 **/

global $post;
if (!empty($post->post_parent)) {
  wp_redirect(get_permalink($post->post_parent));
  die;
  exit;
} else {
  include('page.php');
}

?>

WordPress Redirect to First Child Page

We’ll do the reverse of above, and have a parent page redirect to a child page.

<?
/**
 * Template Name: Redirect to Child
 **/

global $post;
$pagekids = get_pages("child_of=".$post->ID."&sort_column=menu_order");
if ($pagekids) {
  $firstchild = $pagekids[0];
  wp_redirect(get_permalink($firstchild->ID));
} else {
  include('page.php');
}
?>

 

Have some references of your own, or an easier way to do one of the above? Post a comment below.