Post on 03-Jul-2015
description
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
@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.
@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
@michaelrmcneill#wcraleigh
THE WORDPRESS CODEX
@michaelrmcneill#wcraleigh
WHAT IS IT?The WordPress Manual written for all users, Designers and
Developers alike.
@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.
@michaelrmcneill#wcraleigh
THE WORDPRESS APIs
@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.”
@michaelrmcneill#wcraleigh
WHAT DO THE WORDPRESS APIs DO?
• They help you do things using tools that WordPress gives you.• That makes development EASIER!!
@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.
@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.
@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.
@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.
@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
@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.
@michaelrmcneill#wcraleigh
THE WORDPRESS PAGE TEMPLATE HIERARCHY
@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.
@michaelrmcneill#wcraleigh
@michaelrmcneill#wcraleigh
LET’S LOOK AT THE BASICS
@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.
@michaelrmcneill#wcraleigh
header.php
@michaelrmcneill#wcraleigh
footer.php
@michaelrmcneill#wcraleigh
single.php
@michaelrmcneill#wcraleigh
sidebar.php
@michaelrmcneill#wcraleigh
SINGLE POST RULES
1. single-post.php2. single.php3. index.php
@michaelrmcneill#wcraleigh
PAGE TEMPLATE RULES
1. Custom Template defined in WP Admin2. page-<slug>.php3. page-<id>.php4. page.php5. index.php
@michaelrmcneill#wcraleigh
HOME PAGE TEMPLATE RULES• front-page.php• Settings > Reading
• Static Page1. Follows the page template rules.
• Blog Page1. home.php2. index.php
@michaelrmcneill#wcraleigh
SINGLE CUSTOM POST RULES
1. single-<posttype>.php2. single.php3. index.php
@michaelrmcneill#wcraleigh
CUSTOM POST TYPE RULES
1. archive-<posttype>.php2. archive.php3. index.php
@michaelrmcneill#wcraleigh
CATEGORY PAGES
1. category-<slug>.php2. category-<id>.php3. category.php4. archive.php5. index.php
@michaelrmcneill#wcraleigh
TAG PAGES
1. tag-<slug>.php2. tag-<id>.php3. tag.php4. archive.php5. index.php
@michaelrmcneill#wcraleigh
AUTHOR PAGES
1. author-<nicename>.php2. author-<id>.php3. author.php4. archive.php5. index.php
@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
@michaelrmcneill#wcraleigh
CODEX ARTICLE
INTERACTIVE WEBSITE!
http://codex.wordpress.org/Template_Hierarchy
http://wphierarchy.com/
@michaelrmcneill#wcraleigh
WORDPRESS DATA
@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.
@michaelrmcneill#wcraleigh
TYPES OF WORDPRESS CONTENT
• posts• pages• custom post types• attachments• links• menu items
@michaelrmcneill#wcraleigh
ASSOCIATED DATA (POSTMETA)
• categories• tags• custom taxonomies and terms• post metadata
@michaelrmcneill#wcraleigh
OTHER TYPES OF CONTENT
• widgets• options• users• sites (for multisite)
@michaelrmcneill#wcraleigh
@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.
@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.
@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)
@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)
@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)
@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
@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)
@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.
@michaelrmcneill#wcraleigh
$wpdb
Methods in the wpdb() class should not be called directly. You should use the global $wpdb object instead.
@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!
@michaelrmcneill#wcraleigh
TRICKS OF THE TRADE
@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
@michaelrmcneill#wcraleigh
QUESTIONS?