The functions.php file in your WordPress theme is a powerful way you can manipulate your site to do whatever you want. Even if you aren't - and have no intention of becoming - a PHP developer.
Scanning the Internet for External Cloud Exposures via SSL Certs
Functions.php - It's Not Just For Developers
1. Functions.php
It’s Not Just for Developers
@10up http://10up.com Eric A Mann @ericmann
2. Who’s This Guy?
• Senior Web Engineer at 10up
• WordPress Core Contributor
• Plugin developer
• Self-taught PHP dev
@10up http://10up.com Eric A Mann @ericmann
3. And You Are?
• Use WordPress
• Want to tweak your site
• Know a little about code
• Don’t want to become a hard-core dev
@10up http://10up.com Eric A Mann @ericmann
4. So, What’s PHP?
• WP Core language
• Used everywhere
• Ugly
• Beautiful
@10up http://10up.com Eric A Mann @ericmann
5. WordPress Actions
“[H]ooks that the WordPress core launches
at specific points during execution, or when
specific events occur.” 1
• do_action()
• add_action()
@10up http://10up.com Eric A Mann @ericmann
6. WordPress Filters
“[H]ooks that WordPress launches to modify
text of various types before adding it to the
database or sending it to the browser
screen.” 1
• apply_filters()
• add_filter()
@10up http://10up.com Eric A Mann @ericmann
7. What’s Functions.php
• Core part of your theme
• Registers sidebars
• Sets up special functionality
• Lets you add plugin-like code
@10up http://10up.com Eric A Mann @ericmann
8. Code Snippets
function wps_first_post_class( $classes ) {
global $wp_query;
if( 0 === $wp_query->current_post ) {
$classes[] = 'first';
return $classes;
}
}
add_filter( 'post_class', 'wps_first_post_class' );
@10up http://10up.com Eric A Mann @ericmann
9. Manipulating WordPress
• Filters let you change output
• Actions let you add magic
• Both let you control the tool
@10up http://10up.com Eric A Mann @ericmann
10. Live Examples
1. Clean up <head> output
2. Rename Posts
3. Add Pinterest to user profile
@10up http://10up.com Eric A Mann @ericmann
11. Where to Find Snippets
• Official Support Forums
• WordPress Stack Exchange
• WPSnipp
• Google
@10up http://10up.com Eric A Mann @ericmann
12. How to Cheat
http://eam.me/code-snippet-plugin
@10up http://10up.com Eric A Mann @ericmann
Personal IntroductionSenior Web Engineer @ 10upPreviously worked on .NetprojectsFormer freelance PHP/JS developerContributed to every major WP release since 2.3Build plugins and other custom WP code for clientsLike many of you, I’m primarily self-taught in PHP.
PHP is one of the 5 core “languages” used in WordPress. The other four being JavaScript, MySQL, HTML, and CSS.It’s also one of the most widely used interpreted languages on the Internet because it’s preinstalled by a vast majority of web hosts.The syntax is a big ugly. PHP is open source, so there are contributions from literally hundreds of developers, all with different ideas of how to do things. Some functions are easier to understand than others. Some documentation just doesn’t make sense.But there’s also a huge community supporting it. So if something doesn’t make sense, you can probably find someone to explain it. The fact that it’s interpreted rather than compiled means we can get away with a lot of advanced hackery that wouldn’t fly elsewhere.Duck Typing lets us do some powerful and amazing things.
What exactly is an action in WordPress? Well, it’s code that does something.Think of it as an event notification system – whenever WordPress does X, trigger any events that are listening for X to happen.Actions can pass data to listeners so they can act accordingly.
What exactly is a filter in WordPress? Well, it’s code that filters something.Think of it as an approval system. Before doing something with data (i.e. Content), WordPress checks to see if anyone else cares about doing something to that data first and gives you the opportunity to manipulate things.Filters can pass contextual data along with the data being manipulated so you can make further decisions on it.
This chunk of code comes in two parts:A function that does something based on passed data and the global state of WordPressA filter that applies this function to a specific data check in WordPress
Remember, WordPress is a tool, albeit a rich one. You don’t have to fit the tool; you can manipulate the tool to fit you.
The <head> element includes a lot of content that you don’t need. If you’re a Mac user, you probably don’t need the Windows Live Writer manifest in the header. If you don’t use comments, you don’t need a link to the comments feed. You can get rid of this cruft easily.If you’re building a news site, using the word “Article” might make more sense than “Post.”If you’re in to Pinterest, you might want it added to your user profile.Live Examples