Advertisement
sayful

WP Theme Option with Option-Tree

Feb 17th, 2014
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.62 KB | None | 0 0
  1. <?php
  2. /*
  3. *Download Option Tree: http://wordpress.org/extend/plugins/option-tree/
  4. *Copy option-tree folder to your theme directory
  5. */
  6.  
  7. /*================================================
  8. Usage on functions.php to activate Option-Tree
  9. =================================================*/
  10.  
  11. add_filter( 'ot_show_pages', '__return_false' );
  12.  
  13. add_filter( 'ot_show_new_layout', '__return_false' );
  14.  
  15. add_filter( 'ot_theme_mode', '__return_true' );
  16.  
  17. include_once( 'option-tree/ot-loader.php' );
  18.  
  19. include_once( 'includes/theme-options.php' );
  20.  
  21.  
  22.  
  23.  
  24.  
  25. /*===================================================
  26. Create Option theme-options.php under (inc) folder
  27. =====================================================*/
  28.  
  29.  
  30.      
  31.     add_action( 'admin_init', 'custom_theme_options', 1 );
  32.      
  33.     function custom_theme_options() {
  34.      
  35.       $saved_settings = get_option( 'option_tree_settings', array() );
  36.      
  37.      
  38.       $custom_settings = array(
  39.         'sections'        => array(
  40.           array(
  41.             'id'          => 'general',
  42.             'title'       => 'Site Settings'
  43.           )
  44.         ),
  45.         'settings'        => array(
  46.           array(
  47.             'id'          => 'logo_text',
  48.             'label'       => 'Logo Text',
  49.             'desc'        => 'Use H1, H2, H3 tag',
  50.             'type'        => 'textarea',
  51.             'section'     => 'general'
  52.           ),
  53.           array(
  54.             'id'          => 'footer_text',
  55.             'label'       => 'Footer Text',
  56.             'type'        => 'textarea',
  57.             'section'     => 'general'
  58.           )
  59.         )
  60.       );
  61.      
  62.       if ( $saved_settings !== $custom_settings ) {
  63.         update_option( 'option_tree_settings', $custom_settings );
  64.       }
  65.      
  66.     }
  67.  
  68.  
  69.  
  70.  
  71. /*=======================================
  72. Get Data From Option Tree
  73. =======================================*/
  74.  
  75. // To get Condtional Data
  76.  
  77. <?php if ( function_exists( 'get_option_tree') ) : if( get_option_tree( 'your_tree_id') ) : ?>    
  78.  
  79.     <?php get_option_tree( 'your_tree_id', '', 'true' ); ?>
  80.  
  81. <?php else : ?>
  82.  
  83.     Your Default Data
  84.  
  85. <?php endif; endif; ?>
  86.  
  87.  
  88.  
  89. // To get Simple Data
  90.  
  91. <?php get_option_tree( 'facebook', '', 'true' ); ?>
  92.  
  93. ?>
  94.  
  95. <?php
  96. //The following code would echo the images if the key was "src" and the setting ID was "images"
  97.  
  98. if ( function_exists( 'ot_get_option' ) ) {
  99.  
  100.   /* get the images array */
  101.   $images = ot_get_option( 'images', array() );
  102.  
  103.   if ( ! empty( $images ) ) {
  104.     foreach( $images as $image ) {
  105.       echo '<img src="' . $image['src'] . '" alt="' . $image['title'] . '" />';
  106.     }
  107.   }
  108.  
  109. }
  110. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement