Extending WordPress with Functions.php

Some WordPress themes contain one PHP file that isn’t a template, named functions.
php. Experienced theme designers place important parts of their code here, and
then call that code from their template files when they need it. WordPress gurus also
use the functions.php file to add other features, like new shortcodes and widgets,
so they don’t need to create a complete plug-in.
All these tasks are advanced operations, best kept to WordPress gurus. However,
even people with no PHP experience can use the functions.php file to unlock extra
WordPress features. Usually, you do that by copying a few lines of code you read
about online (or in a book), and pasting them into the functions.php file in the
theme editor.
For example, in Chapter 10, you used WordPress’s auto-embed feature to turn certain
types of website addresses into embedded objects, like Flickr slideshows and
YouTube videos. Normally, WordPress turns off this handy feature in widget areas,
like the sidebar. But those in the know can enable shortcodes and embeds in widget
areas with just a couple of lines in the functions.php file. Figure 13-18 shows the result.
The functions.php file works a bit differently from the template files you’ve learned
about so far. The key quirk is that the functions.php file in your child theme extends
the functions.php file in your parent theme; it doesn’t replace it. For that reason, you
can’t simply copy the functions.php file from your parent theme to your child theme.
You need to create a new, empty text file on your computer, change its name to
functions.php, and then upload that new functions.php file to your child theme folder.
If you use the Orbisius Child Theme Creator plug-in, your job is easy. Follow the
process described on page 490 to create a new, blank functions.php file, but don’t
copy any content to that file.

WARNING If you accidentally copy the functions.php file from your parent theme to your child theme,
your site will stop working. That’s because functions.php is full of important routines that Twenty Twelve uses,
and having two copies of this code is enough to blow WordPress’s mind. The only fix (if you disregard this warning
and put the full functions.php file in your child theme folder) is to log in to your site with an FTP program and
delete the copied functions.php file.
Once you add the blank functions.php file to your site, you can edit it in the Edit
Theme page, as you can any other theme file. To enable widget-embedding, as
shown in Figure 13-18, you need to add these lines:

add_filter( ‘widget_text’, array( $wp_embed, ‘run_shortcode’ ), 8);
add_filter( ‘widget_text’, array( $wp_embed, ‘autoembed’), 8 );

Now you can put an auto-embed URL in a Text widget, just as you can in a post

Extending WordPress with Functions.php

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s