The Button field type allows you to add interactive buttons to your custom fields interface. These buttons can trigger actions through WordPress hooks or navigate to specific URLs.
array(
'type' => 'button',
'title' => 'Click Me',
'action' => 'my_custom_action',
'url' => 'https://example.com',
'target' => '_blank',
'primary' => true,
)For Default Field Properties, see Field Types Definition.
title(string) — The text to display on the button.action(string) — Name of a WordPress hook action to trigger when the button is clicked. When the action is triggered, the field properties are passed as a parameter to the action callback.url(string) — URL to navigate to when the button is clicked. This property is ignored ifactionis specified. Also acceptshrefas an alias.target(string) — The target attribute for the link, specifying where to open the URL (e.g.,_blank,_self). Defaults to_blank. This property is ignored ifactionis specified.primary(boolean) — Whether to style the button as a primary action button with highlight color. Defaults tofalse.
This field does not store any value.
'regenerate_button' => array(
'type' => 'button',
'title' => 'Regenerate Thumbnails',
'action' => 'my_regenerate_thumbnails_action',
'primary' => true,
),With JavaScript handler:
// Add your action handler in your custom JS file
wp.hooks.addAction('my_regenerate_thumbnails_action', 'my-plugin', function(props) {
// Your action code here
console.log('Button clicked with props:', props);
// e.g., start a regeneration process via AJAX
});'documentation_button' => array(
'type' => 'button',
'title' => 'View Documentation',
'url' => 'https://docs.example.com',
'target' => '_blank',
),$f = new \Wpify\CustomFields\FieldFactory();
$f->button(
title: 'Click Me',
url: 'https://example.com',
target: '_blank',
primary: true,
);- The Button field type is a static field that does not store any data.
- It is useful for triggering JavaScript actions or providing quick navigation links.
- You can combine it with conditional logic to show or hide buttons based on other field values.
- To make buttons work with custom JavaScript actions, register your action handlers using the WordPress hooks API.