Advertisement
terorama

Wordpress Snippets 5

Oct 13th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 43.21 KB | None | 0 0
  1. <?php
  2.  
  3. //-------------------------------------------------------------------
  4. //        rating taxonomy with custom urls
  5. //-------------------------------------------------------------------
  6. add_action('init', 'my_rating_init');
  7.  
  8. //--------------------------------
  9. function my_rating_init() {
  10. //--------------------------------
  11.     if (!is_taxonomy('rating')) {
  12.  
  13.         register_taxonomy( 'rating', 'post',
  14.  
  15.                    array(   'hierarchical' => FALSE, 'label' => __('Rating'),  
  16.                         'public' => TRUE, 'show_ui' => TRUE,
  17.  
  18.                         'query_var' => 'rating',
  19.                         'rewrite' => true ) );
  20.     }
  21. }
  22.  
  23. //--------------------------------
  24. add_filter('post_link', 'rating_permalink', 10, 3);
  25. add_filter('post_type_link', 'rating_permalink', 10, 3);
  26.  
  27. //----------------------------------
  28. //    creating urls on pages
  29. //----------------------------------
  30. function rating_permalink(
  31. //--------------------------------
  32.                  $permalink,
  33.                  $post_id,
  34.                  $leavename) {
  35. //--------------------------------
  36.  
  37.     if (strpos($permalink, '%rating%') === FALSE) return $permalink;
  38.      
  39.         // Get post
  40.         //---------------------------
  41.         $post = get_post($post_id);
  42.         if (!$post) return $permalink;
  43.  
  44.         // Get taxonomy terms
  45.  
  46.         $terms = wp_get_object_terms($post->ID, 'rating');
  47.    
  48.         if (!is_wp_error($terms) && !empty($terms) && is_object($terms[0]))
  49.  
  50.             $taxonomy_slug = $terms[0]->slug;
  51.         else
  52.             $taxonomy_slug = 'not-rated';
  53.  
  54.     return str_replace('%rating%', $taxonomy_slug, $permalink);
  55. }  
  56.  
  57. ?>
  58.  
  59. <?php
  60. //-------------------------------------------------------------------
  61. //                 output rewrite rules
  62. //-------------------------------------------------------------------
  63. global $wp_rewrite;
  64. $wp_rewrite->flush_rules();
  65. ?>
  66.  
  67. 'rewrite' => array('slug' => 'shiba-gallery', 'with_front' => FALSE)
  68.  
  69.  
  70.  
  71. <?php
  72. //-------------------------------------------------------------------
  73. //            post type with custom sef urls
  74. //-------------------------------------------------------------------
  75.  
  76. $args = array(
  77.     'publicly_queryable' => true,
  78.     'query_var' => true,
  79.  
  80.     'rewrite' => false,
  81.             ...
  82. );
  83. //----------------------------------
  84. register_post_type('gallery',$args);
  85.  
  86.  
  87. // add to our plugin init function
  88. //----------------------------------
  89. global $wp_rewrite;
  90.  
  91. $gallery_structure = '/galleries/%year%/%monthnum%/%gallery%';
  92. $wp_rewrite->add_rewrite_tag("%gallery%", '([^/]+)', "gallery=");
  93. $wp_rewrite->add_permastruct('gallery', $gallery_structure, false);
  94.  
  95.  
  96. // Add filter to plugin init function
  97. //-------------------------------------
  98. add_filter('post_type_link', 'gallery_permalink', 10, 3);  
  99.  
  100.  
  101. // Adapted from get_permalink function in wp-includes/link-template.php
  102. //----------------------------------
  103. //     creating urls
  104. //----------------------------------
  105. function gallery_permalink(
  106.                     $permalink,
  107.                     $post_id,
  108.                     $leavename) {
  109. //----------------------------------
  110.  
  111.     $post = get_post($post_id);
  112.  
  113.     $rewritecode = array(
  114.         '%year%',
  115.         '%monthnum%',
  116.         '%day%',
  117.         '%hour%',
  118.         '%minute%',
  119.         '%second%',
  120.         $leavename? '' : '%postname%',
  121.         '%post_id%',
  122.         '%category%',
  123.         '%author%',
  124.         $leavename? '' : '%pagename%',
  125.     );
  126.  
  127.     if ( '' != $permalink &&
  128.          !in_array($post->post_status, array('draft', 'pending', 'auto-draft')) ) {
  129.  
  130.         $unixtime = strtotime($post->post_date);
  131.      
  132.         $category = '';
  133.  
  134.         if ( strpos($permalink, '%category%') !== false ) {
  135.  
  136.             $cats = get_the_category($post->ID);
  137.  
  138.             if ( $cats ) {
  139.  
  140.                 usort($cats, '_usort_terms_by_ID'); // order by ID
  141.  
  142.                 $category = $cats[0]->slug;
  143.                 if ( $parent = $cats[0]->parent )
  144.                     $category = get_category_parents($parent, false, '/', true) . $category;
  145.             }
  146.  
  147.             // show default category in permalinks, without
  148.             // having to assign it explicitly
  149.  
  150.             if ( empty($category) ) {
  151.                 $default_category = get_category( get_option( 'default_category' ) );
  152.                 $category = is_wp_error( $default_category ) ? '' : $default_category->slug;
  153.             }
  154.         }
  155.      
  156.         $author = '';
  157.         if ( strpos($permalink, '%author%') !== false ) {
  158.  
  159.             $authordata = get_userdata($post->post_author);
  160.             $author = $authordata->user_nicename;
  161.  
  162.         }
  163.      
  164.         $date = explode(" ",date('Y m d H i s', $unixtime));
  165.         $rewritereplace =
  166.         array(
  167.             $date[0],
  168.             $date[1],
  169.             $date[2],
  170.             $date[3],
  171.             $date[4],
  172.             $date[5],
  173.             $post->post_name,
  174.             $post->ID,
  175.             $category,
  176.             $author,
  177.             $post->post_name,
  178.         );
  179.         $permalink = str_replace($rewritecode, $rewritereplace, $permalink);
  180.     } else { // if they're not using the fancy permalink option
  181.     }
  182.     return $permalink;
  183. }
  184. ?>
  185.  
  186. <?php
  187. //-------------------------------------------------------------------
  188. //          create and manage custom post type
  189. //-------------------------------------------------------------------
  190.  
  191. $labels = array(
  192.     'name' => _x('Galleries', 'post type general name'),
  193.     'singular_name' => _x('Gallery', 'post type singular name'),
  194.     'add_new' => _x('Add New', 'gallery'),
  195.     'add_new_item' => __("Add New Gallery"),
  196.     'edit_item' => __("Edit Gallery"),
  197.     'new_item' => __("New Gallery"),
  198.     'view_item' => __("View Gallery"),
  199.     'search_items' => __("Search Gallery"),
  200.     'not_found' =>  __('No galleries found'),
  201.     'not_found_in_trash' => __('No galleries found in Trash'),
  202.     'parent_item_colon' => ''
  203.   );
  204.   $args = array(
  205.     'labels' => $labels,
  206.     'public' => true,
  207.     'publicly_queryable' => true,
  208.     'show_ui' => true,
  209.     'query_var' => true,
  210.     'rewrite' => true,
  211.     'capability_type' => 'post',
  212.     'hierarchical' => false,
  213.     'menu_position' => null,
  214.     'supports' => array('title','thumbnail','excerpt')
  215.   );
  216.   register_post_type('gallery',$args);
  217.  
  218.  
  219. //----------------------------------------------------
  220. add_filter('manage_edit-gallery_columns',
  221.  
  222.                        'add_new_gallery_columns');
  223.  
  224. //----------------------------------------------------
  225. function add_new_gallery_columns($gallery_columns) {
  226. //----------------------------------------------------
  227.     $new_columns['cb'] = '<input type="checkbox" />';
  228.      
  229.     $new_columns['id'] = __('ID');
  230.     $new_columns['title'] = _x('Gallery Name', 'column name');
  231.     $new_columns['images'] = __('Images');
  232.     $new_columns['author'] = __('Author');
  233.      
  234.     $new_columns['categories'] = __('Categories');
  235.     $new_columns['tags'] = __('Tags');
  236.  
  237.     $new_columns['date'] = _x('Date', 'column name');
  238.  
  239.     return $new_columns;
  240. }
  241.  
  242. //----------------------------------------------------
  243. // Add to admin_init function
  244.  
  245. add_action('manage_gallery_posts_custom_column',
  246.  
  247.                        'manage_gallery_columns', 10, 2);
  248.  
  249. //----------------------------------------------------
  250. function manage_gallery_columns(
  251.                               $column_name, $id) {
  252. //----------------------------------------------------
  253.     global $wpdb;
  254.     switch ($column_name) {
  255.     case 'id':
  256.         echo $id;
  257.             break;
  258.  
  259.     case 'images':
  260.         // Get number of images in gallery
  261.  
  262.         $num_images = $wpdb->get_var($wpdb->prepare(
  263.                "SELECT COUNT(*) FROM $wpdb->posts WHERE post_parent = {$id};"));
  264.  
  265.         echo $num_images;
  266.         break;
  267.     default:
  268.         break;
  269.     }
  270. }  
  271. ?>
  272. <?php
  273. //-------------------------------------------------------------------
  274. //                add categories and tags to pages
  275. //-------------------------------------------------------------------
  276.  
  277. //------------------------------------
  278. function add_page_cats_and_tags() {
  279. //------------------------------------
  280.     register_taxonomy_for_object_type('post_tag', 'page');
  281.     register_taxonomy_for_object_type('category', 'page');
  282. }
  283.  
  284. //------------------------------------
  285. add_action( 'admin_init', 'add_page_cats_and_tags' );
  286.  
  287. //------------------------------------
  288. function my_expanded_request($q) {
  289. //------------------------------------
  290.     if (isset($q['tag']) || isset($q['category_name'])) {
  291.         $q['post_type'] = array('post', 'page');
  292.     }
  293.     return $q;
  294. }
  295.  
  296. //------------------------------------
  297. add_filter('request', 'my_expanded_request');
  298. ?>
  299.  
  300.  
  301. <?php
  302. //-------------------------------------------------------------------
  303. //                     pagination
  304. //-------------------------------------------------------------------
  305.  
  306.     if (isset($_GET['paged']))
  307.         $cur_page = absint($_GET['paged']);
  308.     else
  309.         $cur_page = 1;
  310.      
  311.     //--------------------------------
  312.     $args = array(
  313.         'post_type' => 'attachment',
  314.         'posts_per_page' => 5,
  315.         'paged'=> $cur_page
  316.     );  
  317.     //--------------------------------
  318.     query_posts($args);
  319.     //--------------------------------
  320.         global $wp_query;
  321.         $page_links_total =  $wp_query->max_num_pages;
  322.  
  323.         $page_links = paginate_links(
  324.  
  325.           array(
  326.             'base' => add_query_arg( 'paged', '%#%' ),
  327.             'format' => '',
  328.             'prev_text' => __('&laquo;'),
  329.             'next_text' => __('&raquo;'),
  330.             'total' => $page_links_total,
  331.             'current' => $cur_page
  332.         ));
  333.  
  334.     //--------------------------------
  335.     if ( $page_links ) :
  336.     ?>
  337.        <div class="tablenav-pages">
  338.          <?php
  339.        //--------------------------
  340.        $page_links_text =
  341.        //--------------------------
  342.          sprintf( '<span class="displaying-num">' .
  343.                   __( 'Displaying %s&#8211;%s of %s' ) .
  344.                  '</span>%s',
  345.         //--------------------------
  346.         number_format_i18n( ( $cur_page - 1 ) * $wp_query->query_vars['posts_per_page'] + 1 ),
  347.         //--------------------------
  348.         number_format_i18n(
  349.              min(
  350.                 $cur_page * $wp_query->query_vars['posts_per_page'],
  351.                 $wp_query->found_posts )
  352.              ),
  353.         //--------------------------
  354.         number_format_i18n( $wp_query->found_posts ),
  355.         //--------------------------
  356.         $page_links);
  357.         echo $page_links_text;?>
  358.        
  359.     </div>
  360.  
  361.     <?php
  362.     endif;
  363. ?>
  364.  
  365. <?php
  366. //-------------------------------------------------------------------
  367. //         add the admin options page  (settings API)
  368. //-------------------------------------------------------------------
  369.  
  370. add_action('admin_menu',
  371.  
  372.                     'plugin_admin_add_page');
  373.  
  374. //------------------------------------
  375. function plugin_admin_add_page() {
  376. //------------------------------------
  377. add_options_page( 'Custom Plugin Page',
  378.                   'Custom Plugin Menu',
  379.  
  380.                   'manage_options', /*  capabilities   */
  381.                   'plugin'          /*  page-id        */,
  382.  
  383.                   //---------------------------
  384.                   'plugin_options_page'
  385.                   //---------------------------
  386.      );
  387. }
  388.  
  389. //------------------------------------
  390. function plugin_options_page() {
  391. //------------------------------------
  392. ?>
  393. <div>
  394. <h2>My custom plugin</h2>
  395. Options relating to the Custom Plugin.
  396.  
  397. <form action="options.php" method="post">
  398.    <?php
  399.           settings_fields('plugin_options');
  400.    ?>
  401.     <?php
  402.           do_settings_sections('plugin');
  403.     ?>
  404.  
  405. <input name="Submit" type="submit" value="<?php
  406.          esc_attr_e('Save Changes');
  407.         ?>" />
  408. </form>
  409. </div>
  410.  
  411. <?php
  412. }
  413.  
  414. // add the admin settings and such
  415. //-----------------------------------------
  416. add_action('admin_init',
  417.                     'plugin_admin_init');
  418.  
  419. //-----------------------------------------
  420. function plugin_admin_init(){
  421. //-----------------------------------------
  422.  
  423.    register_setting( 'plugin_options' /* options group */,
  424.                     'plugin_options'  /* option name */,
  425.  
  426.                          //--------------------------
  427.                          'plugin_options_validate'
  428.                          //--------------------------
  429.                       );
  430.  
  431.    //----------------------------------
  432.    add_settings_section('plugin_main' /* section-id */,
  433.                         'Main Settings',
  434.  
  435.                          //-----------------------
  436.                          'plugin_section_text',
  437.                          //-----------------------
  438.                     'plugin'  /* page-id */);
  439.  
  440.    //----------------------------------
  441.    add_settings_field('ztext_string'  /* option-id */,
  442.                       'Plugin Text Input',
  443.  
  444.                            //-------------------------
  445.                            'plugin_setting_string',
  446.                            //-------------------------
  447.                  'plugin'      /*   page-id    */,
  448.                  'plugin_main' /*   section-id */);
  449. }
  450.  
  451. //---------------------------------------
  452. function plugin_section_text() {
  453. //---------------------------------------
  454. echo '<p>Main description of this section here.</p>';
  455. }
  456. //---------------------------------------
  457. function plugin_setting_string() {
  458. //---------------------------------------
  459.  
  460. $options = get_option('plugin_options');
  461.  
  462. echo "<input id='plugin_text_string'
  463.           name='plugin_options[ztext_string]'
  464.           size='40' type='text'
  465.             value='{$options['ztext_string']}' />";
  466. }
  467. //---------------------------------------
  468. function plugin_options_validate($input) {
  469. //---------------------------------------
  470. $newinput['ztext_string'] = trim($input['ztext_string']);
  471.  
  472. if(!preg_match('/^[a-z0-9]{32}$/i', $newinput['ztext_string'])) {
  473.      $newinput['ztext_string'] = '';
  474. }
  475. return $newinput;
  476. }
  477. ?>
  478.  
  479. <?php
  480. //-------------------------------------------------------------------
  481. //                       Settings API
  482. //-------------------------------------------------------------------
  483.  
  484.  function eg_settings_api_init() {
  485.    
  486.         //-------------------------------------
  487.     add_settings_section(
  488.         //-------------------------------------
  489.                 'eg_setting_section'  /* section-id */,
  490.  
  491.         'Example settings section in reading',
  492.  
  493.                 //------------------------
  494.         'eg_setting_section_callback_function'
  495.                 //------------------------,
  496.                
  497.         'reading'             /* page-id    */);
  498.  
  499.         //-------------------------------------
  500.     add_settings_field(
  501.         //-------------------------------------
  502.  
  503.                 'eg_setting_name'  /* option-id */,
  504.  
  505.         'Example setting Name',
  506.                  //---------------------------
  507.         'eg_setting_callback_function',
  508.                 //----------------------------
  509.  
  510.         'reading',                /*  page-id     */
  511.         'eg_setting_section'      /*  section-id  */ );
  512.    
  513.         //-------------------------------------
  514.     register_setting(
  515.         //-------------------------------------
  516.                          'reading'           /* settings-group */,
  517.                          'eg_setting_name'   /* option-id    */);
  518.  }
  519.  
  520.  
  521.  add_action('admin_init', 'eg_settings_api_init');
  522.  
  523.  //------------------------------------------
  524.  function eg_setting_section_callback_function() {
  525.     echo '<p>Intro text for our settings section</p>';
  526.  }
  527.  
  528.  //------------------------------------------
  529.  function eg_setting_callback_function() {
  530.     echo '<input name=
  531.                   //-----------------------
  532.                   "eg_setting_name"
  533.                   //-----------------------
  534.            id="gv_thumbnails_insert_into_excerpt" type="checkbox" value="1" class="code" ' .
  535.         checked( 1,
  536.         //---------------------------------
  537.         get_option('eg_setting_name'),
  538.         //---------------------------------
  539.         false ) . ' /> Explanation text';
  540.  }
  541.  
  542. ?>
  543.  
  544. <?php
  545. //-------------------------------------------------------------------
  546. //             custom settings output handlers
  547. //-------------------------------------------------------------------
  548.  
  549. //-----------------------------------------------
  550. function custom_do_settings_sections($page) {
  551. //-----------------------------------------------
  552.     global $wp_settings_sections, $wp_settings_fields;
  553.  
  554.     if ( !isset($wp_settings_sections) || !isset($wp_settings_sections[$page]) )
  555.         return;
  556.  
  557.     //----------------------------------
  558.     foreach( (array) $wp_settings_sections[$page] as $section ) {
  559.  
  560.         echo "<h3>{$section['title']}</h3>\n";
  561.  
  562.         call_user_func($section['callback'], $section);
  563.  
  564.         if ( !isset($wp_settings_fields) ||
  565.              !isset($wp_settings_fields[$page]) ||
  566.              !isset($wp_settings_fields[$page][$section['id']]) )
  567.                 continue;
  568.  
  569.         echo '<div class="settings-form-wrapper">';
  570.         //-----------------------------
  571.         custom_do_settings_fields($page, $section['id']);
  572.         //-----------------------------
  573.         echo '</div>';
  574.     }
  575. }
  576.  
  577. //-----------------------------------------------
  578. function custom_do_settings_fields(
  579.                        $page, $section) {
  580. //-----------------------------------------------
  581.     global $wp_settings_fields;
  582.  
  583.     if ( !isset($wp_settings_fields) ||
  584.          !isset($wp_settings_fields[$page]) ||
  585.          !isset($wp_settings_fields[$page][$section]) )
  586.         return;
  587.  
  588.     //--------------------------------
  589.     foreach ( (array) $wp_settings_fields[$page][$section] as $field ) {
  590.  
  591.         echo '<div class="settings-form-row">';
  592.         if ( !empty($field['args']['label_for']) )
  593.             echo '<p><label for="' . $field['args']['label_for'] . '">' .
  594.                 $field['title'] . '</label><br />';
  595.         else
  596.             echo '<p>' . $field['title'] . '<br />';
  597.  
  598.         //---------------------------------
  599.         call_user_func($field['callback'], $field['args']);
  600.         echo '</p></div>';
  601.     }
  602. }
  603. ?>
  604.  
  605. <?php
  606. //-------------------------------------------------------------------
  607. //                    custom post types
  608. //-------------------------------------------------------------------
  609.  
  610. $labels = array(
  611.     'name' => _x('Galleries', 'post type general name'),
  612.     'singular_name' => _x('Gallery', 'post type singular name'),
  613.     'add_new' => _x('Add New', 'gallery'),
  614.     'add_new_item' => __("Add New Gallery"),
  615.     'edit_item' => __("Edit Gallery"),
  616.     'new_item' => __("New Gallery"),
  617.     'view_item' => __("View Gallery"),
  618.     'search_items' => __("Search Gallery"),
  619.     'not_found' =>  __('No galleries found'),
  620.     'not_found_in_trash' => __('No galleries found in Trash'),
  621.     'parent_item_colon' => ''
  622.   );
  623.  
  624.   //---------------------------
  625.   $args = array(
  626.     'labels' => $labels,
  627.     'public' => true,
  628.     'publicly_queryable' => true,
  629.     'show_ui' => true,
  630.     'query_var' => true,
  631.     'rewrite' => true,
  632.     'capability_type' => 'post',
  633.     'hierarchical' => false,
  634.     'menu_position' => null,
  635.     'supports' => array('title','thumbnail','excerpt')
  636.   );
  637.  
  638.   //----------------------------
  639.   register_post_type('gallery',$args);
  640.  
  641. //------------------------------------------
  642. //   add post type supports after creation
  643. //------------------------------------------
  644.  
  645. add_post_type_support('gallery', 'title');
  646. add_post_type_support('gallery', array('title', 'thumbnail', 'excerpt') );
  647.  
  648.  
  649. //------------------------------------------
  650. //     taxonomies linked to post type
  651. //------------------------------------------
  652. $args = array(
  653.   'labels' => $labels,
  654.           ...
  655.           'taxonomies' => array('category', 'post_tag')
  656.           );
  657.  
  658. register_post_type('gallery',$args);
  659.  
  660.  
  661. //--------------------------------------
  662. // Add meta box
  663. //   goes into our admin_init function
  664. //--------------------------------------
  665. add_meta_box(   'gallery-type-div', __('Gallery Type'),
  666.  
  667.                   'gallery_type_metabox',
  668.                   'gallery',
  669.                   'normal', 'low');
  670.  
  671. //-------------------------------------
  672. function gallery_type_metabox($post) {
  673. //-------------------------------------
  674.  
  675.     $gallery_type = get_post_meta($post->ID, '_gallery_type', TRUE);
  676.  
  677.     if (!$gallery_type) $gallery_type = 'attachment';    
  678.     ?>
  679.          <input type="hidden" name="gallery_type_noncename" id="gallery_type_noncename"
  680.  
  681.                value="<?php echo wp_create_nonce( 'gallery_type'.$post->ID );?>" />
  682.  
  683.          <input type="radio" name="gallery_type" value="any"
  684.               <?php if ($gallery_type == 'any') echo "checked=1";?>> Any.<br/>
  685.  
  686.          <input type="radio" name="gallery_type" value="attachment"
  687.               <?php if ($gallery_type == 'attachment') echo "checked=1";?>> Only Attachments.<br/>
  688.  
  689.          <input type="radio" name="gallery_type" value="post"
  690.              <?php if ($gallery_type == 'post') echo "checked=1";?>> Only Posts.<br/>
  691.  
  692.          <input type="radio" name="gallery_type" value="gallery"
  693.              <?php if ($gallery_type == 'gallery') echo "checked=1";?>> Only Galleries.<br/>
  694.     <?php
  695. }
  696.  
  697. //----------------------------
  698. // Add to admin_init function
  699. //----------------------------
  700. add_action('save_post',
  701.                         array(&$this,'save_gallery_data') );
  702.  
  703. //------------------------------------------
  704. function save_gallery_data($post_id) {  
  705. //------------------------------------------
  706.  
  707.     //----------------------------------------
  708.     // verify this came from the our screen
  709.     //        and with proper authorization.
  710.     //----------------------------------------
  711.  
  712.     if ( !wp_verify_nonce( $_POST['gallery_type_noncename'],
  713.  
  714.                            'gallery_type'.$post_id )) {
  715.         return $post_id;
  716.     }
  717.      
  718.     //--------------------------------------------
  719.     // verify if this is an auto save routine.
  720.     //   If it is our form has not been submitted,
  721.     //   so we dont want to do anything
  722.     //--------------------------------------------
  723.  
  724.     if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE )
  725.         return $post_id;
  726.  
  727.     //--------------------------------
  728.     // Check permissions
  729.     //--------------------------------
  730.     if ( !current_user_can( 'edit_post', $post_id ) )
  731.         return $post_id;
  732.          
  733.     //--------------------------------------------
  734.     // OK, we're authenticated:
  735.     //       we need to find and save the data  
  736.     //--------------------------------------------
  737.  
  738.     $post = get_post($post_id);
  739.  
  740.     if ($post->post_type == 'gallery') {
  741.  
  742.         update_post_meta($post_id, '_gallery_type',
  743.  
  744.                         esc_attr($_POST['gallery_type']) );
  745.                         return(
  746.                                 esc_attr($_POST['gallery_type']));
  747.     }
  748.     return $post_id;
  749. }
  750. ?>
  751.  
  752. <?php
  753. //-------------------------------------------------------------------
  754. //    link categories and tags to pages
  755. //    and output pages on category and tag archive pages
  756. //-------------------------------------------------------------------
  757.  
  758.  
  759. add_meta_box(   'tagsdiv-post_tag', __('Page Tags'),
  760.                    'post_tags_meta_box',
  761.                         'page', 'side', 'low');
  762.  
  763. add_meta_box(   'categorydiv', __('Categories'),
  764.                     'post_categories_meta_box',
  765.                         'page', 'side', 'core');
  766.  
  767.  
  768. register_taxonomy_for_object_type('post_tag', 'page');
  769. register_taxonomy_for_object_type('category', 'page');                
  770.  
  771. //----------------------------------
  772.  
  773. add_filter('request', 'my_expanded_request');  
  774. //----------------------------------
  775. function my_expanded_request($q) {
  776. //----------------------------------
  777.     if (isset($q['tag']) || isset($q['category_name']))
  778.                 $q['post_type'] = array('post', 'page');
  779.     return $q;
  780. }
  781.  
  782.  
  783. //-------------------------------------------------------------------
  784. //          registering and using dynamic sidebars
  785. //-------------------------------------------------------------------
  786. ?>
  787.  
  788. <ul id="sidebar">
  789. <?php if ( ! dynamic_sidebar() ) : ?>
  790.     <li>{static sidebar item 1}</li>
  791.     <li>{static sidebar item 2}</li>
  792. <?php endif; ?>
  793. </ul>
  794.  
  795. <ul id="sidebar">
  796.     <?php dynamic_sidebar( 'right-sidebar' ); ?>
  797. </ul>
  798.  
  799. <?php if ( is_active_sidebar( 'left-sidebar' ) ) : ?>
  800.     <ul id="sidebar">
  801.         <?php dynamic_sidebar( 'left-sidebar' ); ?>
  802.     </ul>
  803. <?php endif; ?>
  804.  
  805. <?php
  806.  
  807. register_sidebar( array(
  808.     'id'          => 'top-menu',
  809.     'name'        => __( 'Top Menu', $text_domain ),
  810.     'description' => __( 'This sidebar is located above the age logo.', $text_domain ),
  811. ) );
  812.  
  813. //-------------------------------------------------------------------
  814. //                 output standart widgets
  815. //-------------------------------------------------------------------
  816. ?>
  817.  
  818. <?php the_widget( 'WP_Widget_Archives' ); ?>
  819. <?php the_widget( 'WP_Widget_Archives', 'dropdown=1' ); ?>
  820. <?php the_widget( 'WP_Widget_Calendar', $instance, $args ); ?>
  821. <?php the_widget( 'WP_Widget_Calendar' ); ?>
  822. <?php the_widget( 'WP_Widget_Categories', $instance, $args ); ?>
  823. <?php the_widget( 'WP_Widget_Categories'); ?>
  824. <?php the_widget( 'WP_Widget_Categories', 'dropdown=1&count=1' ); ?>
  825. <?php the_widget( 'WP_Widget_Links', $instance, $args ); ?>
  826. <?php the_widget( 'WP_Widget_Links' ); ?>
  827. <?php the_widget( 'WP_Widget_Links', 'category=2,3' ); ?>
  828. <?php the_widget( 'WP_Widget_Meta', $instance, $args ); ?>
  829.  
  830. <?php the_widget('WP_Widget_Pages', 'title=Contents&sortby=post_modified',
  831.                                     'before_title=<h3>&after_title=</h3>'); ?>
  832.  
  833. <?php the_widget( 'WP_Widget_Recent_Posts', $instance, $args ); ?>
  834. <?php the_widget( 'WP_Widget_RSS', $instance, $args ); ?>
  835. <?php the_widget( 'WP_Widget_Search', $instance, $args ); ?>
  836. <?php the_widget( 'WP_Widget_Tag_Cloud', $instance, $args ); ?>
  837. <?php the_widget( 'WP_Widget_Text', $instance, $args ); ?>
  838.  
  839.  
  840.  
  841.  
  842. <?php
  843. //-------------------------------------------------------------------
  844. //               find out the name of current filter
  845. //-------------------------------------------------------------------
  846.  
  847. add_filter("_my_filter","common_function");
  848. add_filter("_another_filter","common_function");
  849.  
  850. //----------------------------------------
  851. function common_function(){
  852. //----------------------------------------
  853.   $currentFilter = current_filter();
  854.   switch ($currentFilter) {
  855.     case '_my_filter':
  856.       echo "Called by My Filter";
  857.       break;
  858.     case '_another_filter':
  859.       echo "Called by another filter";
  860.       break;
  861.   }
  862. }
  863.  
  864. //------------------------------------------
  865. //   system processing of hooked filters
  866. //------------------------------------------
  867. function <process_hook>($hook, $value) {
  868. //------------------------------------------
  869.   global $wp_filter, $wp_current_filter;
  870.  
  871.   $wp_current_filter[] = $hook;  // "Push" the hook onto the stack.
  872.  
  873.   $value = call_user_func($wp_filter[$hook]['function'],$value);
  874.  
  875.   array_pop($wp_current_filter);
  876.   return $value;
  877. }
  878.  
  879. //---------------------------
  880. function current_filter() {
  881. //---------------------------
  882.   global $wp_current_filter;
  883.   return end( $wp_current_filter );
  884. }
  885.  
  886. ?>
  887.  
  888. <?php
  889. //-------------------------------------------------------------------
  890. //      check if requested archive page of certain post type
  891. //-------------------------------------------------------------------
  892. if( is_post_type_archive( array( 'book', 'video' ) ) ) {
  893.     // находимся на странице архива книг или видеозаписей
  894. }
  895.  
  896. ?>
  897.  
  898. <?php
  899. //-------------------------------------------------------------------
  900. //        add tab view with posts of certain category
  901. //-------------------------------------------------------------------
  902. add_action('pre_get_posts', 'query_add_filter' );
  903.  
  904. //-----------------------------------------
  905. function query_add_filter( $wp_query ) {
  906. //-----------------------------------------
  907.     if( is_admin()) {
  908.         add_filter('views_edit-post', 'Add_My_filter');
  909.     }
  910. }
  911.  
  912. //-----------------------------------------
  913. function Add_My_filter($views) {
  914. //-----------------------------------------
  915.     global $wp_query;
  916.     unset($views['mine']);
  917.  
  918.     $my_cat = YOUR-CAT-ID
  919.  
  920.     $query = array(
  921.         'author'      => $current_user->ID,
  922.         'post_type'   => 'post',
  923.         'post_status' => 'publish',
  924.         'cat'         =>  $my_cat
  925.     );
  926.  
  927.     //-----------------------------
  928.     $result = new WP_Query($query);
  929.  
  930.     $class = ($wp_query->query_vars['cat'] == 'featured') ? ' class="current"' : '';
  931.  
  932.     $views['publish_f'] =
  933.          sprintf('<a href="%s"'. $class .'>Publish Featured <span class="count">(%d)</span></a>',
  934.  
  935.          admin_url('edit.php?post_status=publish&post_type=post&cat='.$my_cat),
  936.          $result->found_posts);
  937.  
  938.     return $views;
  939. }
  940. ?>
  941.  
  942. <?php
  943. //-------------------------------------------------------------------
  944. //                altering tabs in users view
  945. //-------------------------------------------------------------------
  946.  
  947. add_filter( 'views_users', 'modify_views_users_so_15295853' );
  948.  
  949. function modify_views_users_so_15295853( $views )
  950. {
  951.     // Manipulate $views
  952.  
  953.     return $views;
  954. }
  955. ?>
  956.  
  957. The content of $views in my system is:
  958.  
  959. Array
  960. (
  961.     [all] => <a href='users.php' class="current">
  962.                          All <span class="count">(7)</span></a>
  963.  
  964.     [administrator] => <a href='users.php?role=administrator'>
  965.                          Administrator <span class="count">(1)</span></a>
  966.  
  967.     [editor] => <a href='users.php?role=editor'>
  968.                            Editor <span class="count">(1)</span></a>
  969.  
  970.     [subscriber] => <a href='users.php?role=subscriber'>
  971.                            Subscriber <span class="count">(5)</span></a>
  972. )
  973.  
  974. <?php
  975. //-------------------------------------------------------------------
  976. //        remove tab from medialibrary window
  977. //-------------------------------------------------------------------
  978.  
  979. add_filter('media_upload_tabs',
  980.                         'remove_media_library_tab');
  981.  
  982. //------------------------------------------
  983. function remove_media_library_tab($tabs) {
  984. //------------------------------------------
  985.     unset($tabs['library']);
  986.     return $tabs;
  987. }
  988.  
  989. //------------------------------------------
  990. function remove_media_library_tab($tabs) {
  991. //------------------------------------------
  992.  
  993.     if (isset($_REQUEST['post_id'])) {
  994.         $post_type = get_post_type($_REQUEST['post_id']);
  995.  
  996.         if ('post' == $post_type)
  997.  
  998.             unset($tabs['library']);
  999.             unset($tabs['gallery']);
  1000.     }
  1001.     return $tabs;
  1002. }
  1003.  
  1004. ?>
  1005.  
  1006. <?php
  1007. //-------------------------------------------------------------------
  1008. //                   register custom sidebars
  1009. //-------------------------------------------------------------------
  1010. function starkers_widgets_init() {
  1011.  
  1012.   // Area 1, located at the top of the sidebar.
  1013.  
  1014.   register_sidebar( array(
  1015.     'name' => __( 'Primary Widget Area', 'starkers' ),
  1016.     'id' => 'primary-widget-area',
  1017.     'description' => __( 'The primary widget area', 'starkers' ),
  1018.     'before_widget' => '<li>',
  1019.     'after_widget' => '</li>',
  1020.     'before_title' => '<h3>',
  1021.     'after_title' => '</h3>',
  1022.   ) );
  1023.  
  1024.   // Area 3, located in the footer. Empty by default.
  1025.   register_sidebar( array(
  1026.     'name' => __( 'First Footer Widget Area', 'starkers' ),
  1027.     'id' => 'first-footer-widget-area',
  1028.     'description' => __( 'The first footer widget area', 'starkers' ),
  1029.     'before_widget' => '<li>',
  1030.     'after_widget' => '</li>',
  1031.     'before_title' => '<h3>',
  1032.     'after_title' => '</h3>',
  1033.   ) );
  1034.  
  1035.   // ... more calls to register_sidebar() ...
  1036. }
  1037.  
  1038. ?>
  1039.  
  1040. <?php
  1041. //-------------------------------------------------------------------
  1042. //      choose custom static sidebar according to conditions
  1043. //-------------------------------------------------------------------
  1044.  
  1045.  
  1046. function mytheme_sidebar($name)
  1047. {
  1048.      $name = apply_filters('mytheme_sidebar', $name);
  1049.      get_sidebar($name);
  1050. }
  1051. //-----------------------------
  1052. add_filter('mytheme_sidebar',
  1053.                          'mytheme_custom_sidebar');
  1054. //-----------------------------
  1055. mytheme_sidebar('left');
  1056.  
  1057. //------------------------------------
  1058. function mytheme_custom_sidebar($name)
  1059. //------------------------------------
  1060. {
  1061.     return is_user_logged_in() ? 'loggedin' : $name;
  1062. }
  1063. ?>
  1064.  
  1065. <?php
  1066. //-------------------------------------------------------------------
  1067. //         Retrieve full list of sidebars and their widgets.
  1068. //-------------------------------------------------------------------
  1069. $sidebars_widgets = wp_get_sidebars_widgets();
  1070.  
  1071. //-----------------------------------------------------
  1072. function wp_get_sidebars_widgets($deprecated = true) {
  1073. //-----------------------------------------------------
  1074.     if ( $deprecated !== true )
  1075.         _deprecated_argument( __FUNCTION__, '2.8.1' );
  1076.  
  1077.     global $wp_registered_widgets, $_wp_sidebars_widgets, $sidebars_widgets;
  1078.  
  1079.     //-------------------------------------------------
  1080.     // If loading from front page, consult $_wp_sidebars_widgets rather than options
  1081.     // to see if wp_convert_widget_settings() has made manipulations in memory.
  1082.     //-------------------------------------------------
  1083.     if ( !is_admin() ) {
  1084.         if ( empty($_wp_sidebars_widgets) )
  1085.  
  1086.             $_wp_sidebars_widgets = get_option('sidebars_widgets', array());
  1087.  
  1088.         $sidebars_widgets = $_wp_sidebars_widgets;
  1089.  
  1090.     } else {
  1091.  
  1092.         $sidebars_widgets = get_option('sidebars_widgets', array());
  1093.     }
  1094.  
  1095.     if ( is_array( $sidebars_widgets ) && isset($sidebars_widgets['array_version']) )
  1096.         unset($sidebars_widgets['array_version']);
  1097.  
  1098.     $sidebars_widgets = apply_filters('sidebars_widgets', $sidebars_widgets);
  1099.     return $sidebars_widgets;
  1100. }
  1101.  
  1102. ?>
  1103.  
  1104. <?php
  1105. //-------------------------------------------------------------------
  1106. //        switch sidebar contents according to conditions
  1107. //-------------------------------------------------------------------
  1108.  
  1109. add_action('widgets_init', 'wpse64492_register');
  1110. //---------------------------------------------------
  1111. function wpse64492_register()
  1112. {
  1113.     register_sidebar(array(
  1114.         'name'  => __('Logged In Sidebar', 'wpse64492'),
  1115.         'id'    => 'logged-in'
  1116.     ));
  1117. }
  1118.  
  1119. //---------------------------------------------------
  1120. add_filter('sidebars_widgets', 'wpse64492_switch');
  1121. //---------------------------------------------------
  1122. //       switch to virtual sidebar 'logged-in'
  1123. //---------------------------------------------------
  1124. function wpse64492_switch($widgets)
  1125. //---------------------------------------------------
  1126. {
  1127.     if(is_admin())
  1128.         return $widgets;
  1129.  
  1130.     $key = 'sidebar-1'; // the sidebar you want to change!
  1131.  
  1132.     if(isset($widgets[$key]) && is_user_logged_in() && isset($widgets['logged-in']))
  1133.         $widgets[$key] = $widgets['logged-in'];
  1134.  
  1135.     return $widgets;
  1136. }
  1137.  
  1138. //-------------------------------------------------------------------
  1139. //        output sidebar if it is not empty
  1140. //-------------------------------------------------------------------
  1141. ?>
  1142.  
  1143.    <?php $sidebars_widgets = wp_get_sidebars_widgets(); ?>
  1144.                
  1145.                 <?php if(!empty($sidebars_widgets['site-footer'])) : ?>
  1146.                         <div id="footer-widgets">
  1147.                                 <div id="footer-widgets-wrapper">
  1148.                                         <?php dynamic_sidebar('Footer') ?>
  1149.                                         <div class="clear"></div>
  1150.                                 </div>
  1151.                         </div>
  1152.                 <?php endif; ?>
  1153.  
  1154. <?php
  1155. //-------------------------------------------------------------------
  1156. //         register and output dynamic sidebar
  1157. //-------------------------------------------------------------------
  1158.  
  1159. if ( function_exists('register_sidebar') )
  1160. register_sidebar(array(
  1161. 'before_widget' => '',
  1162. 'after_widget' => '',
  1163. 'before_title' => '<h4>',
  1164. 'after_title' => '</h4>',
  1165. ));
  1166. ?>
  1167.  
  1168. <?php if ( !function_exists('dynamic_sidebar')
  1169. || !dynamic_sidebar() ) : ?>
  1170. <?php endif; ?>
  1171.  
  1172.  
  1173. //-------------------------------------------------------------------
  1174. //  The $wp_registered_sidebars global variable contains an array
  1175. //-------------------------------------------------------------------
  1176.  
  1177. Array (
  1178.     [sidebar-1] => Array (
  1179.             [name] => Right Sidebar
  1180.             [id] => sidebar-1
  1181.             [description] =>
  1182.             [before_widget] => <li id="%1$s" class="widget %2$s">
  1183.             [after_widget] => </li>
  1184.             [before_title] => <div class="widget-title">
  1185.             [after_title] => </div> )
  1186.  
  1187.     [sidebar-2] => Array (
  1188.             [name] => Extra Sidebar
  1189.             [id] => sidebar-2
  1190.             [description] =>
  1191.             [before_widget] => <li id="%1$s" class="widget %2$s">
  1192.             [after_widget] => </li>
  1193.             [before_title] => <div class="widget-title">
  1194.             [after_title] =></div> )
  1195. ... )
  1196.  
  1197.  
  1198. //-------------------------------------------------------------------
  1199. The $wp_registered_widgets global variable contains an array
  1200. //-------------------------------------------------------------------
  1201. Array (
  1202.     [archives-1] => Array (
  1203.             [name] => Archives
  1204.             [id] => archives-1
  1205.             [callback] => Array (
  1206.                     [0] => WP_Widget_Archives Object (
  1207.                             [id_base] => archives
  1208.                             [name] => Archives
  1209.                             [widget_options] => Array (
  1210.                                     [classname] => widget_archive
  1211.                                     [description] => A monthly archive of your blog&#8217;s posts )
  1212.  
  1213.                             [control_options] => Array (
  1214.                                     [id_base] => archives )
  1215.  
  1216.                             [number] => 1
  1217.                             [id] => archives-1
  1218.                             [updated] =>
  1219.                             [option_name] => widget_archives )
  1220.  
  1221.                     [1] => display_callback )
  1222.  
  1223.             [params] => Array (
  1224.                     [0] => Array (
  1225.                             [number] => -1 )
  1226.                 )
  1227.  
  1228.             [classname] => widget_archive
  1229.             [description] => A monthly archive of your blog&#8217;s posts )
  1230.  
  1231.     [text-8] => Array (
  1232.             [name] => Text
  1233.             [id] => text-8
  1234.             [callback] => Array (
  1235.                     [0] => WP_Widget_Text Object (
  1236.                             [id_base] => text
  1237.                             [name] => Text
  1238.                             [widget_options] => Array (
  1239.                                     [classname] => widget_text
  1240.                                     [description] => Arbitrary text or HTML )
  1241.  
  1242.                             [control_options] => Array (
  1243.                                     [id_base] => text
  1244.                                     [width] => 400
  1245.                                     [height] => 350 )
  1246.  
  1247.                             [number] => 8
  1248.                             [id] => text-8
  1249.                             [updated] =>
  1250.                             [option_name] => widget_text )
  1251.  
  1252.                     [1] => display_callback )
  1253.  
  1254.             [params] => Array (
  1255.                     [0] => Array (
  1256.                             [number] => 8 )
  1257.                  )
  1258.  
  1259.             [classname] => widget_text
  1260.             [description] => Arbitrary text or HTML )
  1261. ... )
  1262.  
  1263. <?php
  1264. //-------------------------------------------------
  1265. $sidebar_widgets = wp_get_sidebars_widgets();
  1266. //-------------------------------------------------
  1267. ?>
  1268.  
  1269. Array (
  1270.     [wp_inactive_widgets] => Array (
  1271.             [0] => pages-4
  1272.             [1] => archives-3
  1273.             [2] => categories-3
  1274.             [3] => text-4
  1275.             [4] => menubar-3
  1276.             [5] => calendar-19
  1277.             [6] => meta-6
  1278.             [7] => meta-7 )
  1279.  
  1280.     [sidebar-1] => Array (
  1281.             [0] => archives-2
  1282.             [1] => search-3
  1283.             [2] => categories-2 )
  1284.  
  1285.     [sidebar-2] => Array ()
  1286.  
  1287.     [sidebar-3] => Array (
  1288.             [0] => menubar-4 )
  1289. ... )
  1290.  
  1291. <?php
  1292.  
  1293. wp_set_sidebars_widgets($sidebar_widgets);
  1294. wp_enqueue_script('admin-widgets');
  1295.  
  1296. add_filter('sidebars_widgets',
  1297.                     array(&$this,'sidebars_widgets'));
  1298.  
  1299. function sidebars_widgets($sidebars_widgets) {
  1300.     ...
  1301.     return $new_widgets;
  1302. }
  1303.  
  1304. ?>
  1305.  
  1306. <?php
  1307. //-------------------------------------------------------------------
  1308. //   Add "first" and "last" CSS classes to dynamic sidebar widgets.
  1309. //   Also adds numeric index class for each widget (widget-1, etc.)
  1310. //-------------------------------------------------------------------
  1311.  
  1312. function widget_first_last_classes($params) {
  1313.  
  1314.     global $my_widget_num; // Global a counter array
  1315.  
  1316.         //-------------------------------------------------------
  1317.         // Get the id for the current sidebar we're processing
  1318.         //-------------------------------------------------------
  1319.     $this_id = $params[0]['id'];
  1320.  
  1321.         //------------------------------------------   
  1322.         // Get an array of ALL registered widgets
  1323.         //------------------------------------------   
  1324.     $arr_registered_widgets = wp_get_sidebars_widgets();
  1325.  
  1326.         //---------------------------------------------------
  1327.         //   If the counter array doesn't exist, create it
  1328.         //---------------------------------------------------
  1329.     if(!$my_widget_num) {
  1330.         $my_widget_num = array();
  1331.     }
  1332.  
  1333.         //------------------------------------------------------
  1334.         //      Check if the current sidebar has no widgets
  1335.         //------------------------------------------------------
  1336.     if(!isset($arr_registered_widgets[$this_id]) || !is_array($arr_registered_widgets[$this_id])) {
  1337.         return $params;
  1338.     }
  1339.  
  1340.         //----------------------------------------------------------
  1341.         //   See if the counter array has an entry for this sidebar
  1342.         //----------------------------------------------------------
  1343.     if(isset($my_widget_num[$this_id])) {
  1344.         $my_widget_num[$this_id] ++;
  1345.     }
  1346.  
  1347.         else {
  1348.                //--------------  If not, create it starting with 1
  1349.  
  1350.         $my_widget_num[$this_id] = 1;
  1351.     }
  1352.  
  1353.         //------------------------------------------------------------
  1354.         //  Add a widget number class for additional styling options
  1355.         //------------------------------------------------------------
  1356.     $class = 'class="widget-' . $my_widget_num[$this_id] . ' ';
  1357.  
  1358.     if($my_widget_num[$this_id] == 1) { // If this is the first widget
  1359.         $class .= 'widget-first ';
  1360.  
  1361.         //---------------------------------------
  1362.         //     If this is the last widget
  1363.         //---------------------------------------
  1364.     } elseif($my_widget_num[$this_id] == count($arr_registered_widgets[$this_id])) {
  1365.         $class .= 'widget-last ';
  1366.     }
  1367.         //-----------------------------------------------
  1368.         // Insert our new classes into "before widget"
  1369.         //-----------------------------------------------
  1370.     $params[0]['before_widget'] = str_replace('class="', $class, $params[0]['before_widget']);
  1371.  
  1372.     return $params;
  1373.  
  1374. }
  1375. //----------------------------------------------------
  1376. add_filter('dynamic_sidebar_params',
  1377.                        'widget_first_last_classes');
  1378. //----------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement