PHP code example of underpin / admin-page-loader

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

    

underpin / admin-page-loader example snippets


// Register the option to use on the settings page. See Underpin_Options\Abstracts\Option
underpin()->options()->add( 'example_admin_options', [
	'key'           => 'example_option', //  admin page
underpin()->admin_pages()->add( 'example-admin-page', [
	'page_title' => underpin()->__( 'Example Admin Page' ),
	'menu_title' => underpin()->__( 'Example' ),
	'capability' => 'administrator',
	'menu_slug'  => 'example-admin-page',
	'icon'       => 'dashicons-admin-site-alt',
	'position'   => 5,
	'sections'   => [
		[
			'id'          => 'primary-section',
			'name'        => underpin()->__( 'Primary Section' ),
			'options_key' => 'example_admin_options',
			'fields'      => [
				'test_setting' => [
					'class' => 'Underpin\Factories\Settings_Fields\Text',
					'args'  => [ underpin()->options()->pluck( 'example_admin_options', 'test_setting' ), [
						'name'        => 'test_setting',
						'description' => underpin()->__( 'Optional. Specify the person to say hello to. Default "world".' ),
						'label'       => underpin()->__( 'Name' ),
					] ],
				],
			],
		],
	],
] );



// Register the option to use on the settings page. See Underpin_Options\Abstracts\Option
underpin()->options()->add( 'example_admin_options', [
	'key'           => 'example_option', //  manged by Example Admin Page',
] );

// Register the admin page
underpin()->admin_pages()->add( 'example-admin-page', [
	'page_title' => underpin()->__( 'Example Admin Page' ),
	'menu_title' => underpin()->__( 'Example' ),
	'capability' => 'administrator',
	'menu_slug'  => 'example-admin-page',
	'icon'       => 'dashicons-admin-site-alt',
	'position'   => 5,
	'sections'   => [
		[
			'id'          => 'primary-section',
			'name'        => underpin()->__( 'Primary Section' ),
			'options_key' => 'example_admin_options',
			'fields'      => [
				'test_setting' => [
					'class' => 'Underpin\Factories\Settings_Fields\Text',
					'args'  => [ underpin()->options()->pluck( 'example_admin_options', 'test_setting' ), [
						'name'        => 'test_setting',
						'description' => underpin()->__( 'This is a description of this setting' ),
						'label'       => underpin()->__( 'Setting Name' ),
					] ],
				],
			],
		],
		[
			'id'          => 'secondary-section',
			'name'        => underpin()->__( 'Secondary Section' ),
			'options_key' => 'example_admin_options',
			'fields'      => [
				'test_setting' => [
					'class' => 'Underpin\Factories\Settings_Fields\Text',
					'args'  => [ underpin()->options()->pluck( 'example_admin_options', 'another_setting' ), [
						'name'        => 'another_setting',
						'description' => underpin()->__( 'This is a description of this setting' ),
						'label'       => underpin()->__( 'Secondary Setting Name' ),
					] ],
				],
			],
		],
	],
] );


// Register the option to use on the settings page. See Underpin_Options\Abstracts\Option
underpin()->options()->add( 'example_admin_options', [
	'key'           => 'example_option', //  manged by Example Admin Page',
] );

// Register the admin page
underpin()->admin_pages()->add( 'example-admin-page', [
	'page_title' => underpin()->__( 'Example Admin Page' ),
	'menu_title' => underpin()->__( 'Example' ),
	'capability' => 'administrator',
	'menu_slug'  => 'example-admin-page',
	'layout'     => 'tabs',
	'icon'       => 'dashicons-admin-site-alt',
	'position'   => 5,
	'sections'   => [
		[
			'id'          => 'primary-section',
			'name'        => underpin()->__( 'Primary Section' ),
			'options_key' => 'example_admin_options',
			'fields'      => [
				'test_setting' => [
					'class' => 'Underpin\Factories\Settings_Fields\Text',
					'args'  => [ underpin()->options()->pluck( 'example_admin_options', 'test_setting' ), [
						'name'        => 'test_setting',
						'description' => underpin()->__( 'This is a description of this setting' ),
						'label'       => underpin()->__( 'Setting Name' ),
					] ],
				],
			],
		],
		[
			'id'          => 'secondary-section',
			'name'        => underpin()->__( 'Secondary Section' ),
			'options_key' => 'example_admin_options',
			'fields'      => [
				'test_setting' => [
					'class' => 'Underpin\Factories\Settings_Fields\Text',
					'args'  => [ underpin()->options()->pluck( 'example_admin_options', 'another_setting' ), [
						'name'        => 'another_setting',
						'description' => underpin()->__( 'This is a description of this setting' ),
						'label'       => underpin()->__( 'Secondary Setting Name' ),
					] ],
				],
			],
		],
	],
] );

class Custom_Page extends \Underpin_Admin_Pages\Factories\Admin_Page_Instance{
  
  
	/**
	 * Fetches the valid templates and their visibility.
	 *
	 * override_visibility can be either "theme", "plugin", "public" or "private".
	 *  theme   - sets the template to only be override-able by a parent, or child theme.
	 *  plugin  - sets the template to only be override-able by another plugin.
	 *  public  - sets the template to be override-able anywhere.
	 *  private - sets the template to be non override-able.
	 *
	 * @since 1.0.0
	 *
	 * @return array of template properties keyed by the template name
	 */
	public function get_templates() {
	    if( 'custom-layout' === $this->layout ){
	      return [
	        'admin' => [ // This should match your entry file name for this template.
	           'override_visibility' => 'private' // Or whatever.
            ]
          ];
	    }
	    
	    // Fallback to default.
	    return parent::get_templates();
	}

	/**
	 * Fetches the template group name.
	 *
	 * @since 1.0.0
	 *
	 * @return string The template group name
	 */
	protected function get_template_group() {
	    if('custom-layout' === $this->layout){
	      return 'admin'; // Or whatever you want your subdirectory to be.
	    }
	    
	    // Fallback to default
		return parent::get_template_group();
	}


	/**
	 * @inheritDoc
	 */
	protected function get_template_root_path() {
	    if('custom-layout' === $this->layout){
	      return 'custom/root/path';
	    }
	    
	    // Fallback to default
		return parent::get_template_root_path();
	}

}


// Register the admin page
underpin()->admin_pages()->add( 'example-admin-page', [
	'class' => 'Custom_Page', // Name of the class to use
	'args'  => [ // Arguments use to set up this instance.
		'page_title' => underpin()->__( 'Example Admin Page' ),
		'menu_title' => underpin()->__( 'Example' ),
		'capability' => 'administrator',
		'menu_slug'  => 'example-admin-page',
		'layout'     => 'tabs',
		'icon'       => 'dashicons-admin-site-alt',
		'position'   => 5,
		'sections'   => [
			[
				'id'          => 'primary-section',
				'name'        => underpin()->__( 'Primary Section' ),
				'options_key' => 'example_admin_options',
				'fields'      => [
					'test_setting' => [
						'class' => 'Underpin\Factories\Settings_Fields\Text',
						'args'  => [ underpin()->options()->pluck( 'example_admin_options', 'test_setting' ), [
							'name'        => 'test_setting',
							'description' => underpin()->__( 'This is a description of this setting' ),
							'label'       => underpin()->__( 'Setting Name' ),
						] ],
					],
				],
			],
			[
				'id'          => 'secondary-section',
				'name'        => underpin()->__( 'Secondary Section' ),
				'options_key' => 'example_admin_options',
				'fields'      => [
					'test_setting' => [
						'class' => 'Underpin\Factories\Settings_Fields\Text',
						'args'  => [ underpin()->options()->pluck( 'example_admin_options', 'another_setting' ), [
							'name'        => 'another_setting',
							'description' => underpin()->__( 'This is a description of this setting' ),
							'label'       => underpin()->__( 'Secondary Setting Name' ),
						] ],
					],
				],
			],
		],
	],
] );