Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //b4b Theme Options Panel - by Connor
- /*------------Field Argument Formats------------
- img upload args = 'id', 'desc'
- input args = 'id', 'desc'
- textarea args = 'id', 'rows', 'cols', 'desc'
- checkbox args = 'id','desc'
- select args = 'opt1', 'opt2', 'opt3', 'opt#...', 'id', 'desc'
- ----------------------------------------------*/
- function my_admin_scripts() {
- wp_enqueue_media();
- wp_register_style('options-style', get_template_directory_uri() . '/options-style.css');
- wp_enqueue_style('options-style');
- wp_register_script('b4b-upload', get_template_directory_uri() . '/js/b4b-upload.js', array('jquery'));
- wp_register_script('b4b-options', get_template_directory_uri() . '/js/b4b-options.js', array('jquery'));
- wp_enqueue_script('b4b-upload');
- wp_enqueue_script('b4b-options');
- }
- add_action('admin_enqueue_scripts', 'my_admin_scripts');
- function b4b_create_menu_page(){
- add_theme_page(
- 'Boundaries For Breaking Options',
- 'B4B Options',
- 'administrator',
- 'b4b_theme_options',
- 'b4b_theme_display'
- );
- }
- add_action('admin_menu', 'b4b_create_menu_page');
- function b4b_theme_display(){
- $sections = array('General Options', 'Header Options', 'Footer Options');
- ?>
- <div class="wrap">
- <div id="icon-themes" class="icon32"></div>
- <h2>Boundaries For Breaking Options</h2>
- <?php settings_errors(); ?>
- <nav class="b4b-options-menu">
- <ul>
- <?php
- foreach($sections as $section){
- echo '<li>' . $section . '</li>';
- }
- ?>
- </ul>
- </nav>
- <form method="post" action="options.php">
- <?php settings_fields( 'b4b_theme_display_options' ); ?>
- <?php do_settings_sections( 'b4b_theme_display_options') ?>
- <?php submit_button(); ?>
- </form>
- </div>
- <?php
- }
- /*----------------------------
- register settings
- ----------------------------*/
- function b4b_init_theme_options(){
- if( false == get_option( 'b4b_theme_display_options' ) ) {
- add_option( 'b4b_theme_display_options' );
- }
- //add_settings_section
- add_settings_section(
- 'b4b_general_options',
- 'General Options',
- 'b4b_section_callback',
- 'b4b_theme_display_options'
- );
- add_settings_section(
- 'b4b_header_options',
- 'Header Options',
- 'b4b_section_callback',
- 'b4b_theme_display_options'
- );
- add_settings_section(
- 'b4b_footer_options',
- 'Footer Options',
- 'b4b_section_callback',
- 'b4b_theme_display_options'
- );
- //add_settings_field
- add_settings_field(
- 'favicon_image',
- 'Change favicon',
- 'b4b_image_upload_callback',
- 'b4b_theme_display_options',
- 'b4b_general_options',
- array(
- 'favicon_image',
- 'Upload & choose a favicon image.'
- )
- );
- add_settings_field(
- 'number_of_posts',
- 'Number of posts on homepage',
- 'b4b_input_callback',
- 'b4b_theme_display_options',
- 'b4b_general_options',
- array(
- 'number_of_posts',
- 'Choose the number of posts displayed on the homepage.'
- )
- );
- add_settings_field(
- 'share_post_enable',
- 'Enable sharing buttons in posts',
- 'b4b_checkbox_callback',
- 'b4b_theme_display_options',
- 'b4b_general_options',
- array(
- 'share_post_enable',
- 'Check the box to enable social sharing buttons in posts.'
- )
- );
- add_settings_field(
- 'author_bio_enable',
- 'Enable author bio in posts',
- 'b4b_checkbox_callback',
- 'b4b_theme_display_options',
- 'b4b_general_options',
- array(
- 'author_bio_enable',
- 'Check the box to enable an author bio in posts.'
- )
- );
- add_settings_field(
- 'header_type',
- 'Text or Image header',
- 'b4b_select_callback',
- 'b4b_theme_display_options',
- 'b4b_header_options',
- array(
- 'image','text',
- 'header_type','Choose the type of header you want.'
- )
- );
- add_settings_field(
- 'header_image',
- 'Header image',
- 'b4b_image_upload_callback',
- 'b4b_theme_display_options',
- 'b4b_header_options',
- array(
- 'header_image',
- 'Upload & choose a header image.'
- )
- );
- add_settings_field(
- 'header_title',
- 'Header title text',
- 'b4b_input_callback',
- 'b4b_theme_display_options',
- 'b4b_header_options',
- array(
- 'header_title',
- 'Type the text you want for the header title.'
- )
- );
- add_settings_field(
- 'header_slogan',
- 'Header slogan text',
- 'b4b_input_callback',
- 'b4b_theme_display_options',
- 'b4b_header_options',
- array(
- 'header_slogan',
- 'Type the text you want for the header slogan.'
- )
- );
- add_settings_field(
- 'social_icons_enable',
- 'Enable social icons',
- 'b4b_checkbox_callback',
- 'b4b_theme_display_options',
- 'b4b_footer_options',
- array(
- 'social_icons_enable',
- 'Enable/Disable social icons at the end of your posts.'
- )
- );
- add_settings_field(
- 'footer_text',
- 'Text in footer',
- 'b4b_textarea_callback',
- 'b4b_theme_display_options',
- 'b4b_footer_options',
- array(
- 'footer_text',
- '3','70',
- 'Type the text you want in the footer.'
- )
- );
- add_settings_field(
- 'facebook_url',
- 'Facebook url',
- 'b4b_input_callback',
- 'b4b_theme_display_options',
- 'b4b_footer_options',
- array(
- 'facebook_url',
- 'Type in your facebook url.'
- )
- );
- add_settings_field(
- 'twitter_url',
- 'Twitter url',
- 'b4b_input_callback',
- 'b4b_theme_display_options',
- 'b4b_footer_options',
- array(
- 'twitter_url',
- 'Type in your twitter url.'
- )
- );
- //register_setting
- register_setting(
- 'b4b_theme_display_options',
- 'b4b_theme_display_options',
- 'b4b_theme_display_options_validation'
- );
- }
- add_action('admin_init', 'b4b_init_theme_options');
- /*----------------------------
- section & field callbacks
- ----------------------------*/
- function b4b_section_callback(){
- }
- function b4b_image_upload_callback($args){
- $options = get_option('b4b_theme_display_options');
- $html = '<span id="' . $args[0] . '" class="upload">';
- $html .= '<input type="url" id="' . $args[0] . '" class="regular-text text-upload" name="b4b_theme_display_options[' . $args[0] . ']" value="' . esc_url( $options[$args[0]] ) . '" />';
- $html .= '<input type="button" id="' . $args[0] . '" class="button button-upload" value="Upload an image" /><br />';
- $html .= '<img style="max-width: 300px; display: block;" src="' . esc_url( $options[$args[0]] ) . '" id="' . $args[0] . '" class="preview-upload" />';
- $html .= '</span>';
- echo $html;
- }
- function b4b_input_callback($args){
- $options = get_option('b4b_theme_display_options');
- if($args[0]=="number_of_posts"){
- $html = '<input type="number"';
- $html .= ' pattern="\d*"';
- } elseif ($args[0]=="facebook_url" || $args[0]=="twitter_url"){
- $html = '<input type="url"';
- } else {
- $html = '<input type="text"';
- }
- $html .= ' id="' . $args[0] . '" name="b4b_theme_display_options[' . $args[0] . ']" value="' . $options[$args[0]] . '" />';
- echo $html;
- }
- function b4b_textarea_callback($args){
- $options = get_option('b4b_theme_display_options');
- $html = '<textarea id="' . $args[0] . '" name="b4b_theme_display_options[' . $args[0] . ']" rows="' . $args[1] . '" cols="' . $args[2] . '">' . $options[$args[0]] . '</textarea>';
- echo $html;
- }
- function b4b_checkbox_callback($args){
- $options = get_option('b4b_theme_display_options');
- $html = '<input type="checkbox" id="' . $args[0] . '" name="b4b_theme_display_options[' . $args[0] . ']" value="1" ' . checked(1, $options[$args[0]], false) . '/>';
- $html .= '<label for="' . $args[0] . '"> ' . $args[1] . '</label>';
- echo $html;
- }
- function b4b_select_callback($args){
- $options = get_option('b4b_theme_display_options');
- $html = '<select id="' . $args[Count($args)-2] . '" name="b4b_theme_display_options[' . $args[Count($args)-2] . ']">';
- foreach($args as $arg){
- if ($args[Count($args)-1] != $arg && $args[Count($args)-2] != $arg){
- $html .= '<option value="' . $arg . '"' . selected($options[$args[Count($args)-2]], $arg, false) . '>' . $arg . '</option>';
- }
- }
- $html .= '</select>';
- $html .= '<label for="' . $args[Count($args)-2] . '">' . $args[Count($args)-1] . '</label>';
- echo $html;
- }
- /*----------------------------
- validation & sanitization
- ----------------------------*/
- function b4b_theme_display_options_validation($input){
- $output = array();
- foreach($input as $key => $value){
- if(isset($input[$key])){
- $output[$key] = strip_tags(stripslashes($input[$key]));
- }
- }
- return apply_filters('b4b_theme_display_options_validation', $output, $input);
- }
- /*----------------------------
- usage
- ----------------------------*/
- function b4b_add_favicon(){
- $b4b_options = get_option('b4b_theme_display_options');
- $b4b_favicon = $b4b_options['favicon_image'];
- ?>
- <link rel="icon" type="image/png" href="<?php echo esc_url($b4b_favicon); ?>">
- <?php
- }
- add_action('wp_head', 'b4b_add_favicon');
- function b4b_number_of_posts($query){
- $b4b_options = get_option('b4b_theme_display_options');
- $b4b_number_of_posts = $b4b_options['number_of_posts'];
- if($b4b_number_of_posts != '' && $query->is_home() && $query->is_main_query()){
- $query->set('posts_per_page', $b4b_number_of_posts);
- }
- }
- add_action('pre_get_posts', 'b4b_number_of_posts');
- function b4b_sharing_enabled($content){
- $b4b_options = get_option('b4b_theme_display_options');
- $b4b_share_post_enable = $b4b_options['share_post_enable'];
- if ($b4b_share_post_enable && is_single() || is_page()){
- $url = get_permalink();
- $content .= '<nav class="sharethis"><ul>';
- $content .= '<li><a href="https://www.facebook.com/sharer/sharer.php?u=' . esc_url($url) . '" onclick="window.open(this.href, \'facebook-share-dialog\', \'resizable=yes,width=626,height=436\'); return false;">facebook</a></li>';
- $content .= '<li><a href="https://twitter.com/share?url=' . esc_url($url) . '" onclick="window.open(this.href, \'twitter-share-dialog\', \'resizable=yes,width=626,height=436\'); return false;">twitter</a></li>';
- $content .= '<li><a href="https://plus.google.com/share?url=' . esc_url($url) . '" onclick="window.open(this.href, \'googleplus-share-dialog\', \'resizable=yes,width=626,height=436\'); return false;">googleplus</a></li>';
- $content .= '</ul></nav>';
- return $content;
- } else {
- return $content;
- }
- }
- add_filter('the_content', 'b4b_sharing_enabled');
- function b4b_header_options(){
- $b4b_options = get_option('b4b_theme_display_options');
- $b4b_header_type = $b4b_options['header_type'];
- $b4b_header_image = $b4b_options['header_image'];
- $b4b_header_title = $b4b_options['header_title'];
- $b4b_header_slogan = $b4b_options['header_slogan'];
- if ($b4b_header_type == 'image'){ ?>
- <a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home">
- <img src="<?php echo $b4b_header_image; ?>" alt="logo" />
- </a>
- <?php } elseif($b4b_header_type && $b4b_header_title == '' && $b4b_header_slogan == '') { ?>
- <h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h1>
- <h2 class="site-description"><?php bloginfo( 'description' ); ?></h2>
- <?php } elseif($b4b_header_type && $b4b_header_title != '' && $b4b_header_slogan != '') { ?>
- <h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( $b4b_header_title ); ?>" rel="home"><?php echo esc_attr( $b4b_header_title ); ?></a></h1>
- <h2 class="site-description"><?php echo $b4b_header_slogan ?></h2>
- <?php }
- }
- add_action('site-branding', 'b4b_header_options');
- function b4b_footer_options(){
- $b4b_options = get_option('b4b_theme_display_options');
- $b4b_social_icons_enable = $b4b_options['social_icons_enable'];
- $b4b_facebook_url = $b4b_options['facebook_url'];
- $b4b_twitter_url = $b4b_options['twitter_url'];
- $b4b_footer_text = $b4b_options['footer_text'];
- if($b4b_social_icons_enable){
- ?>
- <nav class="socialicons">
- <ul>
- <?php
- if($b4b_facebook_url != ''){
- echo '<li><a href="' . esc_url($b4b_facebook_url) . '">facebook</a></li>';
- }
- if($b4b_twitter_url != ''){
- echo '<li><a href="' . esc_url($b4b_twitter_url) . '">twitter</a></li>';
- }
- ?>
- </ul>
- </nav>
- <?php
- }
- if($b4b_footer_text != ''){
- echo '<p>' . sanitize_text_field($b4b_footer_text) . '</p>';
- }
- }
- add_action('wp_footer', 'b4b_footer_options');
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement