Zum Inhalt springen
nnhelpers for WordPress

nnhelpers for WordPress

A toolbox-plugin with a huge collection of one-liners to drastically speed up your plugin-development

About

  • What is nnhelpers?

Getting started

  • Installation
  • Creating a plugin

Custom Post Types (CPT)

  • Creating a Custom Post Type (CPT)
  • Adding forms and fields to your CPT
  • Adding forms and fields to WordPress posts
  • CPT – field types and configurations

Custom Widgets and Blocks

  • Creating a custom widget for Elementor

Basic Concepts

  • QueryBuilder
  • Model
  • Repository
  • Fluid Templates
  • Creating custom ViewHelpers
  • Home
  • Docs
  • Custom Post Types (CPT)
  • Creating a Custom Post Type (CPT)

Creating a Custom Post Type (CPT)

Table of Contents
  • Create a Custom Post Type (CPT) for WordPress
    • File: Configuration/CPT/book.php
    • Convention over Configuration
Table of Content

Create a Custom Post Type (CPT) for WordPress #

After you have created a plugin you can now define and configure your custom post type (CPT).

In the following example we will create a new Post Type for "books".

  • Add a folder Configuration/CPT to your plugin.
  • Create a file book.php in the folder Configuration/CPT. You can name the file however you like – in this example we want to create a CPT for books, so the name kind of makes sense 😉
  • Add the following script to the book.php:

File: Configuration/CPT/book.php #

<?php

$cptConfig = [
    'labels' => [
        'name' => 'Books',
        'singular_name' => 'Book',
    ],
    'description' => 'List of books from the example extension.',
    'public' => true,
    'has_archive' => true,
    'menu_icon'   => 'dashicons-book',
    'menu_position' => 20,
    'supports' => ['title', 'editor', 'custom-fields', 'thumbnail'],
    'rewrite' => ['slug' => 'example'],
    'taxonomies' => ['category'],
];

return [
    'config' => $cptConfig,
    'forms' => []
];

That was it. You now have a Custom Post Type (CPT) which you can add entries to in the backend.

Information about the options in the CPT-configuration can be found in the WordPress Documentation

Convention over Configuration #

  • The post_type of your CPT will be stored in the database using the convention {pluginname}_{cptname}. If your plugin-folder is named example and the CPT-file is named book.php, then the post_type in the database will be example_book
Share This Article :
  • Facebook
  • Twitter
  • LinkedIn
  • Pinterest
Still stuck? How can we help?

How can we help?

Updated on 4. Juli 2022
Adding forms and fields to your CPT

Powered by BetterDocs

Table of Contents
  • Create a Custom Post Type (CPT) for WordPress
    • File: Configuration/CPT/book.php
    • Convention over Configuration
nnhelpers for WordPress
Mit Stolz präsentiert von WordPress.