55
THINGS YOU SHOULD KNOW ABOUT WORDPRESS (but were always afraid to ask) Power Users Track WordCamp Raleigh 2014 Saturday, November 8th, 2014 Michael McNeill - @michaelrmcneill #wcraleigh

Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

Embed Size (px)

DESCRIPTION

This talk will cover a few key “Aha” moments that you should have about the way WordPress works. We’ll talk about things like the template hierarchy, where WordPress content is stored, how posts and pages and custom post types are represented in the database, what folks are talking about when they talk about hooks and filters, and just generally review the “behind the scenes” mechanics of how WordPress works. We’ll also touch on a few “tricks of the trade” that you might not realize are out there, mainly version control, development environments, and Vagrant.

Citation preview

Page 1: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

THINGS YOU SHOULD KNOW ABOUT WORDPRESS

(but were always afraid to ask)

Power Users TrackWordCamp Raleigh 2014

Saturday, November 8th, 2014

Michael McNeill - @michaelrmcneill #wcraleigh

Page 2: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

ABOUT ME• Developer and Support Engineer in ITS Web Services at the University of

North Carolina at Chapel Hill, where I help to manage two HUGE multisite networks that operate a total of almost 8,500+ sites.

• Owner and Developer at MRMcDEV, Inc. my personal consulting company.• Co-Curator (Executive Director) of TEDxUNC, a non-profit organization

dedicated to bringing “Ideas Worth Spreading” to Carolina.• I’ve been using WordPress for over 5 years now, and I use it for almost all my

client projects.• I’ve worked on exciting and wide ranging projects, such as Black Enterprise

Magazine, Mackay Communications, WiredHoods, smallbiztechnology.com, ALOHA, and MAXI Promotion and Records. I’ve also contracted for DRS Technologies, the United States Department of the Defense, and numerous other companies.

Page 3: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

A QUICK ROADMAP…1. The WordPress APIs - What they are, how to find them, and

how to use them.2. The WordPress Template Hierarchy - What it is, which file

generates a certain type of page, and how to use it to your advantage.

3. The WordPress Database - What is stored there, why it is stored there, how is it stored there, and how should you properly use the data inside it.

4. A few “tricks of the trade” - Version Control and Vagrant

Page 4: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

THE WORDPRESS CODEX

Page 5: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

BOOKMARK IT!http://codex.wordpress.org

Page 6: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

WHAT IS IT?The WordPress Manual written for all users, Designers and

Developers alike.

Page 7: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

USE IT & IMPROVE IT.When you first have a question, go to the Codex first.

The Codex is a living document, and anyone with a WordPress.org account can edit it and improve it. If you see something out of date, or flat out wrong, update it.

Page 8: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

THE WORDPRESS APIs

Page 9: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

WHAT’S AN API?

• APIs are Application Programming Interfaces.• Think about it like this, APIs are basically a contract

between two things stating: "If you tell me this, I will do this.”

Page 10: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

WHAT DO THE WORDPRESS APIs DO?

• They help you do things using tools that WordPress gives you.• That makes development EASIER!!

Page 11: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

HOW DO I FIND THEM?

• Identify what you are trying to do.• Then go to the ___________ and search for it.• If that doesn’t work, try using Google, just

remember to cross-check against what the Codex says.

Page 12: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

HOW DO I USE THEM?• The format is basically the same across all function

reference articles. • There are 3 primary sections.

1. The description of the function.2. The parameters the function wants/needs.3. What values, if any, the function returns.

Page 13: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

WORDPRESS PAGE LIFE CYCLE• The WordPress page life cycle is a combination of

the events that take place from when a browser requests a page to when the server returns the rendered page to the browser.

• That sounds simple, but there are a lot of things going on that get you the end result.

Page 15: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

WORDPRESS HOOKS• Hooks are extremely important to WordPress

developers. • They enable us to literally hook into parts of the

WordPress page life cycle to retrieve, insert, and modify data, and they allow us to take certain actions behind the scenes, before a user sees what is occurring.

Page 16: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

WORDPRESS HOOKS• Two Classifications

• Actions• Actions are triggered by specific events that take place in WordPress,

such as publishing a post, activating a plugin, or loading an admin screen.

• For a comprehensive list of actions, check this Codex article out: http://codex.wordpress.org/Plugin_API/Action_Reference

• Filters• Filters are functions that WordPress passes data through, that are

primarily responsible for intercepting, managing, and returning data, before rendering or saving that data.

• For a pretty comprehensive list of filters, check this Codex article out: http://codex.wordpress.org/Plugin_API/Filter_Reference

Page 17: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

WORDPRESS HOOKS• I’m sure you have the question, when should I use which

hook?• Use actions when you want to add something to the

existing page such as stylesheets, JavaScript, or send an email when an event has happened.

• Use filters when you want to manipulate data coming out of the database prior to going to the browser, or coming from the browser prior to going into the database.

Page 18: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

THE WORDPRESS PAGE TEMPLATE HIERARCHY

Page 19: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

WHAT IS THE TEMPLATE HIERARCHY?

WordPress templates fit together like the pieces of a puzzle to generate the pages on your WordPress site. Some templates (the header and footer templates for example) are used on almost all pages, while others are used only under specific conditions. The template hierarchy decides what template file or files WordPress will use to display a certain type of page.

Page 20: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

Page 21: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

LET’S LOOK AT THE BASICS

Page 22: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

THE NEEDS OF A THEME

• To have a functioning, bare minimum theme you need two things.• style.css - A stylesheet.• index.php - An index file that will render the

