Options Page
class Custom_Settings_Page {
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
add_action( 'admin_init', array( $this, 'init_settings' ) );
}
public function add_admin_menu() {
add_options_page(
esc_html__( 'Theme Options', 'text_domain' ),
esc_html__( 'Theme Options', 'text_domain' ),
'manage_options',
'theme-options',
array( $this, 'page_layout' )
);
}
public function init_settings() {
register_setting(
'settings_group',
'settings_name'
);
add_settings_section(
'settings_name_section',
'',
false,
'settings_name'
);
add_settings_field(
'logo_url',
__( 'Logo URL', 'text_domain' ),
array( $this, 'render_logo_url_field' ),
'settings_name',
'settings_name_section'
);
add_settings_field(
'logo_alt',
__( 'Logo Alt Text', 'text_domain' ),
array( $this, 'render_logo_alt_field' ),
'settings_name',
'settings_name_section'
);
add_settings_field(
'favicon_url',
__( 'Favicon URL', 'text_domain' ),
array( $this, 'render_favicon_url_field' ),
'settings_name',
'settings_name_section'
);
add_settings_field(
'apple_touch_16_url',
__( 'Apple touch icon 16x16 URL', 'text_domain' ),
array( $this, 'render_apple_touch_16_url_field' ),
'settings_name',
'settings_name_section'
);
add_settings_field(
'apple_touch_72_url',
__( 'Apple touch icon 72x72 URL', 'text_domain' ),
array( $this, 'render_apple_touch_72_url_field' ),
'settings_name',
'settings_name_section'
);
add_settings_field(
'apple_touch_144_url',
__( 'Apple touch icon 144x144 URL', 'text_domain' ),
array( $this, 'render_apple_touch_144_url_field' ),
'settings_name',
'settings_name_section'
);
add_settings_field(
'mobile_menu_logo_url',
__( 'Mobile Menu Logo URL', 'text_domain' ),
array( $this, 'render_mobile_menu_logo_url_field' ),
'settings_name',
'settings_name_section'
);
add_settings_field(
'mobile_menu_logo_alt',
__( 'Mobile Menu Logo ALT', 'text_domain' ),
array( $this, 'render_mobile_menu_logo_alt_field' ),
'settings_name',
'settings_name_section'
);
add_settings_field(
'mobile_footer_txt',
__( 'Mobile Menu Footer Text', 'text_domain' ),
array( $this, 'render_mobile_footer_txt_field' ),
'settings_name',
'settings_name_section'
);
}
public function page_layout() {
// Check required user capability
if ( !current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'text_domain' ) );
}
// Admin Page Layout
echo '<div class="wrap">' . "n";
echo ' <h1>' . get_admin_page_title() . '</h1>' . "n";
echo ' <form action="options.php" method="post">' . "n";
settings_fields( 'settings_group' );
do_settings_sections( 'settings_name' );
submit_button();
echo ' </form>' . "n";
echo '</div>' . "n";
}
function render_logo_url_field() {
// Retrieve data from the database.
$options = get_option( 'settings_name' );
// Set default value.
$value = isset( $options['logo_url'] ) ? $options['logo_url'] : '';
// Field output.
echo '<input type="url" name="settings_name[logo_url]" class="regular-text logo_url_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $value ) . '">';
}
function render_logo_alt_field() {
// Retrieve data from the database.
$options = get_option( 'settings_name' );
// Set default value.
$value = isset( $options['logo_alt'] ) ? $options['logo_alt'] : '';
// Field output.
echo '<input type="text" name="settings_name[logo_alt]" class="regular-text logo_alt_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $value ) . '">';
}
function render_favicon_url_field() {
// Retrieve data from the database.
$options = get_option( 'settings_name' );
// Set default value.
$value = isset( $options['favicon_url'] ) ? $options['favicon_url'] : '';
// Field output.
echo '<input type="url" name="settings_name[favicon_url]" class="regular-text favicon_url_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $value ) . '">';
}
function render_apple_touch_16_url_field() {
// Retrieve data from the database.
$options = get_option( 'settings_name' );
// Set default value.
$value = isset( $options['apple_touch_16_url'] ) ? $options['apple_touch_16_url'] : '';
// Field output.
echo '<input type="url" name="settings_name[apple_touch_16_url]" class="regular-text apple_touch_16_url_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $value ) . '">';
}
function render_apple_touch_72_url_field() {
// Retrieve data from the database.
$options = get_option( 'settings_name' );
// Set default value.
$value = isset( $options['apple_touch_72_url'] ) ? $options['apple_touch_72_url'] : '';
// Field output.
echo '<input type="url" name="settings_name[apple_touch_72_url]" class="regular-text apple_touch_72_url_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $value ) . '">';
}
function render_apple_touch_144_url_field() {
// Retrieve data from the database.
$options = get_option( 'settings_name' );
// Set default value.
$value = isset( $options['apple_touch_144_url'] ) ? $options['apple_touch_144_url'] : '';
// Field output.
echo '<input type="url" name="settings_name[apple_touch_144_url]" class="regular-text apple_touch_144_url_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $value ) . '">';
}
function render_mobile_menu_logo_url_field() {
// Retrieve data from the database.
$options = get_option( 'settings_name' );
// Set default value.
$value = isset( $options['mobile_menu_logo_url'] ) ? $options['mobile_menu_logo_url'] : '';
// Field output.
echo '<input type="url" name="settings_name[mobile_menu_logo_url]" class="regular-text mobile_menu_logo_url_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $value ) . '">';
}
function render_mobile_menu_logo_alt_field() {
// Retrieve data from the database.
$options = get_option( 'settings_name' );
// Set default value.
$value = isset( $options['mobile_menu_logo_alt'] ) ? $options['mobile_menu_logo_alt'] : '';
// Field output.
echo '<input type="text" name="settings_name[mobile_menu_logo_alt]" class="regular-text mobile_menu_logo_alt_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '" value="' . esc_attr( $value ) . '">';
}
function render_mobile_footer_txt_field() {
// Retrieve data from the database.
$options = get_option( 'settings_name' );
// Set default value.
$value = isset( $options['mobile_footer_txt'] ) ? $options['mobile_footer_txt'] : '';
// Field output.
echo '<textarea name="settings_name[mobile_footer_txt]" class="regular-text mobile_footer_txt_field" placeholder="' . esc_attr__( '', 'text_domain' ) . '">' . $value . '</textarea>';
}
}
new Custom_Settings_Page;