Advertisement
sandyd

Youtube FrontPage Plugin

Jul 17th, 2012
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.39 KB | None | 0 0
  1. <?php
  2.  
  3. /*  Plugin Name: Youtube FrontPage
  4.   Plugin URI: http://dolphinaura.com
  5.   Description: This plugin sets the default frontpage youtube video.
  6.   Version: 1.0
  7.     Author: sandy(d)
  8.     Author URI: http://dolphinaura.com
  9.     License: GPL2
  10. */
  11.  
  12.  
  13. ?>
  14. <?php
  15. // create custom plugin settings menu
  16. add_action('admin_menu', 'ytfront_create_menu');
  17.  
  18. function ytfront_create_menu() {
  19.  
  20.     //create new top-level menu
  21.     add_menu_page('Youtube FrontPage Plugin Settings', 'Youtube Frontpage', 'administrator', 'ytfront', 'ytfront_settings_page');
  22.  
  23.     //call register settings function
  24.     add_action( 'admin_init', 'register_mysettings' );
  25. }
  26.  
  27.  
  28. function register_mysettings() {
  29.     //register our settings
  30.     register_setting( 'ytfront-settings-group', 'ytfront_url' );
  31. }
  32.  
  33. function ytfront_settings_page() {
  34.  
  35. ?>
  36. <div class="wrap">
  37. <h2>Youtube FrontPage Plugin Settings</h2>
  38.  
  39. <form method="post" action="options.php">
  40.     <?php settings_fields( 'ytfront-settings-group' ); ?>
  41.     <?php do_settings_sections( 'ytfront-settings-group' ); ?>
  42.     <table class="form-table">
  43.         <tr>This controls the Youtube Video seen on the front page.</tr>
  44.         <tr valign="top">
  45.         <th scope="row">Youtube Link</th>
  46.         <td><input type="text" name="ytfront_url" value="<?php echo get_option('ytfront_url'); ?>" /></td>
  47.         </tr>
  48.     </table>
  49.     <h2>Video Preview</h2>
  50.     <?php
  51.         //Match all Youtube Links (excluding youtu.be short links)
  52.         $videoUrl = get_option('ytfront_url');
  53.         preg_match('%https?://(www\.)?youtube\.com/watch\?v=[^&]+.*%', $videoUrl, $matches);
  54.        
  55.         //youtu.be short links
  56.         if (substr(get_option('ytfront_url'),0, 15) =="http://youtu.be") {
  57.                 $strippedurl=substr(get_option('ytfront_url'),16, strlen(get_option('ytfront_url')));?>
  58.                 <iframe width="560" height="315" src="http://www.youtube.com/embed/<?php echo substr(get_option('ytfront_url'),16, strlen(get_option('ytfront_url'))); ?>" frameborder="0" allowfullscreen></iframe>  
  59.             <?php }
  60.         //Not a Youtube Link (Does not match regex)
  61.         elseif (implode(',', $matches) == "") { ?>
  62.                 <h3>This video link is invalid</h3>
  63.         <?php }
  64.         //Youtube Links with stuff like "&feature=player_embedded" at the back.
  65.         //Those are stripped.
  66.         elseif (strpos(get_option('ytfront_url'),'&') !== false) {
  67.             $strippedurl=substr(get_option('ytfront_url'), 0, strpos(get_option('ytfront_url'), '&'));?>
  68.             <iframe width="560" height="315" src="http://www.youtube.com/embed/<?php echo substr($strippedurl, strpos($strippedurl, '=')+1, strlen($strippedurl)); ?>" frameborder="0" allowfullscreen></iframe>
  69.          <?php  
  70.         }
  71.         //Standard Youtube Links
  72.         else {
  73.             $strippedurl=substr(get_option('ytfront_url'), strpos(get_option('ytfront_url'), '=')+1, strlen(get_option('ytfront_url')));
  74.             ?>
  75.             <iframe width="560" height="315" src="http://www.youtube.com/embed/<?php echo $strippedurl ?>" frameborder="0" allowfullscreen></iframe>
  76.        <?php } ?>
  77.        
  78.    
  79.     <div style="text-align:right"><img src="https://s3-us-west-1.amazonaws.com/daura-static/img/sandyd_production.png" alt="sandyd_production" /></div>
  80.    
  81.     <p class="submit">
  82.     <input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
  83.     </p>
  84.  
  85. </form>
  86. </div>
  87. <?php }
  88. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement