getting to the loop - london wordpress meetup july 28th

Post on 17-May-2015

2.172 Views

Category:

Technology

2 Downloads

Preview:

Click to see full reader

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.

TRANSCRIPT

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...

Load

Plugins

DB

Template

The Loop

Load in files for a bootstrap phase

set up Plugins

turn the request URL into a DB query

choose a Template

start The Loop

Load

important files for this step:

• index.php

• blog-header.php

• wp-load.php

• wp-settings.php

<?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

<?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

<?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

<?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

<?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

<?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

<?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

<?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

Plugins

• change url handling, templates, queries

• work based on hooks

• add_action()

• apply_filter()

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’)

loading plugins

• change url handling, templates, queries

• work based on hooks

• add_action()

• apply_filter()

// 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

loading plugins

• change url handling, templates, queries

• work based on hooks

• add_action()

• apply_filter()

DB

• change url handling, templates, queries

• work based on hooks

• add_action()

• apply_filter()

turn the request URL into a DB query

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

• to - WP_Query(category=‘something’)

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)); }

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)); }

Template

• change url handling, templates, queries

• work based on hooks

• add_action()

• apply_filter()

<?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

<?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

<?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

The Loop

• change url handling, templates, queries

• work based on hooks

• add_action()

• apply_filter()

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!

Phew! Thank you!

c@headshift.com@mrchrisadams

top related