Advertisement
garethdaine

functions.php file

Mar 29th, 2012
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.77 KB | None | 0 0
  1. <?php
  2.     // Add JavaScript files
  3.     function add_scripts() {
  4.         wp_enqueue_script('modernizr_script', get_template_directory_uri() . '/js/modernizr.js', __FILE__);
  5.         wp_enqueue_script('global_functions_script', get_template_directory_uri() . '/js/global-functions.js', __FILE__);
  6.     }
  7.     add_action('wp_enqueue_scripts', 'add_scripts');
  8.    
  9.     // Setup and load the theme options page and related code
  10.     require(get_template_directory() . '/inc/theme-options.php');
  11.    
  12.     //Get theme options
  13.     $options = get_option('shv_theme_options');
  14.  
  15.     // Add support for custom backgrounds
  16.     add_custom_background();
  17.    
  18.     // Add support for post thumbnails and admin bar
  19.     add_theme_support('post-thumbnails');
  20.     add_theme_support('admin-bar');
  21.    
  22.     // This theme uses wp_nav_menu() in two locations
  23.         if (function_exists('register_nav_menus')) {
  24.             register_nav_menus(array(
  25.                 'primary' => 'Primary Navigation',
  26.                 'secondary' => 'Secondary Navigation'
  27.             ));
  28.         }
  29.        
  30.     /*
  31.      * Register custom post types
  32.      */
  33.    
  34.     //Testimonials Post Type
  35.     class SHV_Testimonials_Post_Type {
  36.        
  37.         //Create construct function
  38.         public function __construct() {
  39.             $this->register_post_type();
  40.             $this->metaboxes();
  41.         }
  42.        
  43.         //Register post type
  44.         public function register_post_type() {
  45.             $args = array(
  46.                 'labels' => array(
  47.                     'name' => 'Testimonials',
  48.                     'singular_' => 'Testimonial',
  49.                     'add_new' => 'Add New Testimonial',
  50.                     'all_items' => 'All Testimonials',
  51.                     'add_new_item' => 'Add New Testimonial',
  52.                     'edit_item' => 'Edit Testimonial',
  53.                     'new_item' => 'New Testimonial',
  54.                     'view_item' => 'View Testimonial',
  55.                     'search_items' => 'Search Testimonials',
  56.                     'not_found' => 'No Testimonials Found',
  57.                     'not_found_in_trash' => 'No Testimonials Found in Trash'
  58.                 ),
  59.                 'query_var' => 'testimonials',
  60.                 'rewrite' => array(
  61.                     'slug' => 'testimonials',
  62.                 ),
  63.                 'public' => true,
  64.                 'supports' => array(
  65.                     'title'
  66.                 )
  67.             );
  68.             register_post_type('shv_testimonials', $args);
  69.         }
  70.        
  71.         //Build metaboxes
  72.         public function metaboxes() {
  73.            
  74.             //Add new meta boxes testimonial and testimonial author
  75.             add_action('add_meta_boxes', 'add_testimonial_meta_box');
  76.             add_action('add_meta_boxes', 'add_testimonial_author_meta_box');
  77.            
  78.             //Run add_meta_box functions
  79.             function add_testimonial_meta_box() {
  80.                 add_meta_box('shv_testimonial', 'Testimonial', 'testimonial', 'shv_testimonials');
  81.             }
  82.             function add_testimonial_author_meta_box() {
  83.                 add_meta_box('shv_testimonial_author', 'Testimonial Author', 'testimonial_author', 'shv_testimonials');
  84.             }
  85.            
  86.             //Create form elements and pull through any data associated with each meta box
  87.             function testimonial($post) {
  88.                 $testimonial = get_post_meta($post->ID, 'shv_testimonial', true); ?>
  89.                 <p>
  90.                     <label for="shv_testimonial">Please enter your testimonial</label>
  91.                     <textarea class="large-text" name="shv_testimonial" id="shv_testimonial" cols="50" rows="5"><?php echo esc_attr($testimonial); ?></textarea>
  92.                 </p>
  93.             <?php }
  94.  
  95.             function testimonial_author($post) {
  96.                 $author = get_post_meta($post->ID, 'shv_testimonial_author', true); ?>
  97.                 <p>
  98.                     <label for="shv_testimonial_author">Please enter a testimonial author name and school, e.g. John Doe, St Ambrose Barlow</label>
  99.                     <input type="text" class="widefat" name="shv_testimonial_author" id="shv_testimonial_author" value="<?php echo esc_attr($author); ?>" />
  100.                 </p>
  101.             <?php }
  102.            
  103.             //Save meta box input
  104.             add_action('save_post', 'save_meta_data');
  105.            
  106.             function save_meta_data($id) {
  107.                
  108.                 if (isset($_POST['shv_testimonial'])) {
  109.                     update_post_meta(
  110.                         $id,
  111.                         'shv_testimonial',
  112.                         strip_tags($_POST['shv_testimonial'])
  113.                     );
  114.                 }
  115.                
  116.                 if (isset($_POST['shv_testimonial_author'])) {
  117.                     update_post_meta(
  118.                         $id,
  119.                         'shv_testimonial_author',
  120.                         strip_tags($_POST['shv_testimonial_author'])
  121.                     );
  122.                 }
  123.             }
  124.         }
  125.     }
  126.    
  127.     add_action('init', 'register_testimonials');
  128.    
  129.     function register_testimonials() {
  130.         new SHV_Testimonials_Post_Type();
  131.     }
  132.    
  133.     function show_tweets() { ?>
  134.         <img src="<?php echo get_bloginfo('template_directory') . '/images/twitter-bird-icon.png'; ?>" alt="Tweets" />
  135.             <?php
  136.                 //Get theme options
  137.                 $options = get_option('shv_theme_options');
  138.                 include_once(ABSPATH . WPINC . '/feed.php');
  139.                 $username = $options['shv_twitter_username'];
  140.                 $rss = fetch_feed('https://api.twitter.com/1/statuses/user_timeline.rss?screen_name=' . $username);
  141.                
  142.                 if (!is_wp_error( $rss ) ) : // Checks that the object is created correctly
  143.                     // Figure out how many total items there are, but limit it to 5.
  144.                     $maxitems = $rss->get_item_quantity(1);
  145.                
  146.                     // Build an array of all the items, starting with element 0 (first element).
  147.                     $rss_items = $rss->get_items(0, $maxitems);
  148.                 endif;
  149.             ?>
  150.             <ul>
  151.                 <?php if ($maxitems == 0) echo '<li>No items.</li>';
  152.                 else
  153.                 // Loop through each feed item and display each item as a hyperlink.
  154.                 foreach ( $rss_items as $item ) : ?>
  155.                     <li>
  156.                         <a href='<?php echo $item->get_permalink(); ?>'>
  157.                             <?php echo $item->get_title(); ?>
  158.                         </a>
  159.                     </li>
  160.                 <?php endforeach; ?>
  161.             </ul>
  162.     <?php }
  163.  
  164.     function show_testimonials() {
  165.         $loop = new WP_Query(
  166.             array(
  167.             'post_type' => 'shv_testimonials',
  168.             'order_by' => 'ID',
  169.             )
  170.         );
  171.        
  172.         if ($loop->have_posts()) {
  173.             $output = '<ul class="testimonial-list">';
  174.            
  175.             while($loop->have_posts()) {
  176.                 $loop->the_post();
  177.                 $meta = get_post_meta(get_the_id(), '');
  178.                 print_r($meta);
  179.                
  180.                 $output .= '
  181.                     <li>
  182.                         <a href="' . get_permalink() . '">
  183.                             ' . get_the_title() . ' | ' .
  184.                             $meta['shv_testimonial_author'][0] . '
  185.                         </a>
  186.                     </li>
  187.                 ';
  188.             }
  189.         }
  190.        
  191.         return $output;
  192.     }
  193. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement