Skip to content

Instantly share code, notes, and snippets.

@ScreamingDev
Last active March 13, 2016 14:37
Show Gist options
  • Save ScreamingDev/17fe51a14996888baeb9 to your computer and use it in GitHub Desktop.
Save ScreamingDev/17fe51a14996888baeb9 to your computer and use it in GitHub Desktop.
WordPress add functions per view

Load additional scripts depending on post-type.

Additional files depending on the current screen will be loaded automatically. Possible screens can be:

  • home
  • search
  • {post-type}
    • {post-type}-archive
    • {post-type}-single

The loaded files can be overridden by the child-theme. Just add the according file in the folder and wordpress will notice your "extension".

Example

General screens

Imagine looking at the homepage. This file will be loaded automatically:

  • inc/screen/home.php

For search results this file will be loaded:

  • inc/screen/search.php

Post Type screen

Imagine the single view for post-type "customer" is loaded. Those files will be loaded automatically:

  • inc/screen/customer.php
  • inc/screen/customer-single.php

Looking at an archive those files are loaded

  • inc/screen/customer.php
  • inc/screen/customer-archive.php

Override in child-theme

Imagine those files in the parent theme:

  • parent/inc/screen/home.php
  • parent/inc/screen/post-archive.php

If you like to replace one of these files or extend it's content then just place the same file in your child-theme:

  • child/inc/screen/post-archive.php

From now on WordPress will notice your file first and use it prior to the one in the parent theme.

Issues

  • Missing taxonomy logic.
<?php
/**
* Load additional scripts depending on post-type.
*
* Additional files depending on the current screen will be loaded automatically.
* Possible screens can be:
*
* - home
* - search
* - {post-type}
* - {post-type}-archive
* - {post-type}-single
*
* The loaded files can be overridden by the child-theme.
* Just add the according file in the folder
* and wordpress will notice your "extension".
*
*
* ## Example
*
* ### General screens
*
* Imagine looking at the homepage.
* This file will be loaded automatically:
*
* - inc/screen/home.php
*
* For search results this file will be loaded:
*
* - inc/screen/search.php
*
* ### Post Type screen
*
* Imagine the single view for post-type "customer" is loaded.
* Those files will be loaded automatically:
*
* - inc/screen/customer.php
* - inc/screen/customer-single.php
*
* Looking at an archive those files are loaded
*
* - inc/screen/customer.php
* - inc/screen/customer-archive.php
*
* ### Override in child-theme
*
* Imagine those files in the parent theme:
*
* - parent/inc/screen/home.php
* - parent/inc/screen/post-archive.php
*
* If you like to replace one of these files
* or extend it's content then just place the same file in your child-theme:
*
* - child/inc/screen/post-archive.php
*
* From now on WordPress will notice your file first
* and use it prior to the one in the parent theme.
*
* ## Issues
*
* @todo Missing taxonomy logic.
*
* @copyright Mike Pretzlaw <[email protected]>
* @license https://opensource.org/licenses/MIT
*
* @param $query_vars
*
* @return mixed
*/
function codebook_inc_screen( $query_vars ) {
$screen = 'home';
$context = 'home';
if ( isset( $query_vars['post_type'] ) && $query_vars['post_type'] ) {
$screen = 'post_type';
// trim post-types to prevent hacks, like "../../" post-type
$context = ltrim( $query_vars['post_type'], '/.' );
} elseif ( isset( $query_vars['s'] ) ) {
$screen = 'search';
$context = 'search';
}
$view_file = 'inc/screen/' . sanitize_file_name( str_replace( '_', '-', $context ) );
// Load general file for post-type (e.g. inc/post-types/posts.php).
locate_template( $view_file . '.php', true );
if ( 'post_type' != $screen ) {
// those views have no mode, we're done here
return $query_vars;
}
// assume archive view
$mode = 'archive';
if ( isset( $query_vars['name'] ) && $query_vars['name'] ) {
// a name were given so this is a single view
$mode = 'single';
}
// Load general file for post-type mode (archive or single).
locate_template( $view_file . '-' . $mode . '.php', true );
return $query_vars;
}
add_filter( 'request', 'codebook_inc_screen' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment