-
Notifications
You must be signed in to change notification settings - Fork 10
Component Theme_Settings
Each theme has some options to be set like social links, footer text or 404 page settings.
If you use Advanced Custom Fields PRO plugin (costs 69 AUD) - you can use ACF interface to create all settings you need. However if you don't want to pay any $$ for this feature, you use another plugin to create your settings page.
We prefer to use Titan Framework to build custom admin pages.
For faster start we use small class object which simplify usage of Titan Framework. To create new theme settings page you need to create a class similar to this:
<?php
namespace Boilerplate\Theme\Admin;
class Theme_Settings extends \JustCoded\WP\Framework\Admin\Theme_Settings {
/**
* Create admin page for theme settings
*/
public function init() {
$panel = static::titan_instance()->createContainer( array(
'type' => 'admin-page',
'name' => 'Theme Options',
) );
$this->register_general_tab( $panel );
$this->register_404_tab( $panel );
}
/**
* Register fields for General tab
*
* @param \TitanFrameworkAdminPage $panel panel object to work with.
*/
protected function register_general_tab( $panel ) {
$tab = $panel->createTab( array(
'name' => 'General',
) );
$tab->createOption( array(
'name' => 'Footer ',
'type' => 'heading',
) );
$tab->createOption( array(
'name' => 'Copyright text',
'id' => 'copyright_text',
'type' => 'text',
'default' => '© ' . date( 'Y' ) . '. All rights reserved.',
) );
$tab->createOption( array(
'type' => 'save',
) );
}
/**
* Register fields for 404 tab.
*
* @param \TitanFrameworkAdminPage $panel panel object to work with.
*/
protected function register_404_tab( $panel ) {
$tab = $panel->createTab( array(
'name' => '404 Page',
) );
$tab->createOption( array(
'name' => 'Title',
'id' => '404_title',
'type' => 'text',
'default' => __( 'Oops! That page can’t be found.', 'boilerplate' ),
) );
$tab->createOption( array(
'name' => 'Content',
'id' => '404_content',
'type' => 'editor',
'default' => __( 'It looks like nothing was found at this location. Maybe try one of the links in menu or a search?', 'boilerplate' ),
) );
$tab->createOption( array(
'type' => 'save',
) );
}
}
For more information on fields and panel/tabs methods you can read on official documentation.
To register Theme Settings we create an instance inside functions.php
file:
functions.php
<?php
// ...
$settings = \Boilerplate\Theme\Admin\Theme_Settings::instance();
To get value from settings page you can simple use ThemeSettings::get()
method like this:
<?php
use Boilerplate\Theme\Admin\Theme_Settings;
// ...
echo esc_html( Theme_Settings::get( 'copyright_text' ) );
Property | Type | Description |
---|---|---|
$titan_instance protected, static |
\TitanFramework | Titan framework object |
Method | Description |
---|---|
__construct | Registers Titan Framework action tf_create_options
|
titan_instance public, static |
Makes Singleton TitanFramework instance and return it. |
init (a)tf_create_options |
Method to register a page through titan instance |
get public, static |
Get option value by key |