Add input fields and contextual help to your custom screens (custom post types or taxonomies).
Document Header
Homepage:https://eacDoojigger.earthasylum.com/
Author:EarthAsylum Consulting
Last Updated:09-May-2025
Contributors:EarthAsylum Consulting, Kevin Burkholder
Requires {eac}Doojigger:3.1.2
WordPress URI:https://wordpress.org/plugins/search/earthasylum/
GitHub URI:https://github.com/EarthAsylum/docs.eacDoojigger/wiki/
Option Field Methods
Creating custom post types or taxonomies?
There are several methods used by {eac}Doojigger to create the administrator options screens. These methods may also be used outside of the plugin settings page for which they were created.
Note: These examples should only be used when on your custom screen. You can ensure that by checking the current screen id:
if (get_current_screen()->id !== 'edit-my-screen-id') return;
These html_input
methods are made available by using the html_input_fields trait in your plugin.
use \EarthAsylumConsulting\Traits\html_input_fields;
A simple example is to first create an array of field definitions similar to the plugin and extension options (see {eac}Doojigger: Administrator Options)...
$myFormOptions =
[
'registrar_name' => array(
'type' => 'text',
'label' => 'Registrar Name',
),
'registrar_phone' => array(
'type' => 'tel',
'label' => 'Registrar Telephone',
),
'registrar_contact' => array(
'type' => 'email',
'label' => 'Registrar Support Email',
),
'registrar_web' => array(
'type' => 'url',
'label' => 'Registrar Web Address',
),
];
html_input_style()
To incorporate the css style declarations needed for the embedded html, use the html_input_style()
method...
add_action( 'admin_enqueue_scripts', array($this, 'html_input_style') );
html_input_block()
Then, where you want the form fields displayed, add the grid container and each of the fields, including label and input elements, using the html_input_block()
method...
echo "<div class='settings-grid-container'>\n";
foreach ($myFormOptions as $optionKey => $optionData)
{
$optionValue = ?; // get the current value for the field
echo $this->html_input_block($optionKey, $optionData, $optionValue);
}
echo "</div>\n";
html_input_sanitize()
To sanitize these fields when your form is submitted, use the html_input_sanitize()
method...
foreach ($myFormOptions as $optionKey => $optionData)
{
if (array_key_exists($optionKey,$_POST) )
{
$value = $this->html_input_sanitize($_POST[$optionKey], $optionKey, $optionData);
if ($_POST[$optionKey] == $value)
{
// input is valid (passed sanitization), do something with it
}
else
{
// input is invalid, let the user know
$this->add_option_error($optionKey,
sprintf("%s : The value entered does not meet the criteria for this field.",$optionData['label'])
);
}
}
}
html_input_validate()
The html_input_validate method uses and calls the field's 'validate' callback method (if set).
foreach ($myFormOptions as $optionKey => $optionData)
{
if (array_key_exists($optionKey,$_POST) )
{
$value = $this->html_input_validate($_POST[$optionKey], $optionKey, $optionData);
}
}
html_input_section()
For a more advanced layout with multiple blocks or sections of input fields...
$myFormSections =
[
'registrar_contact' =>
[
'registrar_name' => array(
'type' => 'text',
'label' => 'Registrar Name',
),
'registrar_phone' => array(
'type' => 'tel',
'label' => 'Registrar Telephone',
),
'registrar_contact' => array(
'type' => 'email',
'label' => 'Registrar Support Email',
),
'registrar_web' => array(
'type' => 'url',
'label' => 'Registrar Web Address',
),
],
'registration_defaults' =>
[
// ...
],
'license_limitations' =>
[
// ...
],
];
Add the fields in each section with a section header and fieldset (html_input_section()
)...
foreach ($myFormSections as $groupName => $myFormOptions)
{
echo $this->html_input_section($groupName, $myFormOptions);
foreach ($myFormOptions as $optionKey => $optionData)
{
$optionValue = ?; // get the current value for the field
echo $this->html_input_block($optionKey, $optionData, $optionValue);
}
echo "</fieldset>\n";
echo "</section>\n";
}
Then sanitize the fields from each section using html_input_sanitize()
when the form is submitted...
foreach ($myFormSections as $groupName => $myFormOptions)
{
foreach ($myFormOptions as $optionKey => $optionData)
{
if (array_key_exists($optionKey,$_POST) )
{
$value = $this->html_input_sanitize($_POST[$optionKey], $optionKey, $optionData);
if ($_POST[$optionKey] == $value)
{
// input is valid (passed sanitization), do something with it
}
else
{
// input is invalid, let the user know
$this->add_option_error($optionKey,
sprintf("%s : The value entered does not meet the criteria for this field.",$optionData['label'])
);
}
}
}
}
add_option_error()
The add_option_error()
, add_option_warning()
, add_option_info()
, and add_option_success()
methods are similar to the WordPress add_settings_error()
function with a slightly more advanced API that allows administrator notifications to be added anywhere on the page (befor the footer) and to survive a page reload or redirect.
Convert vertical section headers to horizontal tabs
Easily change a long vertical page with multiple input field sections into a tab layout with each section as a tab.
First add an element with a class of 'tab-container' where you want the tabs...
<nav class='tab-container'></nav>
Next add a 'true' boolean to the html_input_style()
method...
add_action( 'admin_enqueue_scripts', function()
{
$this->html_input_style(true);
}
);
This will include the css and javascript needed for tab functionality.
Additionally, you can pass an element selector which will have its visibility style changed to 'visible'.
$this->html_input_style(true,'table.form-table');
Screen Shot
These examples were derived from the Software taxonomy custom taxonomy extension used by {eac}SoftwareRegistry.
Contextual Help Methods
Taking the examples above and adding contextual help when adding fields is very simple.
First, in your constructor, add something like this to render the help screen.
add_action( 'current_screen', function($screen)
{
if ($screen->id == 'edit-' . 'my-awesome-screen')
{
$this->addPluginHelpTab('My Awesome Screen', $content, ['My Awesome Screen','open']);
$this->plugin_help_render($screen);
}
}
);
Then where you render your fields, render the field-level help with html_input_help()
foreach ($myFormSections as $groupName => $myFormOptions)
{
echo $this->html_input_section($groupName, $myFormOptions);
foreach ($myFormOptions as $optionKey => $optionData)
{
$this->html_input_help('My Awesome Screen', $optionKey, $optionData);
if ($optionData['type'] == 'help') continue;
$optionValue = ?; // get the current value for the field
echo $this->html_input_block($optionKey, $optionData, $optionValue);
}
echo "</fieldset>\n";
echo "</section>\n";
}
See {eac}Doojigger: Providing Contextual Help for more details.