output of the page.

Page 23: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

header.php

Page 24: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

footer.php

Page 25: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

single.php

Page 26: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

sidebar.php

Page 27: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

SINGLE POST RULES

1. single-post.php2. single.php3. index.php

Page 28: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

PAGE TEMPLATE RULES

1. Custom Template defined in WP Admin2. page-<slug>.php3. page-<id>.php4. page.php5. index.php

Page 29: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

HOME PAGE TEMPLATE RULES• front-page.php• Settings > Reading

• Static Page1. Follows the page template rules.

• Blog Page1. home.php2. index.php

Page 30: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

SINGLE CUSTOM POST RULES

1. single-<posttype>.php2. single.php3. index.php

Page 31: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

CUSTOM POST TYPE RULES

1. archive-<posttype>.php2. archive.php3. index.php

Page 32: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

CATEGORY PAGES

1. category-<slug>.php2. category-<id>.php3. category.php4. archive.php5. index.php

Page 33: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

TAG PAGES

1. tag-<slug>.php2. tag-<id>.php3. tag.php4. archive.php5. index.php

Page 34: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

AUTHOR PAGES

1. author-<nicename>.php2. author-<id>.php3. author.php4. archive.php5. index.php

Page 35: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

ATTACHMENT RULES

1. MIME-type.php (e.x. text.php, video.php, image.php)

2. attachment.php3. single_attachment.php4. single.php5. index.php

Page 36: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

CODEX ARTICLE

INTERACTIVE WEBSITE!

http://codex.wordpress.org/Template_Hierarchy

http://wphierarchy.com/

Page 37: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

WORDPRESS DATA

Page 38: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

WORDPRESS DATA• A WordPress website consists of three main

elements:• The WordPress installation itself• The contents of the wp-content directory which

includes the themes, plugins and uploads• The database, where all the content is stored.

Page 39: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

TYPES OF WORDPRESS CONTENT

• posts• pages• custom post types• attachments• links• menu items

Page 40: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

ASSOCIATED DATA (POSTMETA)

• categories• tags• custom taxonomies and terms• post metadata

Page 41: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

OTHER TYPES OF CONTENT

• widgets• options• users• sites (for multisite)

Page 42: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

Page 43: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

A FEW NOTES…

• In the next few slides, I’m using the wp_ prefix by default. You can change this (and you might have), but the concepts are the same.

• A multisite installation will have some extra tables. I haven't included those here as that's outside the scope of this presentation.

Page 44: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

THE WORDPRESS DATABASE STRUCTURE

Most of the tables in the WordPress database are linked to one or more other tables via a specific field. This field is generally a unique ID for each record such as a post_id.

Page 45: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

THE WORDPRESS DATABASE STRUCTURE

TABLE DATA STORED LINKED TO

wp_posts Posts, pages, attachments, revisions and menu items

wp_postmeta (using post_id)

wp_term_relationships(using post_id)

wp_postmeta Post metadata wp_posts(using post_id)

Page 46: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

THE WORDPRESS DATABASE STRUCTURE

TABLE DATA STORED LINKED TO

wp_comments Commentswp_posts

(using post_id)wp_commentmeta(using comment_id)

wp_commentmeta Comment metadata wp_comments(using comment_id)

Page 47: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

THE WORDPRESS DATABASE STRUCTURE

TABLE DATA STORED LINKED TO

wp_users Userswp_posts

(using post_author)wp_usermeta (using user_id)

wp_usermeta Metadata for each user wp_users (using user_id)

Page 48: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

THE WORDPRESS DATABASE STRUCTURE

TABLE DATA STORED LINKED TO

wp_links(DEPRECATED!)

Information related to Links

wp_term_relationships (using link_id)

wp_optionsSite settings and options

(set via the Settings screens and via plugins and themes) as well as widgets

None

Page 49: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

THE WORDPRESS DATABASE STRUCTURE

TABLE DATA STORED LINKED TO

wp_term_relationships Relationships between posts and taxonomies

wp_posts (using post_id)

wp_term_taxonomy (using term_taxonomy_id)

wp_term_taxonomy Taxonomies (including categories and tags)

wp_term_relationships (using term_taxonomy_id)

wp_termsYour categories and tags

and the terms assigned to custom taxonomies

wp_term_taxonomy (using term_id)

Page 50: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

HOW TO USE THE WORDPRESS DB?

WordPress defines a class called wpdb, which contains a set of functions used to interact with a database. Its purpose is to provide an easy to use interface with the WordPress database.

Page 51: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

$wpdb

Methods in the wpdb() class should not be called directly. You should use the global $wpdb object instead.

Page 52: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

A LARGE WARNING

Any function that executes SQL queries, can be vulnerable to SQL injection attacks. To prevent that, you should escape all SQL. Make sure to review the ______ to double-check if the function you plan to use escapes SQL for you or leaves it un-escaped!

Page 53: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

TRICKS OF THE TRADE

Page 54: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

VAGRANT AND VVVhttp://vagrantup.com

https://github.com/Varying-Vagrant-Vagrants/VVV

A great tutorial: http://tangrufus.com/start-wordpress-development-varying-vagrant-

vagrants/

GIT AND GITHUBGreat intro guide and cheat sheet: http://

rogerdudler.github.io/git-guide/GitHub: https://github.com

Deploy (Automated Deployment): https://www.deployhq.com

Page 55: Things you should know about WordPress (but were always too afraid to ask): WordCamp Raleigh 2014

@michaelrmcneill#wcraleigh

QUESTIONS?