Snippets

Joseph Dickson Custom Post Type - Hero Image & Featured Content Tiles

Created by Joseph Dickson
<?php
/**
 * Plugin Name: Hero Image and Featured Content Tiles
 * Description: Custom Post Type for Hero Images and featured content tiles that can be used on a homepage or front page. Requires customizations to the active theme.
 * Author: Joseph Dickson
 * Author URI: https://joseph-dickson.com
 * Version: 0.1
 * License: GPL2
 */

// Prevent direct access to this file
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );

/**
 *
 * https://codex.wordpress.org/Function_Reference/register_post_type#Arguments
 */
function pz_rrc_custom_post_types() {

/**
 * Hero Images for the Front Page
 * Theme will need to be configured using WP Query
 */
$labels = array(
	'name'			=> 'Front Page',
	'singular_name'		=> 'Front Page',
	'menu_name'		=> 'Front Page',
	'name_admin_bar'	=> 'Hero Image',
	'add_new'		=> 'Add a Hero Image',
	'add_new_item'		=> 'Hero Image',
	'new_item'		=> 'Hero Image',
	'edit_item'		=> 'Edit Hero Image',
	'view_item'		=> 'View Hero Image',
	'all_items'		=> 'All Hero Images',
	'search_items'		=> 'Search Hero Images',
	'parent_item_colon'	=> 'Parent Hero Images:',
	'not_found'		=> 'No Hero Images found.',
	'not_found_in_trash'	=> 'No Hero Images found in Trash.',
);

$args = array(
	'labels'		=> $labels,
	'public'		=> true,
	'publicly_queryable'	=> false,
	'show_ui'		=> true,
	'show_in_menu'		=> true,
	'menu_icon'		=> 'dashicons-format-image',
	'query_var'		=> true,
	'rewrite'		=> array( 'slug' => 'hero-image' ),
	'capability_type'	=> 'post',
	'has_archive'		=> false,
	'hierarchical'		=> false,
	'menu_position'		=> 5,
	'show_in_rest'		=> true,
	'supports'		=> array( 'title', 'editor', 'thumbnail' ),
);

register_post_type( 'hero-image', $args );

/**
 * Featured Content Tiles for the Front Page
 * Theme will need to be configured using WP Query
 */
$labels = array(
	'name'			=> 'Featured Content',
	'singular_name'		=> 'Featured Content',
	'menu_name'		=> 'Featured Content',
	'name_admin_bar'	=> 'Tile',
	'add_new'		=> 'Add a Tile',
	'add_new_item'		=> 'Add a Tile',
	'new_item'		=> 'New Tile',
	'edit_item'		=> 'Edit Featured Content',
	'view_item'		=> 'View Featured Content Tiles',
	'all_items'		=> 'Tiles',
	'search_items'		=> 'Search Featured Content Tiles',
	'parent_item_colon'	=> 'Parent Featured Content Tiles:',
	'not_found'		=> 'No Featured Content Tiles found.',
	'not_found_in_trash'	=> 'No Featured Content Tiles found in Trash.',
);

$args = array(
	'labels'		=> $labels,
	'public'		=> true,
	'publicly_queryable'	=> false,
	'show_ui'		=> 'edit.php?post_type=hero-image',
	'show_in_menu'		=> 'edit.php?post_type=hero-image',
	'menu_icon'		=> 'dashicons-format-image',
	'query_var'		=> true,
	'rewrite'		=> array( 'slug' => 'featured-content' ),
	'capability_type'	=> 'post',
	'has_archive'		=> false,
	'hierarchical'		=> false,
	'menu_position'		=> 5,
	'show_in_rest'		=> true,
	'supports'		=> array( 'title', 'editor', 'thumbnail' ),
);

register_post_type( 'featured-content', $args );

}

add_action( 'init', 'pz_rrc_custom_post_types' );

if ( function_exists( 'add_theme_support' ) ) {
	add_image_size( 'event-thumb', 370, 208, array( 'center', 'center' ) ); // Custom thumbnail size for the events on the front page
	add_image_size( 'featured-content', 690, 350, array( 'center', 'center' ) ); // Custom thumbnail size for the featured content on the front page
}

// Flush rewrite rules to add "Zest" as a permalink slug when plugin is activated
function my_rewrite_flush() {
    pz_rrc_custom_post_types();
    flush_rewrite_rules();
}
register_activation_hook( __FILE__, 'my_rewrite_flush' );

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.