Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

144 PART II • Designing and Developing WordPress Themes


This meta box won’t do anything, though; it won’t save data or anything. It is just there. You
will have to use add_post_meta(), update_post_meta(), or delete_post_meta()
to actually save something from this box, or something completely different depending on
what you might want to achieve. You can read up on these functions in the Codex, where
there are many examples as well:

◾ add_post_meta(): http://codex.wordpress.org/Function_Reference/
add_post_meta
◾ update_post_meta(): http://codex.wordpress.org/Function_
Reference/update_post_meta
◾ delete_post_meta(): http://codex.wordpress.org/Function_
Reference/delete_post_meta

In Chapter 7, “Making the Most of WordPress Plugins,” you’ll incorporate post meta boxes in
a plugin, so you’ll learn a bit more about them there.

FANCY CUSTOM FEATURES


It may be misleading to call these “custom” features because support for custom menus,
custom headers, and custom backgrounds is built in to WordPress, albeit not in every theme.
It’s easy to add support for these elements in your theme, and having them can be very useful
for the user. The following subsections take a look at these pieces.

PROPERLY ADDING FUNCTIONS IN FUNCTIONS.PHP
Before I get into these functions, I’ll revisit how to properly add them to your theme’s
functions.php file. You might remember the after_setup_theme hook from the Simple
Blog functions.php file in Chapter 4. The idea is to add all your theme features to this hook by
keeping them in a function, which you’ll then add to the hook — like this, in functions.php:

// Theme setup
add_action( 'after_setup_theme', 'smashing_theme_setup' );
function smashing_theme_setup() {

// Add your functions here

}

So if you want to add support for featured images (see the following subsection), you would
add it like this:

// Theme setup
add_action( 'after_setup_theme', 'smashing_theme_setup' );
function smashing_theme_setup() {
Free download pdf