Advertisement
terorama

Wordpress Snippets 6

Oct 14th, 2013
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 53.11 KB | None | 0 0
  1. <?php
  2.  
  3. //-------------------------------------------------------------------
  4. //                using dynamic sidebars
  5. //-------------------------------------------------------------------
  6.  
  7. if (function_exists(‘register_sidebar’))
  8.  
  9.  register_sidebar(array(‘name’ => ‘sidebar1?,
  10.  
  11.   ’before_widget’ =><li id=%1$s” class=”widget %2$s”>,
  12.   ’after_widget’ =></li>,
  13.   ’before_title’ =><h2 class=”widgettitle”>,
  14.   ’after_title’ =></h2>,
  15.  
  16.  ));
  17.  
  18. //-----------------------------------------------------------
  19. //      altering widgets output dynamically
  20. //-----------------------------------------------------------
  21. add_filter(‘dynamic_sidebar_params’,
  22.                   ‘reclamation_sidebar_params’);
  23.  
  24. //------------------------------------------------
  25. function reclamation_sidebar_params($params){
  26. //------------------------------------------------
  27.  $params[0]['before_widget'] = '<div class=”block”><div class=”block_title”>';
  28.  
  29.  $params[0]['before_title'] =
  30.  
  31.                '<h3><img src=”' .
  32.                get_bloginfo(‘stylesheet_directory’) .
  33.                '/sidebartitles.php?letters=' .
  34.                @$params[0]['widget_name'] . '” />';
  35.  
  36.  $params[0]['after_title'] = '</h3></div>';
  37.  
  38.  $params[0]['after_widget'] =
  39.  
  40.                '</div><div class=”block_bottom”>'  
  41.  
  42.  
  43.  
  44.  return $params;
  45. }
  46.  
  47. ?>
  48.  
  49. <?php
  50. //-------------------------------------------------------------------
  51. //       tweaking output of widgets in the sidebar
  52. //-------------------------------------------------------------------
  53.  
  54. function my_edit_widget_func($params) {
  55.     $params[0]['before_title'] = '<h3 class="' . $params[0]['widget_name'] . '">' ;
  56.     return $params;
  57. }
  58.  
  59. add_filter('dynamic_sidebar_params',
  60.                         'my_edit_widget_func');
  61. ?>
  62.  
  63. <?php
  64. //-------------------------------------------------------------------
  65. //     using  ( pre_get_posts )  action hook
  66. //-------------------------------------------------------------------
  67.  
  68. //----------------------------------------------
  69. //   output posts with custom type on home page
  70. //----------------------------------------------
  71.  
  72. function some_name($query) {
  73.     if ( $query->is_main_query() && $query->is_home() ) {
  74.         $query->set( 'post_type', array( 'home_portfolio' ) );
  75.     }
  76. }
  77. add_action( 'pre_get_posts', 'some_name');
  78.  
  79.  
  80. //----------------------------------------------
  81. //  output posts with custom type on front page
  82. //----------------------------------------------
  83.  
  84. function wpse83754_filter_pre_get_posts( $query ) {
  85.     if ( $query->is_main_query() && is_front_page() ) {
  86.         $query->set( 'post_type', array( 'home_portfolio' ) );
  87.     }
  88. }
  89. add_action( 'pre_get_posts', 'wpse83754_filter_pre_get_posts' );
  90.  
  91. //----------------------------------------------
  92. //    altering output on single page    
  93. //----------------------------------------------
  94.  
  95. function wpse83754_filter_pre_get_posts( $query ) {
  96.     if ( $query->is_main_query() && is_page( $id ) ) {
  97.         $query->set( 'post_type', array( 'home_portfolio' ) );
  98.     }
  99. }
  100. add_action( 'pre_get_posts', 'wpse83754_filter_pre_get_posts' );
  101.  
  102. //----------------------------------------------
  103. //   exclude certain category from output
  104. //----------------------------------------------
  105.  
  106. add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
  107. function foo_modify_query_exclude_category( $query ) {
  108.     if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) )
  109.         $query->set( 'cat', '-5' );
  110. }
  111.  
  112. //----------------------------------------------
  113. //    check if we are in the loop
  114. //----------------------------------------------
  115.  
  116.  in_the_loop(); ?>
  117.  
  118. <?php
  119. //-------------------------------------------------------------------
  120. //    altering post content before output (in main cycle)
  121. //-------------------------------------------------------------------
  122.  
  123. add_filter ('the_excerpt', array ($this, 'insert_biography_box'));
  124. add_filter ('the_content', array ($this, 'insert_biography_box'));
  125.  
  126. //------------------------------------------
  127. function insert_biography_box ($content) {
  128. //------------------------------------------
  129.     error_log ('insert_biography_box: current filter=' . current_filter ());
  130.     if (!is_main_query ()) {
  131.         error_log ('Not main query, baling');
  132.         return $content;
  133.     }
  134.     $biography = 'some-magic-function-return-value';
  135.     return $content . $biography;
  136. }
  137.  
  138. ?>
  139.  
  140. <?php
  141. //-------------------------------------------------------------------
  142. //     registering multiple dynamic sidebars
  143. //-------------------------------------------------------------------
  144.  if(function_exists('register_sidebars')){
  145.  
  146. register_sidebars(2, array(
  147.     'name'=>'sidebar%d',
  148.     'id'  =>'sidebar',
  149.     'before_widget'=>'<li class="box">',
  150.     'after_widget'=>'</li><!--box--><!--aside-->',
  151.     'before_title'=>'<h2>',
  152.     'after_title'=>'</h2>',
  153. ));
  154. }
  155. ?>
  156.  
  157. <?php
  158. //-------------------------------------------------------------------
  159. //     altering widget params before output in sidebar
  160. //-------------------------------------------------------------------
  161. add_filter('dynamic_sidebar_params','io_blogroll_class');
  162.  
  163. //---------------------------------------
  164. function io_blogroll_class($params){
  165. //---------------------------------------
  166.  $params[0]['before_widget'] =  '<li class="'.$params[0]['widget_name'].' box">';
  167.  $params[0]['after_widget'] =  '</li><!--'.$params[0]['widget_name'].'-->';
  168.  
  169.     if ($params[0]['widget_name'] == "Links"){
  170.  
  171.        $params[0]['before_widget'] =  '<li class="'.$params[0]['widget_name'].'">';
  172.        $params[0]['after_widget'] =  '</li><!--'.$params[0]['widget_name'].'-->';
  173. }  
  174. return $params;
  175. }
  176.  
  177. ?>
  178.  
  179. <?php
  180. //-------------------------------------------------------------------
  181. //    altering post content before output (in main cycle)
  182. //-------------------------------------------------------------------
  183.  
  184. add_filter ('the_excerpt', array ($this, 'insert_biography_box'));
  185. add_filter ('the_content', array ($this, 'insert_biography_box'));
  186.  
  187.  
  188. function insert_biography_box ($content) {
  189.     if (!in_the_loop () || !is_main_query ()) {
  190.         return $content;
  191.     }
  192.  
  193.     $biography = 'some-magic-function-return-value';
  194.     return $content . $biography;
  195. }
  196.  
  197. ?>
  198. <?php
  199. //-------------------------------------------------------------------
  200. //        registering custom Widget
  201. //-------------------------------------------------------------------
  202.  
  203. add_action( 'widgets_init', 'register_my_widget' ); // Загрузка виджета
  204.  
  205. function register_my_widget() {}            // Функция регистрации виджета
  206. class My_Widget extends WP_Widget () {}         //Класс виджета (как пример)
  207. function My_Widget() {}                 // Установки виджета
  208. function widget() {}                    // Вывод виджета
  209. function update() {}                    // Обновление виджета
  210. function form() {}                  // Форма для опций виджета
  211.  
  212.  
  213.  
  214. //-------------------------------------------------------------------
  215. //               different output functions
  216. //-------------------------------------------------------------------
  217. ?>
  218.  
  219. <?php wp_list_pages('title_li=<h2>Pages</h2>'); ?>
  220.  
  221. <li id="categories"><?php _e('Categories:'); ?>
  222.     <ul>
  223. <?php wp_list_cats(); ?>
  224.     </ul>
  225.  </li>
  226.  
  227. <div class="navigation">
  228. <div class="alignleft">
  229.      <?php next_posts_link('&laquo; Previous Entries') ?>
  230. </div>
  231. <div class="alignright">
  232.      <?php previous_posts_link('Next Entries &raquo;') ?>
  233. </div>
  234. </div>
  235.  
  236. <li id="archives"><?php _e('Archives:'); ?>
  237.      <ul>
  238. <?php wp_get_archives('type=monthly'); ?>
  239.      </ul>
  240.  
  241. <?php wp_get_archives('type=postbypost&limit=15'); ?>
  242.  
  243. <?php get_links_list(); ?>
  244. <ul><?php get_links(2, '<li>', '</li>', '', TRUE, 'url', FALSE); ?></ul>
  245.  
  246. <h2>Link list of authors:</h2>
  247. <ul>
  248.     <?php
  249.     $order = 'user_nicename';
  250.     $user_ids = $wpdb->get_col("SELECT ID FROM $wpdb->users ORDER BY $order"); // query users
  251.  
  252.     foreach($user_ids as $user_id) : // start authors' profile "loop"
  253.        $user = get_userdata($user_id);
  254.        ?>
  255.        <li>
  256.         <?php echo '<a href="' . $user->user_url . '">' . $user->display_name . '</a>'; ?>
  257.         <br /></li>
  258.  
  259.         <?php
  260.     endforeach; // end of authors' profile 'loop'
  261.     ?>
  262. </ul>
  263.  
  264. <?php get_sidebar(); ?>
  265.  
  266. <?php include ('sidebar-cat.php'); ?>
  267. <?php include ('sidebar-archive.php'); ?>
  268.  
  269. <?php
  270. //-------------------------------------------------------------------
  271. //     hide certain widgets from certain sidebar on single page
  272. //-------------------------------------------------------------------
  273.  
  274. add_filter( 'sidebars_widgets', 'wpse17681_sidebars_widgets' );
  275.  
  276. function wpse17681_sidebars_widgets( $sidebars_widgets )
  277. {
  278.     if ( is_page() /* Or whatever */ ) {
  279.  
  280.         foreach ( $sidebars_widgets as $sidebar_id => &$widgets ) {
  281.  
  282.             if ( 'my_sidebar' != $sidebar_id ) {
  283.                 continue;
  284.             }
  285.  
  286.             foreach ( $widgets as $idx => $widget_id ) {
  287.                
  288.                 if ( 0 === strncmp( $widget_id, 'links-', 6 ) ) {
  289.                     unset( $widgets[$idx] );
  290.                 }
  291.             }
  292.         }
  293.     }
  294.  
  295.     return $sidebars_widgets;
  296. }
  297. ?>
  298. <?php
  299. //-------------------------------------------------------------------
  300. //              admin bar is present on screen
  301. //-------------------------------------------------------------------
  302.  
  303. if ( is_admin_bar_showing() ) {
  304.     // do something
  305. }
  306. ?>
  307.  
  308. <?php
  309. //-------------------------------------------------------------------
  310. // altering output of function checking if post is open for comments
  311. //-------------------------------------------------------------------
  312. add_filter( 'comments_open', 'my_comments_open', 10, 2 );
  313.  
  314. //-----------------------------------------------
  315. function my_comments_open( $open, $post_id ) {
  316. //-----------------------------------------------
  317.  
  318.     $post = get_post( $post_id );
  319.  
  320.     if ( 'page' == $post->post_type )
  321.         $open = false;
  322.  
  323.     return $open;
  324. }
  325.  
  326. //-------------------------------------------------------------------
  327. //                  enqueue custom script
  328. //-------------------------------------------------------------------
  329. add_action( 'wp_print_scripts', 'myplugin_scripts' );
  330.  
  331. //--------------------------------
  332. function myplugin_scripts(){
  333. //--------------------------------
  334.   if ( is_single() && comments_open() ) {
  335.  
  336.     // myplugin_script must have been previously registered via wp_register_script()
  337.  
  338.     wp_enqueue_script( 'myplugin_script' );
  339.   }
  340.  
  341.   if ( is_active_widget( false, false, $this->id_base, true ) ) {
  342.       wp_enqueue_script( 'jquery' );
  343.   }
  344. }
  345.  
  346.  
  347.  
  348. ?>
  349.  
  350. <?php
  351. //-------------------------------------------------------------------
  352. //                  registering custom widget
  353. //-------------------------------------------------------------------
  354.  
  355.  
  356. class MyNewWidget extends WP_Widget {
  357.  
  358.         //------------------------------
  359.     function MyNewWidget() {
  360.        
  361.         parent::__construct( false, 'My New Widget Title' );
  362.     }
  363.  
  364.         //------------------------------
  365.     function widget( $args, $instance ) {
  366.         // Widget output
  367.     }
  368.  
  369.         //------------------------------
  370.     function update( $new_instance, $old_instance ) {
  371.         // Save widget options
  372.     }
  373.         //------------------------------
  374.     function form( $instance ) {
  375.         // Output admin widget options form
  376.     }
  377. }
  378.  
  379. //------------------------------
  380. function myplugin_register_widgets() {
  381.     register_widget( 'MyNewWidget' );
  382. }
  383. //------------------------------
  384. add_action( 'widgets_init', 'myplugin_register_widgets' );
  385.  
  386.  
  387. //-------------------------------------------------------------------
  388. //                  output standart widget
  389. //-------------------------------------------------------------------
  390.  
  391.  the_widget( 'WP_Widget_Archives', $instance, $args ); ?>
  392.  
  393. <?php
  394. //-------------------------------------------------------------------
  395. //             Call a specific sidebar widget
  396. //-------------------------------------------------------------------
  397. // For your functions.php
  398. /**
  399.  * Filter callback
  400.  *
  401.  * @param array $sidebars_widgets
  402.  * @return mixed bool/object $sidebars_widgets The widget or FALSE if not found
  403.  */
  404. //-----------------------------------------------------
  405. function filter_sidebars_widgets( $sidebars_widgets )
  406. //-----------------------------------------------------
  407. {
  408.    
  409.     $wanted = 'recent-posts-2';
  410.  
  411.     //---------------------------------------------------------
  412.     // Prevent intercepting another call - on demand filter(!)
  413.     //---------------------------------------------------------
  414.     remove_filter( current_filter(), __FUNCTION__ );
  415.  
  416.     if ( is_array( $sidebars_widgets ) )
  417.     {
  418.         //---------------------------------------------
  419.         // array_search() returns FALSE
  420.         //   in case the widget isn't present
  421.         //---------------------------------------------
  422.  
  423.         $index            = array_search( $wanted, $sidebars_widgets, FALSE );
  424.         $sidebars_widgets = $sidebars_widgets[ $index ];
  425.     }
  426.     else
  427.     {
  428.         //------------------------------------
  429.         // we add a manual FALSE
  430.         // in case the widget isn't present
  431.         //------------------------------------
  432.         $sidebars_widgets = $wanted === $sidebars_widgets ? $sidebars_widgets : FALSE;
  433.     }
  434.  
  435.     return $sidebars_widgets;
  436. }
  437. //-----------------------------------------------
  438. // In your template: First add the filter...
  439. //-----------------------------------------------
  440. add_filter( 'sidebars_widgets', 'filter_sidebars_widgets' );
  441.  
  442. //-----------------------------------------------
  443. // ...then call the function.
  444. //-----------------------------------------------
  445. $widget = wp_get_sidebars_widgets();
  446.  
  447. // Now do something with $widget :)
  448.  
  449. // As we removed the filter inside the callback, any other call
  450. // to wp_get_sidebars_widgets(); will behave like normal.
  451.  
  452. //-------------------------------------------------------------------
  453. /*
  454. * wrapper function for showing sidebars, so that we can customise widget output
  455. * @param int|string $index Optional, default is 1. Name or ID of dynamic sidebar.
  456. * @return bool True, if widget sidebar was found and called. False if not found or not called.
  457. */
  458. //------------------------------------------------------------------
  459. function theme_dynamic_sidebar($index = 1) {
  460.  
  461.     // capture output from the widgets
  462.     ob_start();
  463.     $result = dynamic_sidebar($index);
  464.     $out = ob_get_clean();
  465.  
  466.     //---------------------------------------------------
  467.     //    replace SendBlaster OptIn widget
  468.     //    Location custom field with drop-down list
  469.     //---------------------------------------------------
  470.     $list = <<<HTML
  471. Location <select size="1" name="$1" id="$2" >
  472.     <option value=""></option>
  473.     <option value="ACT">ACT</option>
  474.     <option value="SA">SA</option>
  475.     <option value="NT">NT</option>
  476.     <option value="QLD">QLD</option>
  477.     <option value="NSW">NSW</option>
  478.     <option value="VIC">VIC</option>
  479.     <option value="TAS">TAS</option>
  480.     <option value="WA">WA</option>
  481.     <option value="NZ">New Zealand</option>
  482.     <option value="US">United States</option>
  483.     <option value="Other">Other</option>
  484. </select>
  485. HTML;
  486.     $out = preg_replace(
  487.        '|Location <input type="text" name="([^"]+)" id="([^"]+)"(?:[^>])*>|', $list, $out);
  488.  
  489.     //----------------------------------------
  490.     // finally, output whatever we have left
  491.     //----------------------------------------
  492.     echo $out;
  493.  
  494.     return $result;
  495. }
  496. //------------------------------------------
  497. theme_dynamic_sidebar('left_sidebar');
  498.  
  499. ?>
  500.  
  501. <?php
  502. //-------------------------------------------------------------------
  503. //              altering comments before output
  504. //-------------------------------------------------------------------
  505.  
  506. function stefan_wrap_comment_text($content) {
  507.  
  508.     return "<div class=\"comment-text\">". $content ."</div>";
  509. }
  510. //----------------------
  511. add_filter('comment_text', 'stefan_wrap_comment_text');
  512. add_filter('comment_text', 'stefan_wrap_comment_text', 1000);
  513.  
  514. ?>
  515. <?php
  516. //-------------------------------------------------------------------
  517. //         altering posts before output on search page
  518. //-------------------------------------------------------------------
  519. function my_the_posts($posts, $query = false) {
  520.     if( is_search() ){
  521.         // do your thing here
  522.     }
  523.     return $posts;
  524. }
  525. add_filter( 'the_posts', 'my_the_posts' );
  526.  
  527. ?>
  528. <?php
  529.  
  530. //-------------------------------------------------------------------
  531. //      conditional output of scripts and styles (any page)
  532. //-------------------------------------------------------------------
  533. // the_posts gets triggered before wp_head
  534. //-------------------------------------------------------------------
  535.  
  536. add_filter('the_posts', 'conditionally_add_scripts_and_styles');
  537.  
  538. //-------------------------------------------------------
  539. function conditionally_add_scripts_and_styles($posts){
  540. //-------------------------------------------------------
  541.  
  542.     if (empty($posts)) return $posts;
  543.  
  544.         //--------------------------------------------
  545.         // use this flag to see
  546.         // if styles and scripts need to be enqueued
  547.         //--------------------------------------------
  548.     $shortcode_found = false;
  549.  
  550.     foreach ($posts as $post) {
  551.         if (stripos($post->post_content, '[code]') !== false) {
  552.             $shortcode_found = true; // bingo!
  553.             break;
  554.         }
  555.     }
  556.  
  557.     if ($shortcode_found) {
  558.        
  559.         wp_enqueue_style('my-style', '/style.css');
  560.         wp_enqueue_script('my-script', '/script.js');
  561.     }
  562.  
  563.     return $posts;
  564. }
  565. ?>
  566.  
  567. <?php
  568. //-------------------------------------------------------------------
  569. //              add custom tab in media editor
  570. //-------------------------------------------------------------------
  571.  
  572. add_filter('media_upload_tabs',
  573.                      'my_media_upload_tabs_filter');
  574.  
  575. //----------------------------------------------
  576. function my_media_upload_tabs_filter($tabs) {
  577. //----------------------------------------------
  578.         unset($tabs["type_url"]);
  579.         unset($tabs['library']);
  580.         $newtab = array('ell_insert_gmap_tab' => __('Google Map','insertgmap'));
  581.  
  582.         return array_merge($tabs,$newtab);
  583. }
  584.  
  585. add_action('media_upload_ell_insert_gmap_tab',
  586.                             'media_upload_ell_gmap_tab');
  587.  
  588. //----------------------------------------------
  589. function media_upload_ell_gmap_tab() {
  590. //----------------------------------------------
  591.     return wp_iframe(
  592.                    'media_upload_ell_gmap_form'
  593.                 , $errors );
  594. }
  595. //----------------------------------------------
  596. function media_upload_ell_gmap_form() {
  597. //----------------------------------------------
  598.     echo media_upload_header();
  599.     ?>
  600.     <h2>HTML Form</h2>
  601.  
  602. <?php
  603. }  
  604.  
  605. //-------------------------------------------------------------------
  606. //                    add custom media tab
  607. //-------------------------------------------------------------------
  608.  
  609. //            add the tab
  610. //--------------------------------------
  611. add_filter('media_upload_tabs',
  612.                              'my_upload_tab');
  613.  
  614. //------------------------------
  615. function my_upload_tab($tabs) {
  616. //------------------------------
  617.     $tabs['mytabname'] = "My Tab Name";
  618.     return $tabs;
  619. }
  620.  
  621. //    call the new tab with wp_iframe
  622. //--------------------------------------
  623. add_action('media_upload_mytabname',
  624.                                 'add_my_new_form');
  625.  
  626. //-----------------------------
  627. function add_my_new_form() {
  628. //-----------------------------
  629.     wp_iframe( 'my_new_form' );
  630. }
  631.  
  632. // the tab content
  633. //--------------------------------------
  634. function my_new_form() {
  635.     echo '<p>Example HTML content goes here.</p>';
  636. }
  637. ?>
  638.  
  639.  
  640. //-------------------------------------------------------------------
  641. //            redirect user on previous page after login
  642. //-------------------------------------------------------------------
  643.  
  644. <a href="<?php echo wp_login_url( get_permalink() ); ?>" title="Login">Login</a>
  645.  
  646. <?php
  647.  
  648. //-------------------------------------------------------------------
  649. //                  create custom rewrite rules
  650. //-------------------------------------------------------------------
  651.  
  652. add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' );
  653. add_filter( 'query_vars','my_insert_query_vars' );
  654. add_action( 'wp_loaded','my_flush_rules' );
  655.  
  656.  
  657. // flush_rules() if our rules are not yet included
  658. //-----------------------------------------------------
  659. function my_flush_rules(){
  660. //-----------------------------------------------------
  661.     $rules = get_option( 'rewrite_rules' );
  662.  
  663.     if ( ! isset( $rules['(project)/(\d*)$'] ) ) {
  664.         global $wp_rewrite;
  665.         $wp_rewrite->flush_rules();
  666.     }
  667. }
  668.  
  669. // Adding a new rule
  670. //-----------------------------------------------------
  671. function my_insert_rewrite_rules( $rules )
  672. //-----------------------------------------------------
  673. {
  674.     $newrules = array();
  675.     $newrules['(project)/(\d*)$'] = 'index.php?pagename=$matches[1]&id=$matches[2]';
  676.     return $newrules + $rules;
  677. }
  678.  
  679. // Adding the id var so that WP recognizes it
  680. //-----------------------------------------------------
  681. function my_insert_query_vars( $vars )
  682. //-----------------------------------------------------
  683. {
  684.     array_push($vars, 'id');
  685.     return $vars;
  686. }
  687. ?>
  688.  
  689. <?php
  690. //-------------------------------------------------------------------
  691. //           add custom rewrite rules via rewrite tag
  692. //-------------------------------------------------------------------
  693. function keywords_create_rewrite_rules( $rewrite ) {
  694.     global $wp_rewrite;
  695.    
  696.     // add rewrite tokens
  697.     $keytag_token = '%tag%';
  698.     $wp_rewrite->add_rewrite_tag( $keytag_token, '(.+)', 'tag=' );
  699.    
  700.     $keywords_structure = $wp_rewrite->root . "tag/$keytag_token";
  701.     $keywords_rewrite = $wp_rewrite->generate_rewrite_rules( $keywords_structure );
  702.    
  703.     return ( $rewrite + $keywords_rewrite );
  704. }
  705. ?>
  706.  
  707. <?php
  708. //-------------------------------------------------------------------
  709. //                add rewrite rules for feeds
  710. //-------------------------------------------------------------------
  711.  
  712. function feed_dir_rewrite( $wp_rewrite ) {
  713.     $feed_rules = array(
  714.         'index.rdf' => 'index.php?feed=rdf',
  715.         'index.xml' => 'index.php?feed=rss2',
  716.         '(.+).xml' => 'index.php?feed=' . $wp_rewrite->preg_index(1)
  717.     );
  718.  
  719.     $wp_rewrite->rules = $feed_rules + $wp_rewrite->rules;
  720.     return $wp_rewrite->rules;
  721. }
  722.  
  723. // Hook in.
  724. add_filter( 'generate_rewrite_rules', 'feed_dir_rewrite' );
  725. ?>
  726.  
  727. <?php
  728. //-------------------------------------------------------------------
  729. //               print non-wp rewrite rules
  730. //-------------------------------------------------------------------
  731.  
  732.     $wp_rewrite->non_wp_rules = array( 'Pattern' => 'Substitution' );
  733.     print_r($wp_rewrite->mod_rewrite_rules());
  734. ?>
  735.  
  736. prints
  737.  
  738.  <IfModule mod_rewrite.c>
  739.  RewriteEngine On
  740.  RewriteBase /wp_home/
  741.  RewriteRule ^Pattern /wp_home/Substitution [QSA,L]
  742.  RewriteCond %{REQUEST_FILENAME} !-f
  743.  RewriteCond %{REQUEST_FILENAME} !-d
  744.  RewriteRule . /wp_home/index.php [L]
  745.  </IfModule>
  746.  
  747.  
  748. <?php
  749. //-------------------------------------------------------------------
  750. //     check if we are on singular page of certain post type
  751. //-------------------------------------------------------------------
  752.  
  753. is_singular(array( 'newspaper', 'book' ));
  754.  
  755. ?>
  756.  
  757. <?php
  758. //-------------------------------------------------------------------
  759. //         select template via  template_include  filter
  760. //-------------------------------------------------------------------
  761.  
  762. add_filter('template_include', 'sermon_template_include');
  763.  
  764. function sermon_template_include($template) {
  765.     if(get_query_var('post_type') == 'wpfc_sermon') {
  766.         if ( is_archive() || is_search() ) :
  767.            if(file_exists(TEMPLATEDIR . '/archive-wpfc_sermon.php'))
  768.               return TEMPLATEDIR . '/archive-wpfc_sermon.php';
  769.            return dirname(__FILE__) . '/views/archive-wpfc_sermon.php';
  770.         else :
  771.            if(file_exists(TEMPLATEDIR . '/single-wpfc_sermon.php'))
  772.               return TEMPLATEDIR . '/single-wpfc_sermon.php';
  773.            return dirname(__FILE__) . '/views/single-wpfc_sermon.php';
  774.         endif;
  775.     }
  776.     return $template;
  777. }
  778.  
  779. ?>
  780.  
  781. <?php
  782. //-------------------------------------------------------------------
  783. //      select template via  template_redirect
  784. //           for custom post type
  785. //-------------------------------------------------------------------
  786. function my_template_redirect()
  787.   {
  788.   global $wp;
  789.   global $wp_query;
  790.   if ($wp->query_vars["post_type"] == "your_custom_post_type")
  791.   {
  792.     if (have_posts())
  793.       {
  794.           include(TEMPLATEPATH . '/your_custom_post_type_template.php');
  795.           die();
  796.       }
  797.       else
  798.       {
  799.           $wp_query->is_404 = true;
  800.       }
  801.     }
  802. }
  803.  
  804. add_action("template_redirect", 'my_template_redirect');
  805.  
  806. ?>
  807.  
  808. <?php
  809. //-------------------------------------------------------------------
  810. //          select template via template redirect
  811. //-------------------------------------------------------------------
  812. add_action('template_redirect', 'mcd_set_template');
  813.  
  814. function mcd_set_template() {
  815.     $template_path = TEMPLATEPATH . '/' . "templatename.php";
  816.     if(file_exists($template_path)){
  817.         include($template_path);
  818.         exit;
  819.     }
  820. }
  821. ?>
  822.  
  823. <?php
  824. //-------------------------------------------------------------------
  825. //            usage of template_include filter
  826. //-------------------------------------------------------------------
  827. add_action('template_include', 'mcd_set_template');
  828.  
  829. function mcd_set_template() {
  830.     return locate_template('templatename.php');
  831. }
  832.  
  833. ?>
  834.  
  835. <?php
  836. //-------------------------------------------------------------------
  837. //   select custom template for certain category with fallback
  838. //-------------------------------------------------------------------
  839.  
  840. add_filter('category_template',
  841.  
  842.               'filter_category_template');
  843.  
  844. //--------------------------------------------
  845. function filter_category_template($template){
  846. //--------------------------------------------
  847.  
  848.     /* Get current category */
  849.     $category = get_queried_object();
  850.  
  851.     /* Create hierarchical list of desired templates */
  852.  
  853.     $templates = array (
  854.       'category.php',
  855.       'custom-category-template.php',
  856.       'category-{$category->slug}.php',
  857.       'category-{$category->term_id}.php',
  858.       'index.php'
  859.     );
  860.  
  861.  
  862.     return locate_template($templates);
  863. }
  864. ?>
  865.  
  866. <?php
  867. //-------------------------------------------------------------------
  868. //    select category template according to its parent category
  869. //-------------------------------------------------------------------
  870.  
  871. add_filter( 'template_include',
  872.                                 'ja_template_check' );
  873.  
  874. //--------------------------------------------
  875. function ja_template_check( $template ) {
  876. //--------------------------------------------
  877.     if ( is_category() ){
  878.  
  879.        
  880.         $cat = get_query_var( 'cat' );
  881.         $category_info = get_category( $cat );
  882.  
  883.         if ( $category_info->parent == 7 ){
  884.             return get_stylesheet_directory() . '/subcategory-news.php';
  885.         }
  886.     }
  887.     return $template;
  888.  
  889. }
  890. ?>
  891.  
  892. <?php
  893. //-------------------------------------------------------------------
  894. //   select nav menu according to current category
  895. //-------------------------------------------------------------------
  896.  
  897. // если это категория 4 или её подкатегория  
  898.  
  899. if (cat_is_ancestor_of(4, $cat) or is_category(4)) {  
  900.       wp_nav_menu( array('menu' => 'Music' ));  
  901. } ?>  
  902.  
  903. <?php
  904. //-------------------------------------------------------------------
  905. //         redirecting according to referer
  906. //-------------------------------------------------------------------
  907.  
  908. if ( wp_get_referer() )
  909. {
  910.     wp_safe_redirect( wp_get_referer() );
  911. }
  912. else
  913. {
  914.     wp_safe_redirect( get_home_url() );
  915. }
  916. ?>
  917.  
  918. <?php
  919. //-------------------------------------------------------------------
  920. //            add custom filter to admin post list
  921. //-------------------------------------------------------------------
  922.  
  923. add_action('restrict_manage_posts',
  924.                        'scompt_restrict_manage_posts');
  925.  
  926. //-----------------------------------------
  927. function scompt_restrict_manage_posts() {
  928. //-----------------------------------------
  929.     ?>
  930.         <form name="scompt_attachmentform" id="scompt_attachmentform" action="" method="get">
  931.             <fieldset>
  932.             <legend><?php _e('Attachments'); ?>&hellip;</legend>
  933.             <select name='scompt_attachments' id='scompt_attachments' class='postform'>
  934.                 <option value=""><?php _e('All'); ?></option>
  935.                 <option value="has"><?php _e('Has attachments'); ?></option>
  936.                 <option value="hasnt"><?php _e('Has no attachments'); ?></option>
  937.             </select>
  938.             <input type="submit" name="submit" value="<?php _e('Filter') ?>" class="button" />
  939.         </fieldset>
  940.         </form>
  941.     <?php
  942. }
  943.  
  944. //-----------------------------------------
  945. add_filter('posts_where',
  946.                        'scompt_posts_where');
  947.  
  948. //-----------------------------------------
  949. function scompt_posts_where($where) {
  950. //-----------------------------------------
  951.     if( is_admin() ) {
  952.         global $wpdb;
  953.         if( $_GET['scompt_attachments'] == 'has' ) {
  954.  
  955.             $where .=
  956.                 " AND ID IN (SELECT post_parent FROM {$wpdb->posts} WHERE post_type='attachment' )";
  957.         } else if( $_GET['scompt_attachments'] == 'hasnt' ) {
  958.             $where .=
  959.                 " AND ID NOT IN (SELECT post_parent FROM {$wpdb->posts} WHERE post_type='attachment' )";
  960.         }
  961.     }
  962.     return $where;
  963. }
  964.  
  965. //------------------------------------------------------
  966. //   also count posts with and without attachments
  967. //------------------------------------------------------
  968. add_action('restrict_manage_posts',
  969.                           'scompt_restrict_manage_posts');
  970.  
  971. //-----------------------------------------
  972. function scompt_restrict_manage_posts() {
  973. //-----------------------------------------
  974.     global $wpdb;
  975.     $has_count = $wpdb->get_var(
  976.  
  977.              "SELECT COUNT(*) FROM wp_posts WHERE post_type='post'
  978.             AND ID IN (SELECT post_parent FROM wp_posts WHERE post_type='attachment')");
  979.  
  980.     $hasnt_count = $wpdb->get_var(
  981.  
  982.             "SELECT COUNT(*) FROM wp_posts WHERE post_type='post'
  983.            AND ID NOT IN (SELECT post_parent FROM wp_posts WHERE post_type='attachment')");
  984.  
  985.     ?>
  986.       <form name="scompt_attachmentform" id="scompt_attachmentform" action="" method="get">
  987.           <fieldset style="margin: 0pt 1em 1em 1.5em; padding: 0pt; float: left;">
  988.               <legend><?php _e('Attachments'); ?>&hellip;</legend>
  989.  
  990.               <select name='scompt_attachments' id='scompt_attachments' class='postform'>
  991.  
  992.                   <option value=""><?php _e('All'); ?></option>
  993.  
  994.                   <option value="has" <?php if( isset($_GET['scompt_attachments']) &&
  995.               $_GET['scompt_attachments']=='has')
  996.             echo 'selected="selected"' ?>><?php _e('Has attachments'); ?>  
  997.              (<?php echo $has_count ?>)</option>
  998.  
  999.                   <option value="hasnt" <?php if( isset($_GET['scompt_attachments']) &&
  1000.                  $_GET['scompt_attachments']=='hasnt') echo 'selected="selected"' ?>>
  1001. <?php _e('Has no attachments'); ?>  (<?php echo $hasnt_count ?>)</option>
  1002.  
  1003.               </select>
  1004.           </fieldset>
  1005.  
  1006.           <input type="submit" name="submit" value="<?php _e('Filter & #187;');
  1007.            ?>" class="button" style="float:left;margin:14px 0pt 1em;position:relative;top:0.35em;"/>
  1008.       </form>
  1009.   <?php
  1010. }
  1011.  
  1012.  
  1013. //-----------------------------------------
  1014. add_action('admin_head',
  1015.                      'scompt_admin_head');
  1016.  
  1017. //-----------------------------------------
  1018. function scompt_admin_head() {
  1019. //-----------------------------------------
  1020.     add_filter('posts_where', 'scompt_posts_where');
  1021. }
  1022. //-----------------------------------------
  1023. function scompt_posts_where($where) {
  1024. //-----------------------------------------
  1025.     global $wpdb;
  1026.     if( $_GET['scompt_attachments'] == 'has' ) {
  1027.  
  1028.         $where .= " AND ID IN
  1029.                      (SELECT post_parent FROM {$wpdb->posts} WHERE post_type='attachment' )";
  1030.  
  1031.     } else if( $_GET['scompt_attachments'] == 'hasnt' ) {
  1032.  
  1033.         $where .= " AND ID NOT IN
  1034.                        (SELECT post_parent FROM {$wpdb->posts} WHERE post_type='attachment' )";
  1035.     }
  1036.     return $where;
  1037. }
  1038. ?>
  1039.  
  1040. <?php
  1041. //-------------------------------------------------------------------
  1042. //      add filters by custom taxonomies for custom post type
  1043. //-------------------------------------------------------------------
  1044.  
  1045. //-------------------------------------------
  1046. function work_restrict_manage_posts() {
  1047. //-------------------------------------------
  1048.     global $typenow;
  1049.     $args=array( 'public' => true, '_builtin' => false );
  1050.     $post_types = get_post_types($args);
  1051.  
  1052.     if ( in_array($typenow, $post_types) ) {
  1053.  
  1054.     $filters = get_object_taxonomies($typenow);
  1055.         foreach ($filters as $tax_slug) {
  1056.             $tax_obj = get_taxonomy($tax_slug);
  1057.  
  1058.             wp_dropdown_categories(array(
  1059.                 'show_option_all' => __('Alle '.$tax_obj->label ),
  1060.                 'taxonomy' => $tax_slug,
  1061.                 'name' => $tax_obj->name,
  1062.                 'orderby' => 'term_order',
  1063.                 'selected' => $_GET[$tax_obj->query_var],
  1064.                 'hierarchical' => $tax_obj->hierarchical,
  1065.                 'show_count' => false,
  1066.                 'hide_empty' => true
  1067.             ));
  1068.         }
  1069.     }
  1070. }
  1071.  
  1072. //-------------------------------------------
  1073. function work_convert_restrict($query) {
  1074. //-------------------------------------------
  1075.     global $pagenow;
  1076.     global $typenow;
  1077.  
  1078.     if ($pagenow=='edit.php') {
  1079.  
  1080.         $filters = get_object_taxonomies($typenow);
  1081.  
  1082.         foreach ($filters as $tax_slug) {
  1083.  
  1084.             $var = &$query->query_vars[$tax_slug];
  1085.  
  1086.             if ( isset($var) && $var>0) {
  1087.                 $term = get_term_by('id',$var,$tax_slug);
  1088.                 $var = $term->slug;
  1089.             }
  1090.         }
  1091.     }
  1092.     return $query;
  1093. }
  1094.  
  1095. //--------------------------------------
  1096. add_action( 'restrict_manage_posts',
  1097.                              'work_restrict_manage_posts' );
  1098. //--------------------------------------
  1099. add_filter('parse_query',
  1100.                        'work_convert_restrict');
  1101. ?>
  1102.  
  1103.  
  1104. <?php
  1105. //-------------------------------------------------------------------
  1106. //          add sort by meta field for custom post type
  1107. //-------------------------------------------------------------------
  1108. add_action('restrict_manage_posts',
  1109.                  'restrict_manage_movie_sort_by_genre');
  1110.  
  1111. //-------------------------------------------------
  1112. function restrict_manage_movie_sort_by_genre() {
  1113. //-------------------------------------------------
  1114.     if (isset($_GET['post_type'])) {
  1115.         $post_type = $_GET['post_type'];
  1116.  
  1117.         if (post_type_exists($post_type) && $post_type=='movie') {
  1118.             global $wpdb;
  1119.  
  1120.             $sql=<<<SQL
  1121. SELECT pm.meta_key FROM {$wpdb->postmeta} pm
  1122. INNER JOIN {$wpdb->posts} p ON p.ID=pm.post_id
  1123. WHERE p.post_type='movie' AND pm.meta_key='Genre'
  1124. GROUP BY pm.meta_key
  1125. ORDER BY pm.meta_key
  1126. SQL;
  1127.  
  1128.             $results = $wpdb->get_results($sql);
  1129.  
  1130.             $html = array();
  1131.             $html[] = "<select id=\"sortby\" name=\"sortby\">";
  1132.             $html[] = "<option value=\"None\">No Sort</option>";
  1133.  
  1134.             $this_sort = $_GET['sortby'];
  1135.  
  1136.             foreach($results as $meta_key) {
  1137.  
  1138.                 $default = ($this_sort==$meta_key->meta_key ? ' selected="selected"' : '');
  1139.                 $value = esc_attr($meta_key->meta_key);
  1140.                 $html[] = "<option value=\"{$meta_key->meta_key}\"$default>{$value}</option>";
  1141.             }
  1142.             $html[] = "</select>";
  1143.             echo "Sort by: " . implode("\n",$html);
  1144.         }
  1145.     }
  1146. }
  1147.  
  1148. //--------------------------------------------
  1149. add_filter( 'parse_query',
  1150.  
  1151.                        'sort_movie_by_meta_value' );
  1152.  
  1153. //-------------------------------------------------
  1154. function sort_movie_by_meta_value($query) {
  1155. //-------------------------------------------------
  1156.     global $pagenow;
  1157.     if (is_admin() && $pagenow=='edit.php' &&
  1158.         isset($_GET['post_type']) && $_GET['post_type']=='movie' &&
  1159.         isset($_GET['sortby'])  && $_GET['sortby'] !='None')  {
  1160.         $query->query_vars['orderby'] = 'meta_value';
  1161.         $query->query_vars['meta_key'] = $_GET['sortby'];
  1162.     }
  1163. }
  1164.  
  1165. //-------------------------------------------------
  1166. add_action('manage_movie_posts_columns',
  1167.  
  1168.                                'manage_movie_posts_columns');
  1169.  
  1170. //-----------------------------------------------------
  1171. function manage_movie_posts_columns($posts_columns) {
  1172. //-----------------------------------------------------
  1173.     $posts_columns = array(
  1174.         'cb' => $posts_columns['cb'],
  1175.         'title' => 'Movie Name',
  1176.         );
  1177.     if (isset($_GET['sortby']) && $_GET['sortby'] !='None')
  1178.         $posts_columns['meta_value'] = 'Sorted By';
  1179.  
  1180.     return $posts_columns;
  1181. }
  1182.  
  1183. //-----------------------------------------------------
  1184. add_action('manage_pages_custom_column',
  1185.  
  1186.                    'manage_movie_pages_custom_column',10,2);
  1187.  
  1188. //---------------------------------------------------------------
  1189. function manage_movie_pages_custom_column(
  1190.                                 $column_name,$post_id) {
  1191. //---------------------------------------------------------------
  1192.     global $pagenow;
  1193.     $post = get_post($post_id);
  1194.  
  1195.     if ($post->post_type=='movie' && is_admin() && $pagenow=='edit.php')  {
  1196.  
  1197.         switch ($column_name) {
  1198.             case 'meta_value':
  1199.                 if (isset($_GET['sortby']) && $_GET['sortby'] !='None') {
  1200.                     echo get_post_meta($post_id,$_GET['sortby'],true);
  1201.                 }
  1202.                 break;
  1203.         }
  1204.     }
  1205. }
  1206.  
  1207. ?>
  1208.  
  1209. <?php
  1210. //-------------------------------------------------------------------
  1211. //         Expand Media Library admin panel
  1212. //-------------------------------------------------------------------
  1213.  
  1214. add_action('admin_init', 'init_media_plus');
  1215.  
  1216. //------------------------------------
  1217. function init_media_plus() {
  1218. //------------------------------------
  1219.  
  1220.     // Only activate plugin for the Media Library page
  1221.     //------------------------------------
  1222.  
  1223.     if (strpos($_SERVER["REQUEST_URI"], "upload.php") === FALSE)
  1224.         return;
  1225.      
  1226.     //---------------------------------
  1227.     add_action('restrict_manage_posts',
  1228.                                      'media_library_menu');
  1229.  
  1230.     //---------------------------------
  1231.     add_action('admin_head',
  1232.                              'media_plus_header', 51);
  1233.  
  1234.     //---------------------------------
  1235.     add_filter('wp_redirect',
  1236.                              'media_plus_redirect', 10, 2);
  1237. }
  1238.  
  1239. //------------------------------------
  1240. function media_library_menu() {
  1241. //------------------------------------
  1242.  
  1243.     global $wpdb;
  1244.  
  1245.     ?>
  1246.     <div class="wrap">  
  1247.         <?php screen_icon(); ?>
  1248.         <h2>Media Library Plus</h2>
  1249.  
  1250.         <form id="media-plus-form" action="" method="get">
  1251.             <select name="action" class="select-action">
  1252.             <option value="-1" selected="selected"><?php _e('Bulk Actions'); ?></option>
  1253.             <option value="delete"><?php _e('Delete'); ?></option>
  1254.             <option value="attach"><?php _e('Attach to a post'); ?></option>
  1255.             <option value="remove"><?php _e('Detach from a post'); ?></option>
  1256.  
  1257.             </select>
  1258.          
  1259.             <input type="submit" value="<?php esc_attr_e('Apply'); ?>"
  1260.  
  1261.                         name="doaction" id="doaction" class="button-secondary action"
  1262.  
  1263.                          onClick="getSelectedMedia('posts-filter');" />
  1264.  
  1265.  
  1266.             <?php wp_nonce_field('bulk-media'); ?>
  1267.             <?php find_posts_div(); ?>
  1268.         </form>
  1269.     </div> <!-- End div wrap -->
  1270.     <?php    
  1271. }
  1272.  
  1273. //------------------------------------
  1274. function media_plus_header() {
  1275. //------------------------------------
  1276. ?>
  1277. <script type="text/javascript">
  1278.    <!--
  1279.    // get_selected_media(document.posts-filter.list)
  1280.  
  1281.    
  1282.    function addNewArg(name, value) {
  1283.    //-----------------------------------
  1284.        var newArg = document.createElement("input");
  1285.        newArg.type = "hidden";
  1286.        newArg.name = name;
  1287.        newArg.value = value;
  1288.        newArg.id = name;
  1289.        return newArg;
  1290.    }
  1291.  
  1292.    function getSelectedMedia() {
  1293.    //------------------------------------
  1294.        var mediaPlusForm=document.getElementById('media-plus-form' );
  1295.        var args = document.getElementById('posts-filter').elements;
  1296.      
  1297.        for (i = 0; i < args.length; i++) {
  1298.            if (args[i].name == "media[]" && args[i].checked) {
  1299.                mediaPlusForm.appendChild(addNewArg("media[]", args[i].value));
  1300.            }
  1301.        }
  1302.  
  1303.        // Redirect to previous page by adding previous mime_type and detached state
  1304.  
  1305.        var hasType = location.href.indexOf('post_mime_type');
  1306.  
  1307.        if (hasType >= 0) {
  1308.            var sPos = location.href.indexOf('=', mimeType);
  1309.            var ePos = location.href.indexOf('&', sPos);
  1310.          
  1311.            if (ePos >= 0) {
  1312.                var mimeStr = location.href.substring(sPos+1, ePos);
  1313.            } else {
  1314.                var mimeStr = location.href.substring(sPos+1);
  1315.            }
  1316.  
  1317.            mediaPlusForm.appendChild(addNewArg('post_mime_type', mimeStr));
  1318.        }
  1319.      
  1320.        if (location.href.indexOf('detached') >= 0) {
  1321.             mediaPlusForm.appendChild(addNewArg('detached', '1'));
  1322.        }
  1323. }
  1324. //-->
  1325. </script>
  1326. <?php
  1327. }
  1328.  
  1329. // Change redirect set in upload.php
  1330. //----------------------------------------------------
  1331. function media_plus_redirect($location, $status) {
  1332. //----------------------------------------------------
  1333.     if ( isset($_GET['found_post_id']) && isset($_GET['media']) ) {
  1334.         if (!isset($_GET['detached']))
  1335.             $location = remove_query_arg('detached', $location);
  1336.     }
  1337.     return $location;
  1338. }
  1339.  
  1340. //----------------------------------------------------
  1341. function detach_media_action() {
  1342. //----------------------------------------------------
  1343.  
  1344.     // $_GET['doaction'] should be 'Apply'
  1345.     global $wpdb;
  1346.      
  1347.     if ( !strcmp($_GET['action'], 'remove') && isset($_GET['media']) ) {
  1348.         check_admin_referer('bulk-media');
  1349.      
  1350.         $attach = array();
  1351.         foreach( (array) $_GET['media'] as $att_id ) {
  1352.             $att_id = (int) $att_id;
  1353.      
  1354.             if ( !current_user_can('edit_post', $att_id) )
  1355.                 continue;
  1356.      
  1357.             $attach[] = $att_id;
  1358.         }
  1359.      
  1360.         if ( ! empty($attach) ) {
  1361.             $attach = implode(',', $attach);
  1362.             $attached = $wpdb->query( $wpdb->prepare(
  1363.  
  1364.            "UPDATE $wpdb->posts SET post_parent = %d
  1365.              WHERE post_type = 'attachment' AND ID IN ($attach)", '') );
  1366.         }
  1367.      
  1368.         if ( isset($attached) ) {
  1369.             $location = 'upload.php';
  1370.             if ( $referer = wp_get_referer() ) {
  1371.                 if ( false !== strpos($referer, 'upload.php') )
  1372.                     $location = $referer;
  1373.             }
  1374.      
  1375.             $location = add_query_arg( array( 'attached' => $attached ) , $location );
  1376.             // redirect after header here can't use wp_redirect($location);
  1377.             ?>
  1378.               <script type="text/javascript">
  1379.               <!--
  1380.               window.location= <?php echo "'" . $location . "'"; ?>;
  1381.               //-->
  1382.               </script>
  1383.             <?php
  1384.             exit;
  1385.         }
  1386.          
  1387.     }
  1388. }
  1389. ?>
  1390.  
  1391. <?php
  1392. //-------------------------------------------------------------------
  1393. //                  altering add_media screen
  1394. //-------------------------------------------------------------------
  1395.  
  1396. //----------------------------------------
  1397. add_action('wp_enqueue_scripts',
  1398.                                   array($this, 'add_js_css'));
  1399.  
  1400. //----------------------------------------
  1401. add_filter('attachment_fields_to_edit',
  1402.  
  1403.                                  array($this, 'attachment_fields'), 15, 2);
  1404.  
  1405. //--------------------------------------------------
  1406. public function attachment_fields($form_fields) {
  1407. //--------------------------------------------------
  1408.     global $post;
  1409.  
  1410.     $file = wp_get_attachment_url($post->ID);
  1411.  
  1412.     unset($form_fields['post_excerpt']);
  1413.     unset($form_fields['post_content']);
  1414.     unset($form_fields['url']['helps']);
  1415.  
  1416.     $form_fields['url']['label'] = 'URL';
  1417.  
  1418.     $form_fields['url']['html'] = "<input type='text' class='text urlfield'
  1419.                  name='attachments[$post->ID][url]' value='" . esc_attr($file) . "' /><br />";
  1420.  
  1421.     $form_fields['buttons'] = array(
  1422.     'label' => '',
  1423.     'value' => '',
  1424.     'html' => "<input type='submit' class='button'
  1425.            name='send[$post->ID]' value='" . esc_attr__( 'Add MP3' ) . "' />",
  1426.  
  1427.     'input' => 'html'
  1428.  
  1429.     );
  1430.  
  1431.     return $form_fields;
  1432. }
  1433. ?>
  1434.  
  1435. <?php
  1436. //-------------------------------------------------------------------
  1437. //             remove tabs from add_media screen
  1438. //-------------------------------------------------------------------
  1439.  
  1440. add_filter('media_upload_tabs',
  1441.                          'remove_media_library_tab');
  1442.  
  1443. //--------------------------------------------
  1444. function remove_media_library_tab($tabs) {
  1445. //--------------------------------------------
  1446.  
  1447.     if (isset($_REQUEST['post_id'])) {
  1448.  
  1449.         $post_type = get_post_type($_REQUEST['post_id']);
  1450.  
  1451.         if ('post' == $post_type)
  1452.             unset($tabs['library']);
  1453.     }
  1454.     return $tabs;
  1455. }
  1456.  
  1457. //----------------------------------------------
  1458. add_filter( 'media_upload_tabs',
  1459.                         'no_media_library_tab' );
  1460.  
  1461. //--------------------------------------------
  1462. function no_media_library_tab( $tabs ) {
  1463. //--------------------------------------------
  1464.     unset($tabs['library']);
  1465.     return $tabs;
  1466. }
  1467.  
  1468. //--------------------------------------------
  1469. add_filter('media_upload_tabs',
  1470.                       'no_library_tab');
  1471.  
  1472. //--------------------------------------------
  1473. function no_library_tab($tabs) {
  1474. //--------------------------------------------
  1475.     if (!current_user_can('promote_users')) {
  1476.         unset($tabs['library']);
  1477.     }
  1478.     return $tabs;
  1479. }
  1480.  
  1481. ?>
  1482.  
  1483. <?php
  1484. //-------------------------------------------------------------------
  1485. //        add custom tab to add_media screen
  1486. //-------------------------------------------------------------------
  1487.  
  1488. add_filter('media_upload_tabs',
  1489.                    'upload_test_media_menu');
  1490.  
  1491. //------------------------------------------
  1492. function upload_test_media_menu($tabs) {
  1493. //------------------------------------------
  1494.  
  1495.    $tabs['test_upload']='Test Upload';
  1496.    return $tabs;
  1497. }
  1498. //--------------------------------------------------
  1499. //   Needed script to make sure
  1500. //   wordpresses media upload scripts are inplace
  1501. //--------------------------------------------------
  1502. wp_enqueue_script('media-upload');
  1503.  
  1504.  
  1505. //---------------------------------------------
  1506. add_action('media_upload_test_upload',
  1507.                    'upload_test_menu_handle');
  1508.  
  1509. //---------------------------------------------
  1510. //      This is our form for the plugin
  1511. //---------------------------------------------
  1512. function upload_test_upload_handle () {
  1513. //---------------------------------------------
  1514.  
  1515.    media_upload_header();
  1516.  
  1517. ?>
  1518. <div class="test-form">
  1519. Here's some stuff in our plugin
  1520. </div>
  1521.  
  1522. <?php }
  1523.   {
  1524.  
  1525.    return wp_iframe('upload_test_upload_form');
  1526. }
  1527.  
  1528.  
  1529.  
  1530. //---------------------------------------------
  1531. function upload_test_upload_form () {
  1532. //---------------------------------------------
  1533. media_upload_header();
  1534. ?>
  1535. <div class="test-form">
  1536. <input type='text' id='name' />
  1537. <input id='insert_shortcode' type='button' class='button' value='Insert Shortcode'>
  1538. </div>
  1539. <?php }
  1540.  
  1541.  
  1542. //---------------------------------------------
  1543. function upload_test_scripts() {
  1544. //---------------------------------------------
  1545.  
  1546. $myStyleUrl = plugins_url('upload-media.css', __FILE__);
  1547. wp_register_style('myStyleSheets', $myStyleUrl);
  1548. wp_enqueue_style( 'myStyleSheets');
  1549.  
  1550. wp_deregister_script('jquery');
  1551. wp_register_script( 'jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js');
  1552. wp_enqueue_script( 'jquery' );
  1553.  
  1554. wp_deregister_script( 'upload-media-script' );
  1555. wp_register_script( 'upload-media-script', '/wp-content/plugins/upload-media/upload-media.js');
  1556. wp_enqueue_script( 'upload-media-script' );
  1557. }
  1558.  
  1559. ?>
  1560.  
  1561. //------------------------------------
  1562. upload_media.js
  1563. //------------------------------------
  1564. (function ($) {
  1565. $(function () {
  1566. console.log('Our Plugin Works')
  1567. });
  1568. })(jQuery);
  1569.  
  1570. (function ($) {
  1571.  
  1572.     insertShortcode = function(name) {
  1573.     var win = window.dialogArguments || opener || parent || top;
  1574.     var shortcode='[testcode name='+name+']';
  1575.     win.send_to_editor(shortcode);
  1576. }
  1577.  
  1578. $(function () {
  1579.  
  1580.    $('#insert_shortcode').bind('click',function() {
  1581.    var name = $('#name').val();
  1582.    insertShortcode(name);});
  1583. });
  1584. })(jQuery);
  1585.  
  1586.  
  1587. <?php
  1588. //-------------------------------------------------------------------
  1589. //       Removes specified media tabs from the media uploader.
  1590. //-------------------------------------------------------------------
  1591.  
  1592. add_filter( 'media_upload_tabs', 'cwwp_remove_media_tabs' );
  1593.  
  1594. //----------------------------------------------
  1595. function cwwp_remove_media_tabs( $tabs ) {
  1596. //----------------------------------------------
  1597.    
  1598.    
  1599.     unset( $tabs['type'] ); // This removes the "From Computer" tab
  1600.     unset( $tabs['type_url'] ); // This removes the "From URL" tab
  1601.     unset( $tabs['gallery'] ); // This removes the "Gallery" tab
  1602.     unset( $tabs['library'] ); // This remove the "Media Library" tab
  1603.        
  1604.     return $tabs;
  1605.    
  1606. }
  1607. ?>
  1608.  
  1609. <?php
  1610. //-------------------------------------------------------------------
  1611. //       show list of terms in custom taxonomy for post
  1612. //-------------------------------------------------------------------
  1613. $product_terms = wp_get_object_terms($post->ID, 'product');
  1614. if(!empty($product_terms)){
  1615.   if(!is_wp_error( $product_terms )){
  1616.     echo '<ul>';
  1617.     foreach($product_terms as $term){
  1618.       echo '<li><a href="'.get_term_link($term->slug, 'product').'">'.$term->name.'</a></li>';
  1619.     }
  1620.     echo '</ul>';
  1621.   }
  1622. }
  1623.  
  1624. ?>
  1625.  
  1626. <?php
  1627. //-------------------------------------------------------------------
  1628. //         redirect 404 error page to custom template
  1629. //-------------------------------------------------------------------
  1630.  
  1631. //----------------------------------------------
  1632. function loadWordPressTemplate($template) {
  1633. //----------------------------------------------
  1634.  
  1635.     global $wp_query;
  1636.  
  1637.     if ($wp_query->is_404) {
  1638.  
  1639.         $wp_query->is_404 = false;
  1640.         $wp_query->is_archive = true;
  1641.     }
  1642.    
  1643.     header("HTTP/1.1 200 OK");
  1644.     include($template);
  1645.     exit;
  1646. }
  1647.  
  1648. //----------------------------------------------
  1649. function templateRedirect() {
  1650. //----------------------------------------------
  1651.     global $wp;
  1652.  
  1653.     if ($wp->request == 'some-custom-url-that-does-not-exist') {
  1654.         loadWordPressTemplate(TEMPLATEPATH . '/super-custom-template.php');
  1655.     }
  1656. }
  1657.  
  1658. //----------------------------------------------
  1659. add_action('template_redirect',
  1660.                   'templateRedirect');
  1661.  
  1662. ?>
  1663.  
  1664. <?php
  1665.  
  1666. //-------------------------------------------------------------------
  1667. //           create popup window using thickbox
  1668. //-------------------------------------------------------------------
  1669.  
  1670. add_action('init', 'init_theme_method');
  1671.  
  1672. function init_theme_method() {
  1673.    add_thickbox();
  1674. }
  1675.  
  1676. ?>
  1677.  
  1678. <div style="text-align:center;padding:20px;">
  1679.  
  1680.     <input alt="#TB_inline?height=300&amp;width=400&amp;inlineId=examplePopup1"
  1681.  
  1682.       title="add a caption to title attribute / or leave blank"
  1683.       class="thickbox" type="button"
  1684.       value="Show Thickbox Example Pop-up 1" />  
  1685.  
  1686. </div>
  1687.  
  1688. <div id="examplePopup1" style="display:none">
  1689.  
  1690.       <h2>Example Pop-up Window 1</h2>
  1691.  
  1692.       <div style="float:left;padding:10px;">
  1693.          <img src="http://shibashake.com/wordpress-theme/wp-content/uploads/2010/03/bio1.jpg"  
  1694.                width="150" height = "168"/>
  1695.       </div>
  1696.  
  1697.       I was born at DAZ Studio.
  1698.       They created me with utmost care and that is
  1699.       why I am the hottie that you see today.
  1700.       My interests include posing, trying out cute outfits,
  1701.       and more posing.
  1702.  
  1703.       <select name=""><option>test</option></select>
  1704.  
  1705.       <strong>Just click outside the pop-up to close it.</strong>
  1706. </div>
  1707.  
  1708.  
  1709. //--------------------------------------------------
  1710. add_action('template_redirect', 'add_scripts');
  1711.  
  1712. //-------------------------------------
  1713. function add_scripts() {
  1714. //-------------------------------------
  1715.     if (is_single('2794')) {
  1716.       add_thickbox();
  1717.     }
  1718. }
  1719.  
  1720. //-------------------------------------
  1721. function add_scripts() {
  1722. //-------------------------------------
  1723.     global $wp_query;
  1724.     if (!is_singular() || !$wp_query->post) return;
  1725.  
  1726.     //------------------------
  1727.     // Get post tags
  1728.     //------------------------
  1729.     $tags = wp_get_object_terms($wp_query->post->ID, 'post_tag');
  1730.     foreach ($tags as $tag) {
  1731.         if ($tag->name == "thickbox") {
  1732.             add_thickbox();
  1733.             break;
  1734.         }
  1735.     }  
  1736. }
  1737. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement