You are here:
  1. Home
  2. Blog
  3. WordPress Custom General Options

WordPress Custom General Options

 | Code Snippet code snippet PHP
class jtThemeAdmin {

	public $settings =  array(
		array(
			'name' => 'jt-twitter',
			'label' => 'Twitter URL'
		),
		array(
			'name' => 'jt-linkedin',
			'label' => 'Linkedin URL'
		)
	);
	
	public function __construct() {
		add_action( 'admin_init', array( $this, 'register_custom_settings' ) );
		add_action( 'admin_init',  array( $this, 'register_setting_fields' ) );
	}
	
	public function register_custom_settings( ) {
		foreach( $this->settings as $setting ) {
			register_setting( 'general', $setting['name'] );
		}
	}
	
	public function register_setting_fields( ) {
		foreach( $this->settings as $setting ) {
			add_settings_field(
				$setting['name'] .'_setting-id',
				$setting['label'],
				array( $this, 'custom_settings_callback_function' ),
				'general',
				'default',
				array( 'name' => $setting['name'], 'label_for' => $setting['label'] )
			);
		}
	}
	
	function custom_settings_callback_function( $args ) {
		$option = get_option( $args['name'] );
		echo '<input type="text" id="'. $args['name'] .'" name="'. $args['name'] .'" value="' . $option . '" class="regular-text ltr" />';
	}
	
}
$jtThemesAdmin = new jtThemeAdmin();
Jem Turner jem@jemjabella.co.uk +44(0)7521056376

Follow on Instagram