A Developer's Guide to WordPress

Josh Lee

  • Developer since ~2006
  • WordPress developer since ~2010
  • Have built scores of WordPress websites of all sizes
  • Organizer for the Burlington WordPress Meetup

Find Me

WordPress powers 20% of the internet

Accessibility & Extensibility

Core values

A typical Wordpress site

The Admin

Posts & Pages

built-in Post-Types:

  • Post
  • Page
  • Attachment
  • Revision
  • Navigation Menu
  • Custom CSS
  • Changesets
  • User Data Request

Custom Post types let you make your own, e.g.:

  • Portfolio Item
  • Recipe
  • Book
/**
 * Register a custom post type called "book".
 *
 * @see get_post_type_labels() for label keys.
 */
function register_my_book_type() {
    $labels = array(
        'name'                  => 'Books'
        'singular_name'         => 'Book'
        'menu_name'             => 'Books'
        ...
    );
 
    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'publicly_queryable' => true,
        'show_ui'            => true,
        'show_in_menu'       => true,
        'query_var'          => true,
        'rewrite'            => array( 'slug' => 'book' ),
        'capability_type'    => 'post',
        'has_archive'        => true,
        'hierarchical'       => false,
        'menu_position'      => null,
        'supports'           => array( 'title', 'editor', ... ),
    );
 
    register_post_type( 'book', $args );
}
 
add_action( 'init', 'register_my_book_type' );

Categories & tags

Categories & tags are examples of taxonomies

  • You can make your own taxonomies
  • Taxonomies can be hierarchical (categories) or not (tags)
  • Any taxonomy can be associated with any post type
  • Specific items within a taxonomy are called terms

Sidebars and widgets

Sidebars are just buckets of widgets

The Wordpress filesystem

Plugins vs. Themes

Themes

Themes must contain an index.php and a style.css

 

Generally they should be used to provide style and not functionality, but any PHP and hooks are available for developers to use.

Plugins

Plugins have access to the same action hooks as themes, and typically contain functionality changes which might include default styles.

 

Plugins can also run code upon activation/deactivation

Wait, Just an index.php?

Example Route

  • Request: /books/my-book
  • Single Custom Post Request:
    1. single-book-my-book.php
    2. single-book.php
    3. single.php
    4. singular.php
    5. index.php

If a more specific template does not exist, WordPress will fallback

"The Loop"

<?php 
if ( have_posts() ) {
	while ( have_posts() ) {
		the_post(); 
		//
		// Post Content here
		//
	} // end while
} // end if
?>

the_title

the_permalink

the_content

 

get_header

get_footer

get_sidebar

wp_title

single_post_title

Custom Fields

Custom fields

  • Custom fields allow you to let users attach metadata to posts of any type.
  • You can create your own interface or...
  • Many plugins exist for easily creating custom fields:

Data

Database Tables

  • posts
  • postmeta
  • users
  • usermeta
  • comments
  • commentmeta
  • terms
  • termmeta
  • term_relationships
  • term_taxonomy
  • options

Posts

One table to rule them all

In WordPress, Everything is a Post*

* almost

Fetching Post data

  • "The Loop"
  • get_posts()
  • WP_Query class

So what about metadata?

Post Meta

Key-value pairs containing data for posts. Can be managed by plugins or sometimes directly by administrators.

User Meta

The same, but for users instead of posts.

Caution:

Either might contain serialized data

Hooks & Filters: MAXIMUM Customization

You can attach your own functions to action hooks

WordPress will run your code at the appropriate time

(e.g. upon initialization of the plugin)

Filters are like actions

But the callback takes and returns an argument

Debugging

Debugging tools

CAUTION

Serialized data

Absolute URLS

The Future

LEARN Javascript DEEPLY

- Matt Mullenweg

Project Gutenberg

Find me online

A Developer's Guide to WordPress (2018)

By Josh Lee

A Developer's Guide to WordPress (2018)

  • 1,093