Page tree
Skip to end of metadata
Go to start of metadata

You can add extra buttons with javascript interactions in the admin panel. However, to make sure the security is properly maintained, you can not simply add a row to the array.

Practical Example

During the migration process to Resurs, we want to add an extra button to be able to reset configuration data that resides in the v2.2-configuration set. First of all, we want to configure the button itself in the array like this:

                [
                    'type' => 'button',
                    'action' => 'button',
                    'title' => __(
                        'Clean up prior version settings',
                        'tornevalls-resurs-bank-payment-gateway-for-woocommerce'
                    ),
                    'desc' => __(
                        'Removes all traces of resurs-plugin v2.2 from the database.',
                        'tornevalls-resurs-bank-payment-gateway-for-woocommerce'
                    ),
                    'custom_attributes' => [
                        'onclick' => 'rbwcResetVersion22()',
                    ],
                ],

Now, this is a little bit special case since we don't want to show that button as long as there are no configuration present from version 2.2. But this is just a smaller and not relevant part of the case, it just has to be mentioned here.

Now to the security part: This button will not show up in the admin-frontend instantly. For cases where we add buttons with front-end functions, we need to check in FormFields.php in the function getFieldButtonApi(). In this function we define all button functions that are allowed to be shown in the admin panel. Currently, this function does not contain any filters; if you plan to do external plugins, you either have to solve your problem on your own or tell us to add allowance for this the specific button as we own the configuration space for the plugin. In future releases, if needed, this feature may be changed.

However, to add allowance for the new button, in this case reset_former_plugin_settings, the button should be added in the $allowedFormData-array and the columns-array. The columns array is only necessary if you plan to change the layout format to "columnable"  buttons like this:

This is how it looks:

    public function getFieldButtonApi($formData)
    {
        $action = isset($formData['action']) && !empty($formData['action']) ? $formData['action'] : 'button';

        $allowedFormData = [
            Data::getPrefix('admin_payment_methods_button'),
            Data::getPrefix('admin_callbacks_button'),
            Data::getPrefix('admin_trigger_callback_button'),
            Data::getPrefix('admin_the_reset_button'),
            Data::getPrefix('admin_reset_former_plugin_settings'),
        ];

        if (isset($formData['id']) && in_array($formData['id'], $allowedFormData, true)) {
            $formArray = $formData;
            $formArray['action'] = $action; // Our action
            $formArray['custom_attributes'] = $this->get_custom_attribute_html($formData);
            $formArray['columns'] = [
                'the_reset_button' => true,
                'reset_former_plugin_settings' => true
            ];
            $formArray['short_id'] = preg_replace(sprintf('/%s_admin_/', Data::getPrefix()), '', $formArray['id']);
            echo Data::getEscapedHtml(
                Data::getGenericClass()->getTemplate('adminpage_button', $formArray)
            );
        }
    }

When this is done, you can keep working on the button itself.

  • No labels