youniwemi/wp-settings-kit

WP Settings Kit is a lightweight library for easily creating WordPress settings pages and Post metaboxes.

1.1.4 2024-03-15 18:09 UTC

This package is auto-updated.

Last update: 2024-04-15 18:18:01 UTC


README

Tweet for help GitHub stars GitHub followers — ☝️ Make sure you ⭐ and 👀 this repository!

Ever wanted to build custom settings (or metaboxes) inside your WordPress plugin or theme and didn't like the non-DRY approach for creating custom settings and metaboxes via WordPress API? Well, this package is an attempt to fix this. 🎊

Screenshots

COMPOSER INSTALL

  • You can install the library using composer
composer require youniwemi/wp-settings-kit
  • You'll be able to use WP_Settings_Kit class after requiring vendor/autoload.php

USAGE

USAGE For Setting Page

  • Prepare an array of options then instanciate WP_Settings_Kit
$options = 
[
    'name' => 'MY_AWESOME_FEATURE',
    'title' => 'My Awesome Feature',
    'description' => "My Awesome Feature description",
    'side_panel' => "<p>A side Panel content : to add some explanations if necessary</p>",
    'fields' => [
        [
            'id' => 'ACTIVE',
            'type' => 'checkbox',
            'title' => 'The feature is active' ,
        ],
        [
            'id' => 'FIRST_SETTING',
            'type' => 'number',
            'title' => 'First setting' ,
            'default' => 0 ,
            // This setting will be included only if the first checkbox is checked
            'show_if' => function(){ return defined('MY_AWESOME_FEATURE_ACTIVE') && MY_AWESOME_FEATURE_ACTIVE == 'on'; }
        ]
    ]
];
$setting = new WP_Settings_Kit($options);
  • Once the options are saved, constants MY_AWESOME_FEATURE_ACTIVE will be available and will be able to set the first setting MY_AWESOME_FEATURE_FIRST_SETTING

USAGE For Post Metabox

  • Prepare an array of options as well as the metabox definition then instanciate WP_Settings_Kit
$options = 
[
    'name' => 'MY_AWESOME_FEATURE',
    'title' => 'My Awesome Feature',
    'fields' => [
        [
            'id' => 'ACTIVE',
            'type' => 'checkbox',
            'title' => 'The feature is active' ,
        ],
        [
            'id' => 'FIRST_SETTING',
            'type' => 'number',
            'title' => 'First setting' ,
            'default' => 0 ,
            // This field will be included only if the first checkbox is checked
            'show_if' => function(){ return defined('MY_AWESOME_FEATURE_ACTIVE') && MY_AWESOME_FEATURE_ACTIVE == 'on'; }
        ]
    ]
];
$metabox = [
    'id' => 'my_metabox',
    'title' => 'My Awesome Metabox',
    'post_types' => ['post'], // Post types to display meta box
    'context' => 'advanced',
    'priority' => 'default',
];
$metabox = new WP_Settings_Kit($options , $metabox);
  • Once the metabox is saved, fields will be saved as post metas : MY_AWESOME_FEATURE_FIRST_ACTIVE and MY_AWESOME_FEATURE_FIRST_SETTING

TODO:

  • Basic Settings Page
  • Tabs on Settings Page with JS
  • Tabs on Settings Page with JS
  • Documentation for code workflow
  • Create Field: text
  • Create Field: textarea
  • Create Field: url
  • Create Field: number
  • Create Field: checkbox
  • Create Field: multicheck
  • Create Field: radio
  • Create Field: select
  • Create Field: html
  • Create Field: wysiwyg
  • Create Field: file
  • Create Field: image
  • Create Field: password
  • Create Field: color
  • Create Field: email
  • Create Field: date
  • Create Field (generated content with callback): content
  • Create Field: range
  • Support for post metabox
  • Re-factor the code with WP Standards
  • Tutorials
  • Blog post
  • Documentation

License

Release under GNU GPL v2.0

Credits

This package is a fork of https://github.com/ahmadawais/WP-OOP-Settings-API based on the work of @tareq1988 https://github.com/tareq1988/wordpress-settings-api-class

@AhmadAwais, @deviorobert, @MaedahBatool AND @WordPress, @tareq1988, @royboy789, @twigpress, @rahal.

🙌 Youniwemi

This open source fork is maintained by the help of awesome businesses listed below :

This package is used in the following wordpress plugins



For anything else, tweet at @rahalaboulfeth