Functions.php - It's Not Just For Developers

637 views

Published on

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.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
637
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • 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
  • Functions.php - It's Not Just For Developers

    1. 1. Functions.php It’s Not Just for Developers@10up http://10up.com Eric A Mann @ericmann
    2. 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. 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. 4. So, What’s PHP?• WP Core language• Used everywhere• Ugly• Beautiful@10up http://10up.com Eric A Mann @ericmann
    5. 5. WordPress Actions“[H]ooks that the WordPress core launchesat specific points during execution, or whenspecific events occur.” 1• do_action()• add_action()@10up http://10up.com Eric A Mann @ericmann
    6. 6. WordPress Filters“[H]ooks that WordPress launches to modifytext of various types before adding it to thedatabase or sending it to the browserscreen.” 1• apply_filters()• add_filter()@10up http://10up.com Eric A Mann @ericmann
    7. 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. 8. Code Snippetsfunction 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. 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. 10. Live Examples1. Clean up <head> output2. Rename Posts3. Add Pinterest to user profile@10up http://10up.com Eric A Mann @ericmann
    11. 11. Where to Find Snippets• Official Support Forums• WordPress Stack Exchange• WPSnipp• Google@10up http://10up.com Eric A Mann @ericmann
    12. 12. How to Cheat http://eam.me/code-snippet-plugin@10up http://10up.com Eric A Mann @ericmann
    13. 13. Questions?@10up http://10up.com Eric A Mann @ericmann

    ×