PHP code example of gebruederheitz / wp-easy-customizer
1. Go to this page and download the library: Download gebruederheitz/wp-easy-customizer library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gebruederheitz / wp-easy-customizer example snippets
# functions.php (or controller class)
use Gebruederheitz\Wordpress\Customizer\CustomizerPanel;
// If your settings handler implement their getters as static methods, you will
// only need to instantiate the whole bunch on the customizer page:
if (is_customize_preview()) {
// Set up a new customizer panel with the title 'My Settings'
// It will also 'clean up' the Customizer by removing some panels (see below)
// The ID is automatically prefixed with 'ghwp_customizer_panel_'.
$panel = new CustomizerPanel('my_main_panel', 'My Settings');
use Gebruederheitz\Wordpress\Customizer\CustomizerPanel;
$panel = new CustomizerPanel('my_main_panel')
$panel->addNewSection(
// A unique ID for the section
'my_general_settings',
// The title of the section / panel
'General Settings',
// An optional description shown on the top of the open panel
null,
// Some settings, more information below
[
CompanyInformation::get(),
]
)->addNewSection(/* This method can be chained */);
use Gebruederheitz\Wordpress\Customizer\CustomizerPanel;
use Gebruederheitz\Wordpress\Customizer\CustomizerSection;
$panel = new CustomizerPanel('my_main_panel')
$panel->addsSections(
new CustomizerSection(
'my_general_settings',
'General Settings',
null,
[ CompanyInformation::get() ]
),
// We could add more sections here if we wanted
);
use Gebruederheitz\Wordpress\Customizer\CustomizerSection;
$panelId = $panel->getId();
$section = new CustomizerSection(
'ghwp_general_settings',
'General Settings',
null,
[ CompanyInformation::get() ]
);
// We can associate the section with a panel in two ways:
// Using the actual panel instance directly...
$section->setPanel($panel);
// ...or using its ID.
$section->setPanel($panelId);
// You can also create a section without any handlers and then add them
// later:
$consentManagementSection = new CustomizerSection(
'ghwp_consent_management_settings',
'Consent Management'
);
// Associate with the panel one way...
$consentManagementSection->setPanel($panel);
// ...or another
$panel->addSections($consentManagementSection);
// Add hanlders retroactively
$consentManagementSection->addSettings(ConsentManagementEnabled::get());
$consentManagementSection->addSettings(
OtherConsentManagementSetting::get(),
ExtendedConsentManagementSetting::get(),
);
use \Gebruederheitz\Wordpress\Customizer\BasicCustomizerSetting;
class TelephoneNo extends BasicCustomizerSetting
{
// These two methods are abstract in BasicCustomizerSetting, so your IDE
// will conveniently create stubs for you
public function getKey(): string
{
// A unique key for the option's database entry
return 'prefix_company_info_telephone';
}
public function getLabel(): string
{
// The input label shown to the user
return 'Telephone No';
}
}
$section = new CustomizerSection(
$slug,
$label,
$description,
[
TelephoneNo::get(),
/* ... more settings, if you like ... */
]
);
$section->addSettings(TelephoneNo::get(), OtherSetting::get());
# Somewhere in your code, like templates, action handlers, controllers, hook
# callbacks etc.
use My\TelephoneNo;
$phoneNumber = TelephoneNo::get()->getValue();
// or you can use our little shortcut with any BasicCustomizerSetting:
$phoneNumber = TelephoneNo::value();
use Gebruederheitz\Wordpress\Customizer\BasicCustomizerSetting;
/**
* A setting with an alternative input type and explicit default value. This
* example would of course be simpler to implement using the
* CheckboxCustomizerSetting, as described below.
*/
class ShowAddress extends BasicCustomizerSetting
{
public function getKey() {
return 'prefix_company_info_show_address';
}
public function getLabel() {
return 'Show address in footer';
}
// Optional: default value, defaults to ''
protected $default = false;
// Optional: input type
protected ?string $inputType = 'checkbox';
}
/**
* A setting with an alternative sanitizer
*/
class StreetAddress extends BasicCustomizerSetting
{
public function getKey() {
return 'prefix_company_info_street_address';
}
public function getLabel() {
return 'Street address';
}
// A "callable-string" for a function that will receive the raw value and
// return a sanitized value.
protected ?string $sanitizer = 'sanitize_text_field';
}
/**
* A setting using a select field
*/
class SupportIcon extends BasicCustomizerSetting
{
/* ... key and label */
protected ?string $sanitizer = 'sanitize_text_field';
protected ?string $inputType = 'select';
public function getOptions: ?array
{
return [
ExampleSelectValues::FIRST => 'Label for first option',
ExampleSelectValues::SECOND => 'Label for the second option',
];
}
/**
* A setting for selecting a page from the current site, which is only visible
* if the switch "ShowAddress" is active.
*/
class ContactPage extends BasicCustomizerSetting
{
// ... key and label ...
protected static $inputType = 'dropdown-pages';
public function getActiveCallback() : ?callable
{
// Using an anonymous callback
return function () {
$showAddress = ShowAddress::get();
return CustomizerSettings::getValue(
$showAddress->getKey(),
$showAddress->getDefault()
);
}
// Using a class method
return [ShowAddress::get(), 'getValue'] // getValue() has a default implementation in BasicCustomizerSetting
}
}
class MyCheckbox extends \Gebruederheitz\Wordpress\Customizer\InputTypes\CheckboxCustomizerSetting
{
public function getKey(): string {
return 'my-checkbox';
}
public function getLabel(): string {
return 'My Checkbox';
}
/* The values below are already set on CheckboxCustomizerSetting: */
// protected ?string $inputType = 'checkbox';
// protected $default = false;
}
class MyUrlField extends \Gebruederheitz\Wordpress\Customizer\InputTypes\UrlCustomizerSetting
{
public function getKey(): string {
return 'my-url-field';
}
public function getLabel(): string {
return 'My URL';
}
/* Already set: */
// protected ?string $inputType = 'url';
// protected ?string $sanitizer = 'sanitize_url';
}
class MyTextField extends \Gebruederheitz\Wordpress\Customizer\InputTypes\TextCustomizerSetting
{
public function getKey(): string {
return 'my-text';
}
public function getLabel(): string {
return 'My Text';
}
/* Already set: */
// protected ?string $sanitizer = 'sanitize_text_field';
}
use Gebruederheitz\Wordpress\Customizer\CustomizerSettings;
add_filter(
CustomizerSettings::HOOK_FILTER_DECLUTTER_ITEMS,
function ($items) {
unset($items['panels']['themes']);
unset($items['sections']['static_front_page']);
unset($items['sections']['custom_css']);
unset($items['controls']['custom_logo']);
unset($items['controls']['site_icon']);
return $items;
// To not "declutter" at all simply
return [];
}
);
use Gebruederheitz\Wordpress\Customizer\InputTypes\SeparatorSetting;
$settings = [
// A plain horizontal rule with 2em vertical margin
SeparatorSetting::factory('some-unique-id-for-this-separator'),
TelephoneNo::get(),
// With custom margin of 3em
SeparatorSetting::factory(
'sep-with-custom-margin',
null,
[
'margin' => 3,
]
),
// with a heading in the default color #08d
SeparatorSetting::factory(
'sep-general-settings',
__('General Settings', 'namespace')
),
// with heading in a custom color
SeparatorSetting::factory(
'some-unique-id-for-this-separator',
'Heading',
[
'color' => 'hotpink',
]
),
// with heading in a custom color and custom margin
// hr bottom margin is calc(${customMargin}em + 2em) to compensate for
// the heading's margin collapsing
SeparatorSetting::factory(
'some-unique-id-for-this-separator',
'Heading'
[
'color' => 'hotpink',
'margin' => 1,
]
),
];
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.