Advertisement
kaed

theme-options.php - 7/29/13

Jul 29th, 2013
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.25 KB | None | 0 0
  1. <?php
  2. //b4b Theme Options Panel - by Connor
  3.  
  4. /*------------Field Argument Formats------------
  5.     img upload args = 'id', 'desc'
  6.     input args = 'id', 'desc'
  7.     textarea args = 'id', 'rows', 'cols', 'desc'
  8.     checkbox args = 'id','desc'
  9.     select args = 'opt1', 'opt2', 'opt3', 'opt#...', 'id', 'desc'
  10. ----------------------------------------------*/
  11.  
  12.  
  13. function my_admin_scripts() {
  14.     wp_enqueue_media();
  15.     wp_register_style('options-style', get_template_directory_uri() . '/options-style.css');
  16.     wp_enqueue_style('options-style');
  17.     wp_register_script('b4b-upload', get_template_directory_uri() . '/js/b4b-upload.js', array('jquery'));
  18.     wp_register_script('b4b-options', get_template_directory_uri() . '/js/b4b-options.js', array('jquery'));
  19.     wp_enqueue_script('b4b-upload');
  20.     wp_enqueue_script('b4b-options');
  21. }
  22. add_action('admin_enqueue_scripts', 'my_admin_scripts');
  23.  
  24. function b4b_create_menu_page(){
  25.     add_theme_page(
  26.         'Boundaries For Breaking Options',
  27.         'B4B Options',
  28.         'administrator',
  29.         'b4b_theme_options',
  30.         'b4b_theme_display'
  31.     );
  32. }
  33. add_action('admin_menu', 'b4b_create_menu_page');
  34.  
  35. function b4b_theme_display(){
  36. $sections = array('General Options', 'Header Options', 'Footer Options');
  37. ?>
  38.     <div class="wrap">
  39.    
  40.         <div id="icon-themes" class="icon32"></div>
  41.         <h2>Boundaries For Breaking Options</h2>
  42.        
  43.         <?php settings_errors(); ?>
  44.        
  45.         <nav class="b4b-options-menu">
  46.             <ul>
  47.             <?php
  48.             foreach($sections as $section){
  49.                 echo '<li>' . $section . '</li>';
  50.             }
  51.             ?>
  52.             </ul>
  53.         </nav>
  54.        
  55.         <form method="post" action="options.php">
  56.             <?php settings_fields( 'b4b_theme_display_options' ); ?>
  57.             <?php do_settings_sections( 'b4b_theme_display_options') ?>
  58.             <?php submit_button(); ?>
  59.         </form>
  60.        
  61.     </div>
  62. <?php
  63. }
  64.  
  65. /*----------------------------
  66.     register settings
  67. ----------------------------*/
  68. function b4b_init_theme_options(){
  69.     if( false == get_option( 'b4b_theme_display_options' ) ) {
  70.         add_option( 'b4b_theme_display_options' );
  71.     }
  72.  
  73.     //add_settings_section
  74.     add_settings_section(
  75.         'b4b_general_options',
  76.         'General Options',
  77.         'b4b_section_callback',
  78.         'b4b_theme_display_options'
  79.     );
  80.     add_settings_section(
  81.         'b4b_header_options',
  82.         'Header Options',
  83.         'b4b_section_callback',
  84.         'b4b_theme_display_options'
  85.     );
  86.     add_settings_section(
  87.         'b4b_footer_options',
  88.         'Footer Options',
  89.         'b4b_section_callback',
  90.         'b4b_theme_display_options'
  91.     );
  92.  
  93.     //add_settings_field
  94.     add_settings_field(
  95.         'favicon_image',
  96.         'Change favicon',
  97.         'b4b_image_upload_callback',
  98.         'b4b_theme_display_options',
  99.         'b4b_general_options',
  100.         array(
  101.             'favicon_image',
  102.             'Upload & choose a favicon image.'
  103.         )
  104.     );
  105.  
  106.     add_settings_field(
  107.         'number_of_posts',
  108.         'Number of posts on homepage',
  109.         'b4b_input_callback',
  110.         'b4b_theme_display_options',
  111.         'b4b_general_options',
  112.         array(
  113.             'number_of_posts',
  114.             'Choose the number of posts displayed on the homepage.'
  115.         )
  116.     );
  117.  
  118.     add_settings_field(
  119.         'share_post_enable',
  120.         'Enable sharing buttons in posts',
  121.         'b4b_checkbox_callback',
  122.         'b4b_theme_display_options',
  123.         'b4b_general_options',
  124.         array(
  125.             'share_post_enable',
  126.             'Check the box to enable social sharing buttons in posts.'
  127.         )
  128.     );
  129.  
  130.     add_settings_field(
  131.         'author_bio_enable',
  132.         'Enable author bio in posts',
  133.         'b4b_checkbox_callback',
  134.         'b4b_theme_display_options',
  135.         'b4b_general_options',
  136.         array(
  137.             'author_bio_enable',
  138.             'Check the box to enable an author bio in posts.'
  139.         )
  140.     );
  141.  
  142.     add_settings_field(
  143.         'header_type',
  144.         'Text or Image header',
  145.         'b4b_select_callback',
  146.         'b4b_theme_display_options',
  147.         'b4b_header_options',
  148.         array(
  149.             'image','text',
  150.             'header_type','Choose the type of header you want.'
  151.         )
  152.     );
  153.  
  154.     add_settings_field(
  155.         'header_image',
  156.         'Header image',
  157.         'b4b_image_upload_callback',
  158.         'b4b_theme_display_options',
  159.         'b4b_header_options',
  160.         array(
  161.             'header_image',
  162.             'Upload & choose a header image.'
  163.         )
  164.     );
  165.  
  166.     add_settings_field(
  167.         'header_title',
  168.         'Header title text',
  169.         'b4b_input_callback',
  170.         'b4b_theme_display_options',
  171.         'b4b_header_options',
  172.         array(
  173.             'header_title',
  174.             'Type the text you want for the header title.'
  175.         )
  176.     );
  177.  
  178.     add_settings_field(
  179.         'header_slogan',
  180.         'Header slogan text',
  181.         'b4b_input_callback',
  182.         'b4b_theme_display_options',
  183.         'b4b_header_options',
  184.         array(
  185.             'header_slogan',
  186.             'Type the text you want for the header slogan.'
  187.         )
  188.     );
  189.  
  190.     add_settings_field(
  191.         'social_icons_enable',
  192.         'Enable social icons',
  193.         'b4b_checkbox_callback',
  194.         'b4b_theme_display_options',
  195.         'b4b_footer_options',
  196.         array(
  197.             'social_icons_enable',
  198.             'Enable/Disable social icons at the end of your posts.'
  199.         )
  200.     );
  201.  
  202.     add_settings_field(
  203.         'footer_text',
  204.         'Text in footer',
  205.         'b4b_textarea_callback',
  206.         'b4b_theme_display_options',
  207.         'b4b_footer_options',
  208.         array(
  209.             'footer_text',
  210.             '3','70',
  211.             'Type the text you want in the footer.'
  212.         )
  213.     );
  214.  
  215.     add_settings_field(
  216.         'facebook_url',
  217.         'Facebook url',
  218.         'b4b_input_callback',
  219.         'b4b_theme_display_options',
  220.         'b4b_footer_options',
  221.         array(
  222.             'facebook_url',
  223.             'Type in your facebook url.'
  224.         )
  225.     );
  226.  
  227.     add_settings_field(
  228.         'twitter_url',
  229.         'Twitter url',
  230.         'b4b_input_callback',
  231.         'b4b_theme_display_options',
  232.         'b4b_footer_options',
  233.         array(
  234.             'twitter_url',
  235.             'Type in your twitter url.'
  236.         )
  237.     );
  238.  
  239.     //register_setting
  240.     register_setting(
  241.         'b4b_theme_display_options',
  242.         'b4b_theme_display_options',
  243.         'b4b_theme_display_options_validation'
  244.     );
  245. }
  246. add_action('admin_init', 'b4b_init_theme_options');
  247.  
  248. /*----------------------------
  249.     section & field callbacks
  250. ----------------------------*/
  251. function b4b_section_callback(){
  252. }
  253.  
  254. function b4b_image_upload_callback($args){
  255.     $options = get_option('b4b_theme_display_options');
  256.     $html = '<span id="' . $args[0] . '" class="upload">';
  257.         $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]] ) . '" />';
  258.         $html .= '<input type="button" id="' . $args[0] . '" class="button button-upload" value="Upload an image" /><br />';
  259.         $html .= '<img style="max-width: 300px; display: block;" src="' . esc_url( $options[$args[0]] ) . '" id="' . $args[0] . '" class="preview-upload"  />';
  260.     $html .= '</span>';
  261.    
  262.     echo $html;
  263. }
  264.  
  265. function b4b_input_callback($args){
  266.     $options = get_option('b4b_theme_display_options');
  267.     if($args[0]=="number_of_posts"){
  268.         $html = '<input type="number"';
  269.         $html .= ' pattern="\d*"';
  270.     } elseif ($args[0]=="facebook_url" || $args[0]=="twitter_url"){
  271.         $html = '<input type="url"';
  272.     } else {
  273.         $html = '<input type="text"';
  274.     }
  275.     $html .= ' id="' . $args[0] . '" name="b4b_theme_display_options[' . $args[0] . ']" value="' . $options[$args[0]] . '" />';
  276.    
  277.     echo $html;
  278. }
  279.  
  280. function b4b_textarea_callback($args){
  281.     $options = get_option('b4b_theme_display_options');
  282.     $html = '<textarea id="' . $args[0] . '" name="b4b_theme_display_options[' . $args[0] . ']" rows="' . $args[1] . '" cols="' . $args[2] . '">' . $options[$args[0]] . '</textarea>';
  283.    
  284.     echo $html;
  285. }
  286.  
  287. function b4b_checkbox_callback($args){
  288.     $options = get_option('b4b_theme_display_options');
  289.     $html = '<input type="checkbox" id="' . $args[0] . '" name="b4b_theme_display_options[' . $args[0] . ']" value="1" ' . checked(1, $options[$args[0]], false) . '/>';
  290.     $html .= '<label for="' . $args[0] . '"> ' . $args[1] . '</label>';
  291.    
  292.     echo $html;
  293. }
  294.  
  295. function b4b_select_callback($args){
  296.     $options = get_option('b4b_theme_display_options');
  297.     $html = '<select id="' . $args[Count($args)-2] . '" name="b4b_theme_display_options[' . $args[Count($args)-2] . ']">';
  298.     foreach($args as $arg){
  299.         if ($args[Count($args)-1] != $arg && $args[Count($args)-2] != $arg){
  300.             $html .= '<option value="' . $arg . '"' . selected($options[$args[Count($args)-2]], $arg, false) . '>' . $arg . '</option>';
  301.         }
  302.     }
  303.     $html .= '</select>';
  304.     $html .= '<label for="' . $args[Count($args)-2] . '">' . $args[Count($args)-1] . '</label>';
  305.    
  306.     echo $html;
  307. }
  308.  
  309. /*----------------------------
  310.     validation & sanitization
  311. ----------------------------*/
  312. function b4b_theme_display_options_validation($input){
  313.     $output = array();
  314.     foreach($input as $key => $value){
  315.         if(isset($input[$key])){
  316.             $output[$key] = strip_tags(stripslashes($input[$key]));
  317.         }
  318.     }
  319.     return apply_filters('b4b_theme_display_options_validation', $output, $input);
  320. }
  321.  
  322. /*----------------------------
  323.     usage
  324. ----------------------------*/
  325. function b4b_add_favicon(){
  326.     $b4b_options = get_option('b4b_theme_display_options');
  327.     $b4b_favicon = $b4b_options['favicon_image'];
  328.     ?>
  329.     <link rel="icon" type="image/png" href="<?php echo esc_url($b4b_favicon); ?>">
  330.     <?php
  331. }
  332. add_action('wp_head', 'b4b_add_favicon');
  333.  
  334. function b4b_number_of_posts($query){
  335.     $b4b_options = get_option('b4b_theme_display_options');
  336.     $b4b_number_of_posts = $b4b_options['number_of_posts'];
  337.     if($b4b_number_of_posts != '' && $query->is_home() && $query->is_main_query()){
  338.         $query->set('posts_per_page', $b4b_number_of_posts);
  339.     }
  340. }
  341. add_action('pre_get_posts', 'b4b_number_of_posts');
  342.  
  343. function b4b_sharing_enabled($content){
  344.     $b4b_options = get_option('b4b_theme_display_options');
  345.     $b4b_share_post_enable = $b4b_options['share_post_enable'];
  346.    
  347.     if ($b4b_share_post_enable && is_single() || is_page()){
  348.         $url = get_permalink();
  349.        
  350.         $content .= '<nav class="sharethis"><ul>';
  351.         $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>';
  352.         $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>';
  353.         $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>';
  354.         $content .= '</ul></nav>';
  355.        
  356.         return $content;
  357.     } else {
  358.         return $content;
  359.     }
  360. }
  361. add_filter('the_content', 'b4b_sharing_enabled');
  362.  
  363. function b4b_header_options(){
  364.     $b4b_options = get_option('b4b_theme_display_options');
  365.     $b4b_header_type = $b4b_options['header_type'];
  366.     $b4b_header_image = $b4b_options['header_image'];
  367.     $b4b_header_title = $b4b_options['header_title'];
  368.     $b4b_header_slogan = $b4b_options['header_slogan'];
  369.    
  370.     if ($b4b_header_type == 'image'){ ?>
  371.                
  372.         <a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>" rel="home">
  373.             <img src="<?php echo $b4b_header_image; ?>" alt="logo" />
  374.         </a>
  375.        
  376.     <?php } elseif($b4b_header_type && $b4b_header_title == '' && $b4b_header_slogan == '') { ?>
  377.    
  378.         <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>
  379.         <h2 class="site-description"><?php bloginfo( 'description' ); ?></h2>
  380.        
  381.     <?php } elseif($b4b_header_type && $b4b_header_title != '' && $b4b_header_slogan != '') { ?>
  382.    
  383.         <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>
  384.         <h2 class="site-description"><?php echo $b4b_header_slogan ?></h2>
  385.        
  386.     <?php }
  387. }
  388. add_action('site-branding', 'b4b_header_options');
  389.  
  390. function b4b_footer_options(){
  391.     $b4b_options = get_option('b4b_theme_display_options');
  392.     $b4b_social_icons_enable = $b4b_options['social_icons_enable'];
  393.     $b4b_facebook_url = $b4b_options['facebook_url'];
  394.     $b4b_twitter_url = $b4b_options['twitter_url'];
  395.     $b4b_footer_text = $b4b_options['footer_text'];
  396.    
  397.     if($b4b_social_icons_enable){
  398.         ?>
  399.         <nav class="socialicons">
  400.             <ul>
  401.             <?php
  402.             if($b4b_facebook_url != ''){
  403.                 echo '<li><a href="' . esc_url($b4b_facebook_url) . '">facebook</a></li>';
  404.             }
  405.             if($b4b_twitter_url != ''){
  406.                 echo '<li><a href="' . esc_url($b4b_twitter_url) . '">twitter</a></li>';
  407.             }
  408.             ?>
  409.             </ul>
  410.         </nav>
  411.         <?php
  412.     }
  413.    
  414.     if($b4b_footer_text != ''){
  415.         echo '<p>' . sanitize_text_field($b4b_footer_text) . '</p>';
  416.     }
  417. }
  418. add_action('wp_footer', 'b4b_footer_options');
  419. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement