plugin_name = $plugin_name; $this->version = $version; } /** * This function introduces the theme options into the 'Appearance' menu and into a top-level * 'WPPB Demo' menu. */ public function setup_plugin_options_menu() { //Add the menu to the Plugins set of menu items add_plugins_page( 'WPPB Demo Options', // The title to be displayed in the browser window for this page. 'WPPB Demo Options', // The text to be displayed for this menu item 'manage_options', // Which type of users can see this menu item 'wppb_demo_options', // The unique ID - that is, the slug - for this menu item array( $this, 'render_settings_page_content') // The name of the function to call when rendering this menu's page ); } /** * Provides default values for the Display Options. * * @return array */ public function default_display_options() { $defaults = array( 'show_header' => '', 'show_content' => '', 'show_footer' => '', ); return $defaults; } /** * Provide default values for the Social Options. * * @return array */ public function default_social_options() { $defaults = array( 'twitter' => 'twitter', 'facebook' => '', 'googleplus' => '', ); return $defaults; } /** * Provides default values for the Input Options. * * @return array */ public function default_input_options() { $defaults = array( 'input_example' => 'default input example', 'textarea_example' => '', 'checkbox_example' => '', 'radio_example' => '2', 'time_options' => 'default' ); return $defaults; } /** * Renders a simple page to display for the theme menu defined above. */ public function render_settings_page_content( $active_tab = '' ) { ?>
' . __( 'Select which areas of content you wish to display.', 'wppb-demo-plugin' ) . ''; } // end general_options_callback /** * This function provides a simple description for the Social Options page. * * It's called from the 'wppb-demo_theme_initialize_social_options' function by being passed as a parameter * in the add_settings_section function. */ public function social_options_callback() { $options = get_option('wppb_demo_social_options'); var_dump($options); echo '' . __( 'Provide the URL to the social networks you\'d like to display.', 'wppb-demo-plugin' ) . '
'; } // end general_options_callback /** * This function provides a simple description for the Input Examples page. * * It's called from the 'wppb-demo_theme_initialize_input_examples_options' function by being passed as a parameter * in the add_settings_section function. */ public function input_examples_callback() { $options = get_option('wppb_demo_input_examples'); var_dump($options); echo '' . __( 'Provides examples of the five basic element types.', 'wppb-demo-plugin' ) . '
'; } // end general_options_callback /** * Initializes the theme's display options page by registering the Sections, * Fields, and Settings. * * This function is registered with the 'admin_init' hook. */ public function initialize_display_options() { // If the theme options don't exist, create them. if( false == get_option( 'wppb_demo_display_options' ) ) { $default_array = $this->default_display_options(); add_option( 'wppb_demo_display_options', $default_array ); } add_settings_section( 'general_settings_section', // ID used to identify this section and with which to register options __( 'Display Options', 'wppb-demo-plugin' ), // Title to be displayed on the administration page array( $this, 'general_options_callback'), // Callback used to render the description of the section 'wppb_demo_display_options' // Page on which to add this section of options ); // Next, we'll introduce the fields for toggling the visibility of content elements. add_settings_field( 'show_header', // ID used to identify the field throughout the theme __( 'Header', 'wppb-demo-plugin' ), // The label to the left of the option interface element array( $this, 'toggle_header_callback'), // The name of the function responsible for rendering the option interface 'wppb_demo_display_options', // The page on which this option will be displayed 'general_settings_section', // The name of the section to which this field belongs array( // The array of arguments to pass to the callback. In this case, just a description. __( 'Activate this setting to display the header.', 'wppb-demo-plugin' ), ) ); add_settings_field( 'show_content', __( 'Content', 'wppb-demo-plugin' ), array( $this, 'toggle_content_callback'), 'wppb_demo_display_options', 'general_settings_section', array( __( 'Activate this setting to display the content.', 'wppb-demo-plugin' ), ) ); add_settings_field( 'show_footer', __( 'Footer', 'wppb-demo-plugin' ), array( $this, 'toggle_footer_callback'), 'wppb_demo_display_options', 'general_settings_section', array( __( 'Activate this setting to display the footer.', 'wppb-demo-plugin' ), ) ); // Finally, we register the fields with WordPress register_setting( 'wppb_demo_display_options', 'wppb_demo_display_options' ); } // end wppb-demo_initialize_theme_options /** * Initializes the theme's social options by registering the Sections, * Fields, and Settings. * * This function is registered with the 'admin_init' hook. */ public function initialize_social_options() { delete_option('wppb_demo_social_options'); if( false == get_option( 'wppb_demo_social_options' ) ) { $default_array = $this->default_social_options(); update_option( 'wppb_demo_social_options', $default_array ); } // end if add_settings_section( 'social_settings_section', // ID used to identify this section and with which to register options __( 'Social Options', 'wppb-demo-plugin' ), // Title to be displayed on the administration page array( $this, 'social_options_callback'), // Callback used to render the description of the section 'wppb_demo_social_options' // Page on which to add this section of options ); add_settings_field( 'twitter', 'Twitter', array( $this, 'twitter_callback'), 'wppb_demo_social_options', 'social_settings_section' ); add_settings_field( 'facebook', 'Facebook', array( $this, 'facebook_callback'), 'wppb_demo_social_options', 'social_settings_section' ); add_settings_field( 'googleplus', 'Google+', array( $this, 'googleplus_callback'), 'wppb_demo_social_options', 'social_settings_section' ); register_setting( 'wppb_demo_social_options', 'wppb_demo_social_options', array( $this, 'sanitize_social_options') ); } /** * Initializes the theme's input example by registering the Sections, * Fields, and Settings. This particular group of options is used to demonstration * validation and sanitization. * * This function is registered with the 'admin_init' hook. */ public function initialize_input_examples() { //delete_option('wppb_demo_input_examples'); if( false == get_option( 'wppb_demo_input_examples' ) ) { $default_array = $this->default_input_options(); update_option( 'wppb_demo_input_examples', $default_array ); } // end if add_settings_section( 'input_examples_section', __( 'Input Examples', 'wppb-demo-plugin' ), array( $this, 'input_examples_callback'), 'wppb_demo_input_examples' ); add_settings_field( 'Input Element', __( 'Input Element', 'wppb-demo-plugin' ), array( $this, 'input_element_callback'), 'wppb_demo_input_examples', 'input_examples_section' ); add_settings_field( 'Textarea Element', __( 'Textarea Element', 'wppb-demo-plugin' ), array( $this, 'textarea_element_callback'), 'wppb_demo_input_examples', 'input_examples_section' ); add_settings_field( 'Checkbox Element', __( 'Checkbox Element', 'wppb-demo-plugin' ), array( $this, 'checkbox_element_callback'), 'wppb_demo_input_examples', 'input_examples_section' ); add_settings_field( 'Radio Button Elements', __( 'Radio Button Elements', 'wppb-demo-plugin' ), array( $this, 'radio_element_callback'), 'wppb_demo_input_examples', 'input_examples_section' ); add_settings_field( 'Select Element', __( 'Select Element', 'wppb-demo-plugin' ), array( $this, 'select_element_callback'), 'wppb_demo_input_examples', 'input_examples_section' ); register_setting( 'wppb_demo_input_examples', 'wppb_demo_input_examples', array( $this, 'validate_input_examples') ); } /** * This function renders the interface elements for toggling the visibility of the header element. * * It accepts an array or arguments and expects the first element in the array to be the description * to be displayed next to the checkbox. */ public function toggle_header_callback($args) { // First, we read the options collection $options = get_option('wppb_demo_display_options'); // Next, we update the name attribute to access this element's ID in the context of the display options array // We also access the show_header element of the options collection in the call to the checked() helper function $html = ''; // Here, we'll take the first argument of the array and add it to a label next to the checkbox $html .= ''; echo $html; } // end toggle_header_callback public function toggle_content_callback($args) { $options = get_option('wppb_demo_display_options'); $html = ''; $html .= ''; echo $html; } // end toggle_content_callback public function toggle_footer_callback($args) { $options = get_option('wppb_demo_display_options'); $html = ''; $html .= ''; echo $html; } // end toggle_footer_callback public function twitter_callback() { // First, we read the social options collection $options = get_option( 'wppb_demo_social_options' ); // Next, we need to make sure the element is defined in the options. If not, we'll set an empty string. $url = ''; if( isset( $options['twitter'] ) ) { $url = esc_url( $options['twitter'] ); } // end if // Render the output echo ''; } // end twitter_callback public function facebook_callback() { $options = get_option( 'wppb_demo_social_options' ); $url = ''; if( isset( $options['facebook'] ) ) { $url = esc_url( $options['facebook'] ); } // end if // Render the output echo ''; } // end facebook_callback public function googleplus_callback() { $options = get_option( 'wppb_demo_social_options' ); $url = ''; if( isset( $options['googleplus'] ) ) { $url = esc_url( $options['googleplus'] ); } // end if // Render the output echo ''; } // end googleplus_callback public function input_element_callback() { $options = get_option( 'wppb_demo_input_examples' ); // Render the output echo ''; } // end input_element_callback public function textarea_element_callback() { $options = get_option( 'wppb_demo_input_examples' ); // Render the output echo ''; } // end textarea_element_callback public function checkbox_element_callback() { $options = get_option( 'wppb_demo_input_examples' ); $html = ''; $html .= ' '; $html .= ''; echo $html; } // end checkbox_element_callback public function radio_element_callback() { $options = get_option( 'wppb_demo_input_examples' ); $html = ''; $html .= ' '; $html .= ''; $html .= ' '; $html .= ''; $html .= ' '; $html .= ''; echo $html; } // end radio_element_callback public function select_element_callback() { $options = get_option( 'wppb_demo_input_examples' ); $html = ''; echo $html; } // end select_element_callback /** * Sanitization callback for the social options. Since each of the social options are text inputs, * this function loops through the incoming option and strips all tags and slashes from the value * before serializing it. * * @params $input The unsanitized collection of options. * * @returns The collection of sanitized values. */ public function sanitize_social_options( $input ) { // Define the array for the updated options $output = array(); // Loop through each of the options sanitizing the data foreach( $input as $key => $val ) { if( isset ( $input[$key] ) ) { $output[$key] = esc_url_raw( strip_tags( stripslashes( $input[$key] ) ) ); } // end if } // end foreach // Return the new collection return apply_filters( 'sanitize_social_options', $output, $input ); } // end sanitize_social_options public function validate_input_examples( $input ) { // Create our array for storing the validated options $output = array(); // Loop through each of the incoming options foreach( $input as $key => $value ) { // Check to see if the current option has a value. If so, process it. if( isset( $input[$key] ) ) { // Strip all HTML and PHP tags and properly handle quoted strings $output[$key] = strip_tags( stripslashes( $input[ $key ] ) ); } // end if } // end foreach // Return the array processing any additional functions filtered by this action return apply_filters( 'validate_input_examples', $output, $input ); } // end validate_input_examples }