developing themes

without going crazy

@scribu
AppThemes

Keep related stuff together.

Keep unrelated stuff apart.

Typical HTML layout:

header.php

<html>
<head>
    ...
</head>

<body <?php body_class(); ?>>
<div id="page">
    <header id="branding" role="banner">
        ...
    </header><!-- #branding -->

    <div id="main">

index.php

<?php get_header(); ?>

<div id="primary">
    <div id="content" role="main">
        ...
    </div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>

<?php get_footer(); ?>

theme wrappers

scribu.net/wordpress/theme-wrappers.html

wrapper.php

<html>
<head>
    ...
</head>

<body <?php body_class(); ?>>
<div id="page">
    <?php get_header(); ?>

    <div id="main">
    <?php include app_template_path(); ?>
    </div><!-- #main -->

    <?php get_footer(); ?>
</div><!-- #page -->

<?php wp_footer(); ?>

</body>
</html>

header.php (wrapped)

    <header id="branding" role="banner">
        ...
    </header><!-- #branding -->

index.php (wrapped)

<div id="primary">
    <div id="content" role="main">
        ...
    </div><!-- #content -->
</div><!-- #primary -->

<?php get_sidebar(); ?>

Typical PHP layout:

functions.php; html-script: true

Level 1

add_filter( 'posts_where', 'my_posts_where', 10, 2 );
add_action( 'template_redirect', 'my_template_redirect', 10, 2 );

function my_posts_where( $sql, $wp_query ) {
	if ( $wp_query->is_main_query() && $wp_query->get( 'my_query_var' ) ) {
		$sql = ...;
	}

	return $sql;
}

function my_template_redirect() {
	global $wp_query;

	if ( $wp_query->is_main_query() && $wp_query->get( 'my_query_var' ) ) {
		wp_enqueue_script( ... );
	}
}

Level 2

class My_View {

	function __construct() {
		add_filter( 'posts_where', array( $this, 'posts_where' ), 10, 2 );
		add_action( 'template_redirect', array( $this, 'template_redirect' ), 10, 2 );
	}

	function posts_where( $sql, $wp_query ) {
		if ( $wp_query->is_main_query() && $wp_query->get( 'my_query_var' ) ) {
			$sql = ...;
		}

		return $sql;
	}

	function template_redirect() {
		global $wp_query;

		if ( $wp_query->is_main_query() && $wp_query->get( 'my_query_var' ) ) {
			wp_enqueue_script( ... );
		}
	}
}

new My_View;

Level 3

class My_View extends APP_View {

	function condition() {
		return get_query_var( 'my_query_var' );
	}

	function posts_where( $sql, $wp_query ) {
		$sql = ...;

		return $sql;
	}

	function template_redirect() {
		wp_enqueue_script( ... );
	}
}

new My_View;

APP_View: gist.github.com/2865132

contact

site: scribu.net
twitter+github: @scribu

/

#