PHP code example of sergeliatko / wpsettings

1. Go to this page and download the library: Download sergeliatko/wpsettings 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/ */

    

sergeliatko / wpsettings example snippets




//make use of the class once in your file
use \SergeLiatko\WPSettings\Setting;
//then create setting like this
$my_option = new Setting( array(
	'option' => 'option_name_in_db',
	'label'  => __( 'My option label', 'my-text-domain' )
) );

add_action( 'admin_init', function () {
	register_setting(
		'general',
		'option_name_in_db',
		array(
			'sanitize_callback' => 'sanitize_text_field',
		)
	);
}, 10, 0 );

add_action( 'admin_menu', function () {
	add_settings_field(
		'option-name-in-db',
		__( 'My option label', 'my-text-domain' ),
		function() {
			printf(
				'<input type="%1$s" id="%2$s" name="%3$s" value="%4$s" class="%5$s">',
				'text',
				'option-name-in-db',
				'option_name_in_db',
				esc_attr( get_option( 'option_name_in_db', '' ) ),
				'regular-text code'
			);
		},
		'general',
		'default',
		array(
			'label_for' => 'option-name-in-db'
		)
	);
}, 10, 0 );


//...
//Load WPSettings Framework
th_to/form-fields/autoload.php' );


//...
//make use of the Setting class once in your file
use \SergeLiatko\WPSettings\Setting;

//...

//then create setting like this
$my_option = Setting::createInstance( array(
	'option' => 'option_name_in_db',
	'label'  => __( 'My option label', 'my-text-domain' )
) );
//...


//...
//make use of the Section class once in your file
use SergeLiatko\WPSettings\Section;

//...

//then create settings section like this
$my_section = Section::createInstance( array(
	'id'          => 'custom-section-id',
	'title'       => __( 'My section title', 'my-text-domain' ),
	'description' => __( 'This is section description text that appears above setting fields.', 'my-text-domain' ),
	'settings'    => array(
		array(
			'option' => 'option_1_name_in_db',
			'label'  => __( 'My option 1 label', 'my-text-domain' ),
		),
		array(
			'option' => 'option_2_name_in_db',
			'label'  => __( 'My option 2 label', 'my-text-domain' ),
		),
	),
) );
//...


//...
//make use of the Page class once in your file
use SergeLiatko\WPSettings\Page;

//...
//then create admin page like this
$my_section = Page::createInstance( array(
	'slug'     => 'my-admin-page',
	'label'    => __( 'My Admin Page', 'my-text-domain' ),
	'sections' => array(
		array(
			'id'          => 'default',
			'title'       => __( 'My section title', 'my-text-domain' ),
			'description' => __( 'In this section my setting fields will appear', 'my-text-domain' ),
			'settings'    => array(
				array(
					'option' => 'option_1_name_in_db',
					'label'  => __( 'My option 1 label', 'my-text-domain' ),
				),
				array(
					'option' => 'option_2_name_in_db',
					'label'  => __( 'My option 2 label', 'my-text-domain' ),
				),
			),
		),
	),
) );
//...

$my_option = Setting::createInstance( array(
	'_class' => '\\MyNameSpace\\MySettingExtension',
	'option' => 'option_name_in_db',
	'label'  => __( 'My option label', 'my-text-domain' )
) );