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)
  • Adding forms and fields to your CPT

Adding forms and fields to your CPT

Table of Contents
  • How to add a form-field to your Custom Post Type (CPT)
    • File: Configuration/CPT/book.php
    • Convention over Configuration
Table of Content

How to add a form-field to your Custom Post Type (CPT) #

After you created your custom post type you can simply modify the form-Array of your CPT-configuration in Configuration/CPT/book.php.

This way you can create custom forms and fields to add additional data to your CPT in the WordPress backend when editing the post. Following the WordPress tradition, this data is stored in the table postmeta of your database.

Let's add two input-fields for the ISBN-number of the book and a link to the homepage of the author.

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'],
];

$bookInfoForm = [
    'ctrl' => [
        'title'   => 'Additional Infos',
        'position' => 'right,bottom',
    ],
    'columns' => [
        'isbn' => [
            'label' => 'ISBN-number',
            'config' => [
                'type' => 'input',
                'size' => 30,
                'max' => 255,
            ],
        ],
        'www' => [
            'label' => 'Author website',
            'config' => [
                'type' => 'input',
                'placeholder' => 'https://...',
            ],
        ],
    ],
];

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

Done.

When editing your CPT, you now will see a new form on the right bottom column with two input fields.

nnhelpers takes care of all the complicated things like rendering the form-fields, adding a nonce for security and writing / updating the data in the postmeta-database table.

Convention over Configuration #

The form-data will be saved as postmeta-data using the convention _{pluginname}_{fieldname}.

In the example above we are in a plugin called example and have defined two fields with the names isbn and www. These will be saved in the table postmeta with the meta_key _example_isbn and _example_www.

Share This Article :
  • Facebook
  • Twitter
  • LinkedIn
  • Pinterest
Still stuck? How can we help?

How can we help?

Updated on 3. Juli 2022
Creating a Custom Post Type (CPT)Adding forms and fields to WordPress posts

Powered by BetterDocs

Table of Contents
  • How to add a form-field to your Custom Post Type (CPT)
    • File: Configuration/CPT/book.php
    • Convention over Configuration
nnhelpers for WordPress
Mit Stolz präsentiert von WordPress.