Extensibility

Longview aims to be as extensible as possible. Almost all content between the body tags is added via action hooks so that any portion can be added, removed, or extended. Most text output (including class names and template part names) is filterable as well.

As an example, let’s take a look at the action section of the index.php file.

get_header();

// Use this hook to add and remove actions.
do_action( 'thmfdn_template_setup' );

do_action( 'thmfdn_content_before' );
do_action( 'thmfdn_content_top' );

if ( have_posts() ) {
	while ( have_posts() ) {
		the_post();
		get_template_part(
			apply_filters( 'thmfdn_template_part_slug', 'template-parts/content' ),
			apply_filters( 'thmfdn_template_part_name', '' )
		);
	}
} else {
	get_template_part(
		apply_filters( 'thmfdn_404_template_part_slug', 'template-parts/404' ),
		apply_filters( 'thmfdn_404_template_part_name', '' )
	);
}

do_action( 'thmfdn_content_bottom' );
do_action( 'thmfdn_content_after' );

get_sidebar();
get_footer();

Don’t worry, this isn’t the entire index.php file. This just gives you an idea of the extensibility of the theme. All the content is added via action hooks, template parts, and calls to get_header/sidebar/footer(). This means that extending the index.php file as a base for other template files is simple and straight forward.

As an example, here is the entire page.php file.

/**
 *  Single page template
 *
 * @package Longview
 * @since 1.0.0
 */

if ( !function_exists( 'thmfdn_page_featured_image' ) ) {
	/**
	 * Featured image
	 *
	 * Displays the featured image (formerly called the post thumbnail).
	 *
	 * @since 1.0.0
	 */
	function thmfdn_page_featured_image() {
		if ( has_post_thumbnail() ) {
			?>
				<div class="row thmfdn-featured-image">
					<div class="wrap">
						<div>
							<?php the_post_thumbnail( apply_filters( 'thmfdn_thumbnail_size', '' ) ); ?>
						</div>
					</div>
				</div>
			<?php
		}
	}
}
// The thmfdn_header_after action hook is located in the header.php file.
add_action( 'thmfdn_header_after', 'thmfdn_page_featured_image', 50 );

if ( !function_exists( 'thmfdn_template_part_page' ) ) {
	/**
	 * Single template part
	 *
	 * Filters the default template part to use.
	 *
	 * @since 1.0.0
	 */
	function thmfdn_template_part_page() {
		return 'page';
	}
}
// The thmfdn_template_part_name filter hook is located in the index.php file.
add_filter( 'thmfdn_template_part_name', 'thmfdn_template_part_page' );

// Loads the default template.
locate_template( array( 'index.php' ), true );

As you can see, the page.php file adds one action (to display the featured image) and adds one filter (to change which template part gets loaded). Then, because the page.php file extends the index.php file, the index.php file is loaded. The locate_template() function is used so that the index.php file will be loaded from a child theme if available.

This isn’t the end though. The page.php file can be extended to create custom page templates. For example, here is the entire code for the full width content page template.

/**
 * Single page template with no sidebars and full width content
 *
 * @package Longview
 * @since 1.0
 */

/*
Template Name: Full Width
*/

/**
 * Sets the layout class
 *
 * @since 1.0
 * @return string Layout class.
 */
function thmfdn_full_width_page() {
	return 'content-full-width';
}
add_filter( 'thmfdn_layout_class', 'thmfdn_full_width_page' );

// Loads the default page template.
locate_template( array( 'page.php' ), true );

It only takes five lines of actual code to create the custom page template. This is the extensibility built into the Longview theme.