Snippets

Joseph Dickson Custom Post Type - Directory

Created by Joseph Dickson

File index.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+/**
+ * Plugin Name: Directory Custom Post Type
+ * Description: Custom Post Type for an employee directory. Requires customization to the active theme to display custom fields.
+ * Version: 1.0
+ * License: GPL2
+ */
+
+// Prevent direct access to this file
+defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
+
+function pz_employee_custom_post_type() {
+
+/**
+ * Hero Images for the Front Page
+ * Theme will need to be configured using WP Query to use this tool
+ */
+$labels = array(
+	'name'			=> 'Directory',
+	'singular_name'		=> 'Employee',
+	'menu_name'		=> 'Directory',
+	'name_admin_bar'	=> 'Employee',
+	'add_new'		=> 'Add an Employee',
+	'add_new_item'		=> 'Add an Employee',
+	'new_item'		=> 'New Employee',
+	'edit_item'		=> 'Edit Employee',
+	'view_item'		=> 'View Employee',
+	'all_items'		=> 'All Employees',
+	'search_items'		=> 'Search Employees',
+	'parent_item_colon'	=> 'Parent Employees:',
+	'not_found'		=> 'No Employees found.',
+	'not_found_in_trash'	=> 'No Employee entries found in Trash.',
+);
+
+$args = array(
+	'labels'		=> $labels,
+	'public'		=> false,
+	'publicly_queryable'	=> true,
+	'show_ui'		=> true,
+	'show_in_menu'		=> true,
+	'menu_icon'		=> 'dashicons-groups',
+	'query_var'		=> true,
+	'rewrite'		=> array( 'slug' => 'directory' ),
+	'capability_type'	=> 'post',
+	'has_archive'		=> true,
+	'hierarchical'		=> true,
+	'menu_position'		=> 5,
+	'show_in_rest'		=> true,
+	'supports'		=> array( 'title', 'editor', 'thumbnail', 'custom-fields', 'page-attributes' ),
+	'taxonomies'		=> array( 'category' ),
+);
+
+register_post_type( 'employees', $args );
+
+}
+
+add_action( 'init', 'pz_employee_custom_post_type' );
+
+// Flush rewrite to add as a permalink slug when plugin is activated or reactiviated.
+function pz_rewrite_flush() {
+    pz_employee_custom_post_type();
+    flush_rewrite_rules();
+}
+register_activation_hook( __FILE__, 'pz_rewrite_flush' );
+
+/*
+ * Disables Advanced Custom Fields default preference to hide custom fields
+ * This allows the user to add standard custom fields that can be added to the
+ * active theme such as email address, office location etc.
+if ( function_exists('get_field') ) {
+	add_filter('acf/settings/remove_wp_meta_box', '__return_false');
+}
HTTPS SSH

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