Skip to content

Instantly share code, notes, and snippets.

@gmutschler
Created August 20, 2014 09:49
Show Gist options
  • Save gmutschler/f13f1d26abc2186ed0d4 to your computer and use it in GitHub Desktop.
Save gmutschler/f13f1d26abc2186ed0d4 to your computer and use it in GitHub Desktop.
<?php
// WARNING STILL NEED TO SANITIZE
class MyContactPage {
/** Holds the values to be used in the fields callbacks */
private $options;
private $my_option_name = 'contact_infos';
private $my_option_admin_slug = 'contact-informations';
/** Automatically calculated variable*/
private $option_group = 'contact_infos_options_group_1';
private $admin_page_handle = 'contact-informations-admin-page';
/** Start up */
public function __construct() {
add_action( 'admin_menu', array( $this, 'add_option_page' ) );
add_action( 'admin_init', array( $this, 'page_init' ) );
}
/** Add options page */
public function add_option_page() {
// This page will appear as top level menu item in the admin
// To add it under "Settings" use 'add_options_page'
// To add it under any other top level item use 'add_menu_page'
add_menu_page(
_x('Contact Informations', 'theme-option'),
_x('Contact', 'theme-option'),
'edit_published_pages', //in this case editors can also access the page. To restrict it to admins replace 'with manage_options'
$this->my_option_admin_slug,
array( $this, 'create_option_page' ),
'dashicons-location-alt',
21
);
}
/** Options page callback */
public function create_option_page() {
// Set class property
$this->options = get_option( $this->my_option_name );
echo '<div class="wrap">';
echo '<h2>'._x('Contact Informations', 'theme-option').'</h2>';
echo '<form method="post" action="options.php">';
// This prints out all hidden setting fields
settings_fields( $this->option_group );
do_settings_sections( $this->admin_page_handle );
submit_button();
echo '</form>';
echo '</div>';
}
/** Register and add settings */
public function page_init() {
register_setting(
$this->option_group, // Option group (used for nonce and other security issues. Must be the same as setting_fields)
$this->my_option_name, // Option name
array( $this, 'sanitize' ) // Sanitize
);
add_settings_section(
'main_contact_infos', // ID
'Main Contact informations', // Title
array( $this, 'print_main_contact_info_desc' ), // Callback
$this->admin_page_handle // Page
);
$my_fields = array(
array(
'label' => 'Legal name',
'setting_section_id' => 'main_contact_infos',
'input_type' => 'text',
)
);
foreach ($my_fields as $input_field) {
$input_id = strtolower ( preg_replace('/[^\da-z]/i', '_', $input_field['label']) );
add_settings_field(
$input_id, // ID
__($input_field['label']), // Title
array( $this, 'render_field' ), // Callback
$this->admin_page_handle, // Page
$input_field['setting_section_id'], // Section
array('input_type'=>$input_field['input_type'], 'input_id' => $input_id)
);
}
}
/** Sanitize each setting field as needed */
public function sanitize( $input ) {
foreach ($input as $input_id => $value) {
$this->options[$input_id] = trim($value);
}
if( isset( $input['id_number'] ) )
$this->options['id_number'] = absint( trim($input['id_number']) );
return $this->options;
}
/** Print the Section text */
public function render_field( $args ) {
$output_value = isset($args[ 'input_id' ]) ? esc_attr($this->options['legal_name']) : '';
$input_type = isset($args[ 'input_type' ]) ? esc_attr($args[ 'input_type']) : 'text';
if ($input_type === 'textarea') {
printf(
'<textarea id="%s" name="%s[%s]">%s</textarea>',
$args[ 'input_id' ],
$this->my_option_name,
$args[ 'input_id' ],
$output_value
);
} else {
printf(
'<input type="%s" id="%s" name="%s[%s]" value="%s" />',
$input_type,
$args[ 'input_id' ],
$this->my_option_name,
$args[ 'input_id' ],
$output_value
);
}
}
public function print_main_contact_info_desc() {
echo '<p>'.__('Will be use to generate your contact page and tile (home screen).').'<br><em>'.__('Contact email will receive the messages sent through the contact form').'</em></p>';
}
/** Get the settings option array and print one of its values */
public function id_number_callback() {
$output_value = isset($this->options['id_number']) ? esc_attr($this->options['id_number']) : '';
printf(
'<input type="text" id="id_number" name="%s[id_number]" value="%s" class="regular-text" />',
$this->my_option_name,
$output_value
);
}
/** Get the settings option array and print one of its values */
public function title_callback() {
$output_value = isset($this->options['title']) ? esc_attr($this->options['title']) : '';
printf(
'<input type="text" id="title" name="%s[title]" value="%s" />',
$this->my_option_name,
$output_value
);
}
}
if( is_admin() )
$my_settings_page = new MyContactPage();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment