Advertisement
rexcoder

Widget with Conditional Advertisements

Jan 10th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.83 KB | None | 0 0
  1. //Just use "image widget" Plugin************************************************************************************
  2.  
  3. <!-- Insert into sidebar.php -->
  4. //It's conditional widget---------------------------------------------------------------------------
  5.     <?php $options=get_option('plugin_options'); ?>
  6.     <?php
  7.         if($options['home_page_ads']>=1) {
  8.     ?>
  9.     <div id="home_ad_1" class="advert" style="background: transparent url(<?php bloginfo('stylesheet_directory'); ?>/_images/ad_box.gif) no-repeat;">
  10.         <?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('home_ad_1')) : else : ?>
  11.         <div class="pre-widget">
  12.             <p>&nbsp;</p>
  13.             <p>&nbsp;</p>
  14.         </div>
  15.         <?php endif; ?>
  16.     </div>
  17. <?php } ?>
  18.  
  19. <!-- Insert into style.css -->
  20. .widget-area .advert {
  21.     margin: 0 auto 20px auto;  
  22.     width:  284px;
  23.     height: 166px;
  24.     padding: 7px 0 0 5px;
  25. }
  26.  
  27.  
  28. <?php
  29. //Only for dashboard setting panel-----------------------------------------------------------------------------------------
  30. // Insert a menu item in the admin menu called "Advertisements"
  31. add_action('admin_menu', 'create_theme_options_page');
  32. function create_theme_options_page() {
  33.   add_options_page('Advertisements', 'Advertisements', 'administrator', __FILE__, 'build_options_page');
  34. }
  35.  
  36. // Builds the Advertisements page
  37. function build_options_page() {
  38.     ?>
  39.   <div id="theme-options-wrap">
  40.     <div class="icon32" id="icon-tools"> <br /> </div>
  41.     <h2>Advertisements</h2>
  42.     <p>Set the number of ads in the sidebar.</p>
  43.     <form method="post" action="options.php">
  44.       <?php settings_fields('plugin_options'); ?>
  45.       <?php do_settings_sections(__FILE__); ?>
  46.       <p class="submit">
  47.         <input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes'); ?>" />
  48.       </p>
  49.     </form>
  50.   </div>
  51. <?php
  52. }
  53.  
  54. // Initializes the sections of the page when the Dashboard boots up
  55. add_action('admin_init', 'register_and_build_fields');
  56. function register_and_build_fields() {
  57.   register_setting('plugin_options', 'plugin_options', 'validate_setting');
  58.   add_settings_section('main_section', '', 'section_cb', __FILE__);
  59.   add_settings_field('home_page_ads', 'Page Ads:', 'home_page_ads_setting', __FILE__, 'main_section');
  60. }
  61.  
  62. // Ensures that the correct options are chosen
  63. function validate_setting($plugin_options) {
  64.   return $plugin_options;
  65. }
  66. function section_cb() {}
  67.  
  68. // Sets up the select menus
  69. function home_page_ads_setting() {
  70.   $options = get_option('plugin_options');
  71.   $items = array('0','1','2','3');
  72.   echo "<select name='plugin_options[home_page_ads]' style='width:50px;' />";
  73.     foreach($items as $item) {
  74.         $selected = ( $options['home_page_ads'] === $item ) ? 'selected="selected"' : ''    ;
  75.         echo "<option value='$item' $selected > $item </option>";
  76.     };
  77.   echo '</select>';
  78. }
  79. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement