29
Getting to The Loop working with themes is commonplace, but how WP gets there is still seen as magic. it doesn’t need to be that hard. Here’s how it gets there...

Getting to The Loop - London Wordpress Meetup July 28th

Embed Size (px)

DESCRIPTION

This is a slightly modified version of the talk I gave at the London Wordpress meetup. I'm putting it up here a) for people who were taking notes last night and b) to shame me into putting a polished version up here for people who couldn't make it. thanks for @folletto for providing the graphics that split up the endless code snippets.

Citation preview

Page 1: Getting to The Loop - London Wordpress Meetup  July 28th

Getting to The Loop

working with themes is commonplace, but how WP gets there is still seen as magic.

it doesn’t need to be that hard. Here’s how it gets there...

Page 2: Getting to The Loop - London Wordpress Meetup  July 28th

Load

Plugins

DB

Template

The Loop

Page 3: Getting to The Loop - London Wordpress Meetup  July 28th

Load in files for a bootstrap phase

set up Plugins

turn the request URL into a DB query

choose a Template

start The Loop

Page 4: Getting to The Loop - London Wordpress Meetup  July 28th

Load

Page 5: Getting to The Loop - London Wordpress Meetup  July 28th

important files for this step:

• index.php

• blog-header.php

• wp-load.php

• wp-settings.php

Page 6: Getting to The Loop - London Wordpress Meetup  July 28th

<?php/** * Front to the WordPress application. This file doesn't do anything, but loads * wp-blog-header.php which does and tells WordPress to load the theme. */define('WP_USE_THEMES', true);

/** Loads the WordPress Environment and Template */require('./wp-blog-header.php');?>

index.php

Page 7: Getting to The Loop - London Wordpress Meetup  July 28th

<?php/** * Loads the WordPress environment and template. */

require_once( dirname(__FILE__) . '/wp-load.php' );

wp();

require_once( ABSPATH . WPINC . '/template-loader.php' );

}

?>

blog-header.php

Page 8: Getting to The Loop - London Wordpress Meetup  July 28th

<?php

/** Define ABSPATH as this files directory */define( 'ABSPATH', dirname(__FILE__) . '/' );

if ( file_exists( ABSPATH . 'wp-config.php') ) {

require_once( ABSPATH . 'wp-config.php' );

} else {

wp_die("There doesn't seem to be a wp-config.php file. I need this before we can get started. );

}

?>

wp-load.php

Page 9: Getting to The Loop - London Wordpress Meetup  July 28th

<?php

/** Define ABSPATH as this files directory */define( 'ABSPATH', dirname(__FILE__) . '/' );

if ( file_exists( ABSPATH . 'wp-config.php') ) {

require_once( ABSPATH . 'wp-config.php' );

} else {

wp_die("There doesn't seem to be a wp-config.php file. I need this before we can get started. );

}

?>

wp-load.php

Page 10: Getting to The Loop - London Wordpress Meetup  July 28th

<?php///** The name of the database for WordPress */define('DB_NAME', 'database_name_here');

/** MySQL database username */define('DB_USER', 'username_here');

/** MySQL database password */define('DB_PASSWORD', 'password_here');

/** Sets up WordPress vars and included files. */require_once(ABSPATH . 'wp-settings.php');

wp-config.php

Page 11: Getting to The Loop - London Wordpress Meetup  July 28th

<?php///** The name of the database for WordPress */define('DB_NAME', 'database_name_here');

/** MySQL database username */define('DB_USER', 'username_here');

/** MySQL database password */define('DB_PASSWORD', 'password_here');

/** Sets up WordPress vars and included files. */require_once(ABSPATH . 'wp-settings.php');

wp-config.php

Page 12: Getting to The Loop - London Wordpress Meetup  July 28th

<?php

// Load early WordPress files.require( ABSPATH . WPINC . '/compat.php' );require( ABSPATH . WPINC . '/functions.php' );require( ABSPATH . WPINC . '/class-wp.php' );require( ABSPATH . WPINC . '/class-wp-error.php' );require( ABSPATH . WPINC . '/plugin.php' );// Load most of WordPress.require( ABSPATH . WPINC . '/class-wp-ajax-response.php' );require( ABSPATH . WPINC . '/formatting.php' );require( ABSPATH . WPINC . '/query.php' );

wp-settings.php

Page 13: Getting to The Loop - London Wordpress Meetup  July 28th

<?php

// Make taxonomies and posts available to plugins and themes.create_initial_taxonomies();create_initial_post_types();

// Load active plugins.foreach ( wp_get_active_and_valid_plugins() as $plugin ) { include_once( $plugin ); }

do_action( 'plugins_loaded' );do_action( 'setup_theme' );do_action( 'after_setup_theme' );do_action( 'wp_loaded' );?>

wp-settings.php

Page 14: Getting to The Loop - London Wordpress Meetup  July 28th

Plugins

• change url handling, templates, queries

• work based on hooks

• add_action()

• apply_filter()

Page 15: Getting to The Loop - London Wordpress Meetup  July 28th

plugins, hooks, actions

• change url handling, templates, queries

• work based on hooks

• add_action()

• apply_filter()

add_action( ‘hook’, ‘function’) do_action(‘admin_notices’)

add_filter(‘hook’, $template)apply_filter

(‘template_redirect’)

Page 16: Getting to The Loop - London Wordpress Meetup  July 28th

loading plugins

• change url handling, templates, queries

• work based on hooks

• add_action()

• apply_filter()

Page 17: Getting to The Loop - London Wordpress Meetup  July 28th

// Fetch a random song lyric and add it to the pagefunction hello_dolly() { $chosen = hello_dolly_get_lyric(); echo "<p id='dolly'>$chosen</p>";}

// Now we set that function up to execute when the admin_notices action is calledadd_action( 'admin_notices', 'hello_dolly' );

hello_dolly_example.php

Page 18: Getting to The Loop - London Wordpress Meetup  July 28th

loading plugins

• change url handling, templates, queries

• work based on hooks

• add_action()

• apply_filter()

Page 19: Getting to The Loop - London Wordpress Meetup  July 28th

DB

• change url handling, templates, queries

• work based on hooks

• add_action()

• apply_filter()

Page 20: Getting to The Loop - London Wordpress Meetup  July 28th

turn the request URL into a DB query

• from - http://mysite.com/category/something

• to - WP_Query(category=‘something’)

Page 21: Getting to The Loop - London Wordpress Meetup  July 28th

class-wp.php<?php

class WP {

function main($query_args = '') { $this->init(); $this->parse_request($query_args); $this->send_headers(); $this->query_posts(); $this->handle_404(); $this->register_globals(); do_action_ref_array('wp', array(&$this)); }

Page 22: Getting to The Loop - London Wordpress Meetup  July 28th

class-wp.php<?php

class WP {

function main($query_args = '') { $this->init(); $this->parse_request($query_args); $this->send_headers(); $this->query_posts(); $this->handle_404(); $this->register_globals(); do_action_ref_array('wp', array(&$this)); }

Page 23: Getting to The Loop - London Wordpress Meetup  July 28th

Template

• change url handling, templates, queries

• work based on hooks

• add_action()

• apply_filter()

Page 24: Getting to The Loop - London Wordpress Meetup  July 28th

<?php/** * Loads the WordPress environment and template. */

require_once( dirname(__FILE__) . '/wp-load.php' );

wp();

require_once( ABSPATH . WPINC . '/template-loader.php' );

}

?>

blog-header.php

Page 25: Getting to The Loop - London Wordpress Meetup  July 28th

<?php/** * Loads the WordPress environment and template. */

require_once( dirname(__FILE__) . '/wp-load.php' );

wp();

require_once( ABSPATH . WPINC . '/template-loader.php' );

}

?>

blog-header.php

Page 26: Getting to The Loop - London Wordpress Meetup  July 28th

<?php// (abridged version)

if ( is_single() && $template = get_single_template() ) : elseif ( is_page() && $template = get_page_template() ) : elseif ( is_category() && $template = get_category_template() ) : elseif ( is_tag() && $template = get_tag_template() ) : elseif ( is_author() && $template = get_author_template() ) : elseif ( is_date() && $template = get_date_template() ) : elseif ( is_archive() && $template = get_archive_template() ) :

template-loader.php

Page 27: Getting to The Loop - London Wordpress Meetup  July 28th

The Loop

• change url handling, templates, queries

• work based on hooks

• add_action()

• apply_filter()

Page 28: Getting to The Loop - London Wordpress Meetup  July 28th

Start The Loop

• we now have our template, chosen inside template-loader.php

• we now have our query results from wp() in wp-blog-header.php

• now hand these over to The Loop - thanks Keith!