Skip to content

Instantly share code, notes, and snippets.

@thadallender
Last active February 26, 2021 07:39
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thadallender/d40271f49c410745177f0760838b0dd9 to your computer and use it in GitHub Desktop.
Save thadallender/d40271f49c410745177f0760838b0dd9 to your computer and use it in GitHub Desktop.
WordPress theme starter content with custom post type and postmeta integration
<?php
/**
* Sets up theme defaults and registers support for various WordPress features.
*
* Note that this function is hooked into the after_setup_theme hook, which
* runs before the init hook. The init hook is too late for some features, such
* as indicating support for post thumbnails.
*/
function aperture_setup() {
// Define and register starter content to showcase the theme on new sites.
$starter_content = array(
// Create the custom image attachments used as post thumbnails for pages.
'attachments' => array(
'image-boulder' => array(
'post_title' => _x( 'Boulder', 'Theme starter content', 'vs' ),
'file' => 'assets/images/boulder.jpg', // URL relative to the template directory.
),
'image-flare' => array(
'post_title' => _x( 'Flare', 'Theme starter content', 'vs' ),
'file' => 'assets/images/flare.jpg',
),
'image-kiss' => array(
'post_title' => _x( 'Kiss', 'Theme starter content', 'vs' ),
'file' => 'assets/images/kiss.jpg',
),
'image-man' => array(
'post_title' => _x( 'Man', 'Theme starter content', 'vs' ),
'file' => 'assets/images/man.jpg', // URL relative to the template directory.
),
'image-moon' => array(
'post_title' => _x( 'Moon', 'Theme starter content', 'vs' ),
'file' => 'assets/images/moon.jpg',
),
'image-record' => array(
'post_title' => _x( 'Record', 'Theme starter content', 'vs' ),
'file' => 'assets/images/record.jpg',
),
'image-sea' => array(
'post_title' => _x( 'Sea', 'Theme starter content', 'vs' ),
'file' => 'assets/images/sea.jpg', // URL relative to the template directory.
),
'image-thinker' => array(
'post_title' => _x( 'Thinker', 'Theme starter content', 'vs' ),
'file' => 'assets/images/thinker.jpg',
),
'image-woods' => array(
'post_title' => _x( 'Woods', 'Theme starter content', 'vs' ),
'file' => 'assets/images/woods.jpg',
),
),
// Specify the core-defined pages to create and add custom thumbnails to some of them.
'posts' => array(
'home',
'about',
'contact',
'blog',
'gallery' => array(
'post_type' => 'vs_gallery',
'post_title' => _x( 'Example Gallery', 'Theme starter content', 'vs' ),
'post_content' => '',
),
'cart' => array(
'post_type' => 'page',
'post_title' => _x( 'Cart', 'Theme starter content', 'vs' ),
'post_content' => '[vs_cart]',
),
'search' => array(
'post_type' => 'page',
'post_title' => _x( 'Advanced Search', 'Theme starter content', 'vs' ),
'post_content' => '[vs_search]',
),
'thanks' => array(
'post_type' => 'page',
'post_title' => _x( 'Thanks', 'Theme starter content', 'vs' ),
'post_content' => '[vs_thanks]',
),
'lightbox' => array(
'post_type' => 'page',
'post_title' => _x( 'Lightbox', 'Theme starter content', 'vs' ),
'post_content' => '[vs_lightbox]',
),
'login' => array(
'post_type' => 'page',
'post_title' => _x( 'Login', 'Theme starter content', 'vs' ),
'post_content' => '[vs_login]',
),
'dashboard' => array(
'post_type' => 'page',
'post_title' => _x( 'Dashboard', 'Theme starter content', 'vs' ),
'post_content' => '[vs_dashboard]',
),
),
// the widgets
'widgets' => array(
// Add the homepage widget
'homepage' => array(
'example_gallery' => array(
'gallery', array(
'vs-gallery-title' => _x( 'Example Gallery', 'Theme starter content', 'vs' ),
'vs-gallery-show' => 'single',
'vs-gallery-id' => '{{gallery}}',
'vs-gallery-qty' => '9',
),
),
'example_map' => array(
'map',
),
),
),
// Default to a static front page and assign the front and posts pages.
'options' => array(
'show_on_front' => 'page',
'page_on_front' => '{{home}}',
'page_for_posts' => '{{blog}}',
),
// Set up nav menus for each of the two areas registered in the theme.
'nav_menus' => array(
// Assign a menu to the "top" location.
'top' => array(
'name' => __( 'Top Menu', 'vs' ),
'items' => array(
'link_home', // Note that the core "home" page is actually a link in case a static front page is not used.
'archive_galleries' => array(
'title' => _x( 'Galleries', 'Theme starter content', 'vs' ),
'url' => esc_url( site_url( 'galleries' ) ),
),
'page_blog',
'page_contact',
'page_cart' => array(
'type' => 'post_type',
'object' => 'page',
'object_id' => '{{cart}}',
),
),
),
// Assign a menu to the "social" location.
'social' => array(
'name' => __( 'Social Links Menu', 'vs' ),
'items' => array(
'link_twitter' => array(
'title' => _x( 'Twitter', 'Theme starter content', 'vs' ),
'url' => 'https://twitter.com/',
),
'link_facebook' => array(
'title' => _x( 'Facebook', 'Theme starter content', 'vs' ),
'url' => 'https://facebook.com/',
),
'link_instagram' => array(
'title' => _x( 'Instagram', 'Theme starter content', 'vs' ),
'url' => 'https://instagram.com/',
),
),
),
),
);
add_theme_support( 'starter-content', $starter_content );
}
add_action( 'after_setup_theme', 'aperture_setup' );
/**
* Add postmeta to starter content posts
* @param array $content the starter content array
* @param [type] $config [description]
* @return [type] [description]
*/
function aperture_starter_content_add_postmeta( $content, $config ) {
// bail if not a fresh site
if ( false === get_option( 'fresh_site' ) ) {
return;
}
// loop over attachments added as starter-content
$args = array(
'posts_per_page' => 9,
'post_type' => 'attachment',
);
$attachments = get_posts( $args );
if ( $attachments && is_array( $attachments ) ) {
$attachment_ids = array();
foreach ( $attachments as $attachment ) {
$attachment_ids[] = $attachment->ID;
}
// set attachments at postmeta
if ( isset( $content['posts']['gallery'] ) ) {
$content['posts']['gallery']['meta_input'] = array(
'_vs_gallery' => $attachment_ids,
);
}
}
return $content;
}
add_filter( 'get_theme_starter_content', 'aperture_starter_content_add_postmeta', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment