harmonic design.

HDFields Documentation

HDFields is a WordPress settings and form management system that forces clean and intuitive design, while maintaining strict adherence to WordPress core standards and security.

Creating a Settings form with HDFields

The best way to create a settings page or form with HDFields is to start by creating a new class. If you are creating a settings page, then something like _hd_settings would be a good class name, replacing hd with a relevant prefix for your project.

Inside the constructor __construct, I like to call a method fields that returns and sets a public array of the form fields and their structure. I usually write the form fields in JSON format, then parse as an array for easier writing.

The form can then be displayed with a display method, inside of which checks for user permissions, enqueues the needed styles and scripts for HDFields (hdfields/style.css and hdfields/script.js), and then finally calls the HDFields display method.

Example code:

$fields = new _hd_fields($fields, $savedData);
echo $fields->display();

Saving Data

To save data, start by passing the fields and data to the _hd_fields class. You can then run the get_values method (passing false as a parameter) to get clean, sanitized data back and ready to save however you need (such as update_meta, or update_option). It is also a good idea to check for user permissions before saving any data.

Example code:

$data = new _hd_fields($fields, $dataToSave);
$data = $data->get_values(false);
update_option("hdq_settings", $data);

Leave a reply

👍 😆 😠 😢 😍
Reply