Skip to content

Instantly share code, notes, and snippets.

@cdillon
Last active November 15, 2015 19:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cdillon/88f919e1269f3a01370b to your computer and use it in GitHub Desktop.
Save cdillon/88f919e1269f3a01370b to your computer and use it in GitHub Desktop.
A minimalist WordPress plugin to allow child themes to inherit their parent's screenshot.
<?php
/**
* Plugin Name: Theme Screenshot Inheritance
* Description: Child themes without screenshots inherit their parent theme's screenshot.
*
* Drop this in the /plugins or /mu-plugins directory.
*/
/**
* Add parent screenshot to child theme.
*
* @param $prepared_themes
* @return mixed
*/
function theme_screenshot_inheritance( $prepared_themes ) {
// build list of names and id's
$list = array();
foreach ( $prepared_themes as $key => $theme ) {
$list[$key] = $theme['name'];
}
$list = array_flip( $list );
// child theme inherits parent screenshot
foreach ( $prepared_themes as $key => $theme ) {
if ( $theme['parent'] && !$theme['screenshot'][0] ) {
// find theme in our list
$parent_theme_id = $list[$theme['parent']];
// get screenshot
$parent_screenshot = $prepared_themes[$parent_theme_id]['screenshot'];
// replace child theme screenshot
$prepared_themes[$key]['screenshot'] = $parent_screenshot;
}
}
return $prepared_themes;
}
add_filter( 'wp_prepare_themes_for_js', 'theme_screenshot_inheritance' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment