wordpress-custom-post-type-plugin

1

Structure and php code of a basic wordpress plugin that adds a new custom post type to your wordpress installation.

Structure

wp-content
  /plugins
    /devshooin-cpt-project
      /index.php
      /devshooin-cpt-project.php

Content of index.php

<php
  // Silence is golden

Content of devshooin-cpt-project.php

<?php
/*
  Plugin Name: Project Custom Post Type
  Plugin URI:
  Description: Project Custom Post Type
  Version: 0.1
  Author: The Author
  Author: https://theauthorswebsite
  License: GPL2
  Text Domain: devshooin
*/

// security to prevent direct access of php files.
if ( ! defined( 'ABSPATH' )) {
  exit;
}

// Custom Post Type
function create_projects_custom_post_type() {
  $labels = array(
    'name' => __( 'projects', 'Post Type General Name', 'devshooin' ),
    'singular_name' => __( 'Project', 'Post Type Singular Name', 'devshooin' ),
    'menu_name' => __( 'Projects', 'devshooin' ),
    'name_admin_bar' => __( 'Projects', 'devshooin'),
    'archives' => __( 'Project Archives', 'devshooin'),
    'attributes' => __( 'Project attributes', 'devshooin'),
    'parent_item_colon' => __('Parent project', 'devshooin'),
    'all_items' => __( 'All Projects', 'devshooin'),
    'add_new_item' => __('Add new project', 'devshooin'),
    'add_new' => __('Add new', 'devshooin'),
    'new_item' => __('New Project', 'devshooin'),
    'edit_item' => __('Edit Project', 'devshooin'),
    'update_item' => __('Update Project', 'devshooin'),
    'view_item' => __('View Project', 'devshooin'),
    'view_items' => __('View Projects', 'devshooin'),
    'search_items' => __('Search Project', 'devshooin'),
    'not_fount' => __('Not found', 'devshooin'),
    'not_fount_in_trash' => __('Not found in trash', 'devshooin'),
    'featured_image' => __('Featured Image', 'devshooin'),
    'set_featured_image' => __('Set Featured Image', 'devshooin'),
    'remove_featured_image' => __('Remove Featured Image', 'devshooin'),
    'use_featured_image' => __('Use as Featured Image', 'devshooin'),
    'insert_into_item' => __('Insert into project', 'devshooin'),
    'uploaded_to_this_item' => __('Uploaded to this project', 'devshooin'),
    'items_list' => __('Projects list', 'devshooin'),
    'items_list_navigation' => __('Projects list navigation', 'devshooin'),
    'filter_items_list' => __('Filter projects list', 'devshooin')
  );
  
  $args = array(
    'label' => __( 'Project', 'devshooin'),
    'description' => __( 'Project', 'devshooin'),
    'labels' => $labels,
    'menu_icon' => 'dashicons-clipboard',
    'supports' => array(
      'title',
      'editor',
      'thumbnails',
      'revisions',
      'author',
      'excerpt',
      'custom-fields'
    ),
    'taxonomies' => array( 'category', 'post_tag'),
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'menu_position' => 5,
    'show_in_admin_bar' => true,
    'show_in_nav_menus' => true,
    'can_export' => true,
    'has_archive' => true,
    'hierarchical' => false,
    'exclude_from_search' => false,
    'show_in_rest' => true,
    'publicly_queryable' => true,
    'capability_type' => 'post',
    'rewrite' => array( 'slug' => 'projects')
  );

  register_post_type( 'projects', $args );
          
}
add_action( 'init', 'create_projects_custom_post_type', 0 );

function rewrite_project_flush() {
  create_projects_custom_post_type();
  flush_rewrite_rules();
}

register_activation_hook( __FILE__, 'rewrite_project_flush');

Useful links

WordPress Plugin Handbook
https://developer.wordpress.org/plugins/

WordPress Dashicon Catalog
https://developer.wordpress.org/resource/dashicons

Share.

About Author

Christian Unnerstall

Christian Unnerstall, founder of devshooin.com, enjoys designing and building things that look nice and work well, whether it's websites or services within the interwebs. Have questions - feel free to reach out to him. Enjoy

1 Comment

Leave a Reply