How to add additional / custom fields to the default WordPress Posts #
This basically works the same way you add forms and fields to your custom post type.
The only difference: You add 'extends' => ['post']
to the coniguration array.
This way you can add custom forms and fields to the standard WordPress-posts. Following the WordPress tradition, this data will be stored in the table postmeta
of your database.
Let's add an input-field "www" that will appear on the right column when editing a posts in the backend.
File: Configuration/CPT/post.php
#
<?php
$myForm = [
'ctrl' => [
'title' => 'Additional Infos',
'position' => 'right,bottom',
],
'columns' => [
'www' => [
'label' => 'Website',
'config' => [
'type' => 'input',
'size' => 30,
'max' => 255,
],
],
],
];
return [
'extends' => ['post'],
'forms' => [$myForm]
];
Done.
When editing a WordPress post in the backend, you now will see a new form on the right bottom column with an input field.
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, the configuration is inside a plugin called example
and we have defined a fields with the name www
. These will be saved in the table postmeta
with the meta_key _example_www
.