Advertisement
terorama

Wordpress Snippets 4

Oct 13th, 2013
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 54.46 KB | None | 0 0
  1. <?php
  2.  
  3. //-------------------------------------------------------------------
  4.                        remove admin menus
  5. //-------------------------------------------------------------------
  6.  
  7. //-----------------------------
  8. function remove_menus(){  
  9. //-----------------------------
  10.     global $menu;  
  11.     $restricted = array(__('Dashboard'), __('Posts'),
  12.                         __('Media'), __('Links'), __('Pages'),
  13.                         __('Appearance'),
  14.                         __('Tools'), __('Users'),
  15.                         __('Settings'), __('Comments'), __('Plugins'));
  16.  
  17.     end ($menu);  
  18.     while (prev($menu)){  
  19.         $value = explode(' ', $menu[key($menu)][0]);  
  20.         if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}  
  21.     }  
  22. }  
  23. //-----------------------------
  24. add_action('admin_menu', 'remove_menus');  
  25.  
  26. ?>
  27.  
  28.  
  29.  
  30. <?php
  31. //-------------------------------------------------------------------
  32. //              hide admin menu
  33. //-------------------------------------------------------------------
  34.  
  35. add_action('admin_head', 'kill_adminmenu');  
  36. //-----------------------------
  37. function kill_adminmenu(){  
  38. //-----------------------------
  39.     $GLOBALS['menu'] = array();  
  40.  
  41.     echo "<style type='text/css'>#adminmenuwrap,
  42.              #adminmenuback{display:none!important;}
  43.              #wpcontent{margin-left:10px!important;}
  44.         </style>";  
  45. }  
  46. ?>
  47.  
  48. <?php
  49.  
  50. //-------------------------------------------------------------------
  51. //            customize login logo
  52. //-------------------------------------------------------------------
  53.  
  54. //-----------------------------
  55. function my_custom_login_logo(){  
  56. //-----------------------------
  57.  
  58.     echo '<style type="text/css">  
  59.         h1 a { background-image:url('.get_bloginfo('template_directory').
  60.          '/images/custom-login-logo.gif) !important; }  
  61.    </style>';  
  62. }  
  63. //-----------------------------
  64. add_action('login_head', 'my_custom_login_logo');
  65. ?>
  66.  
  67. <?php
  68. //-------------------------------------------------------------------
  69. //                customize admin logo
  70. //-------------------------------------------------------------------
  71.  
  72. //-----------------------------
  73. function my_custom_logo(){  
  74. //-----------------------------
  75.     echo '<style type="text/css">  
  76.    #header-logo { background-image: url('.get_bloginfo('template_directory').
  77.     '/images/custom-logo.gif) !important; }  
  78.    </style>';  
  79. }  
  80. //-----------------------------
  81. add_action('admin_head', 'my_custom_logo');  
  82.  
  83. ?>
  84. <?php
  85.  
  86. //-------------------------------------------------------------------
  87. //                 disable updates
  88. //-------------------------------------------------------------------
  89.  
  90. if( !current_user_can( 'edit_users' ) ){
  91.  
  92.     add_action( 'init', create_function( '$a', "remove_action( 'init', 'wp_version_check' );" ), 2 );  
  93.     add_filter( 'pre_option_update_core', create_function( '$a', "return null;" ) );  
  94.     // для 3.0+  
  95.     add_filter( 'pre_site_transient_update_core', create_function( '$a', "return null;" ) );
  96.  
  97. ?>
  98.  
  99. <?php
  100.  
  101. //-------------------------------------------------------------------
  102. //         Удаление виджетов из Консоли WordPress
  103. //-------------------------------------------------------------------
  104.  
  105. //-----------------------------
  106. function clear_dash(){
  107. //-----------------------------
  108.     $side = &$GLOBALS['wp_meta_boxes']['dashboard']['side']['core'];  
  109.     $normal = &$GLOBALS['wp_meta_boxes']['dashboard']['normal']['core'];  
  110.  
  111.     unset($side['dashboard_quick_press']); //Быстрая публикация  
  112. //  unset($side['dashboard_recent_drafts']); //Полседние черновики  
  113.     unset($side['dashboard_primary']); //Блог WordPress  
  114.     unset($side['dashboard_secondary']); //Другие Нновости WordPress  
  115.  
  116.     unset($normal['dashboard_incoming_links']); //Входящие ссылки  
  117. //  unset($normal['dashboard_right_now']); //Прямо сейчас  
  118.     unset($normal['dashboard_recent_comments']); //Последние комментарии  
  119.     unset($normal['dashboard_plugins']); //Последние Плагины  
  120. }  
  121. add_action('wp_dashboard_setup', 'clear_dash' );  
  122.  
  123. ?>
  124.  
  125. <?php
  126. //-------------------------------------------------------------------
  127. //                add dashboard widget
  128. //-------------------------------------------------------------------
  129.  
  130. //-----------------------------
  131. function example_dashboard_widget_function(){  
  132. //-----------------------------
  133.     // Показать то, что вы хотите показать  
  134.     echo "Привет, мир. Я — великий виджет админки, созданный великими программистами";  
  135. }  
  136. // Создаем функцию, используя хук действия  
  137. //-----------------------------
  138. function example_add_dashboard_widgets() {
  139. //-----------------------------
  140.     wp_add_dashboard_widget('example_dashboard_widget', 'Пример виджета админки',
  141.    'example_dashboard_widget_function');  
  142. }  
  143.  
  144. //-----------------------------
  145. // Хук в 'wp_dashboard_setup', чтобы зарегистрировать нашу функцию среди других  
  146. add_action('wp_dashboard_setup', 'example_add_dashboard_widgets' );
  147.  
  148. ?>
  149.  
  150. <?php
  151. //-------------------------------------------------------------------
  152. //        customize admin header colors
  153. //-------------------------------------------------------------------
  154.  
  155. add_action('admin_head', 'custom_colors');  
  156. //-----------------------------
  157. function custom_colors() {  
  158. //-----------------------------
  159.     echo '<style type="text/css">  
  160.    #wphead{background:#069}  
  161.    </style>';  
  162. }  
  163.  
  164. ?>
  165.  
  166. <?php
  167.  
  168. //-------------------------------------------------------------------
  169. //                      add context help
  170. //-------------------------------------------------------------------
  171.  
  172. //-----------------------------
  173. function my_admin_help($text, $screen) {
  174. //-----------------------------
  175.     // Проверим, только ли для страницы настроек это применимо  
  176.  
  177.     if (strcmp($screen, MY_PAGEHOOK) == 0 ) {  
  178.         $text = 'Вот некоторая полезная информация, которая поможет вам разобраться с плагином...';  
  179.         return $text;  
  180.     }  
  181.     // Пусть по умолчанию штуки с помощью будут и на других страницах панели управления  
  182.     return $text;  
  183. }  
  184. add_action( 'contextual_help', 'my_admin_help' );  
  185. ?>
  186.  
  187. <?php
  188.  
  189. //-------------------------------------------------------------------
  190. //            output error log at dashboard panel
  191. //-------------------------------------------------------------------
  192.  
  193. //-----------------------------
  194. function slt_PHPErrorsWidget() {  
  195. //-----------------------------
  196.     $logfile = '/home/path/logs/php-errors.log'; // Полный пусть до лог файла  
  197.     $displayErrorsLimit = 100; // Максимальное количество ошибок, показываемых в виджете  
  198.     $errorLengthLimit = 300; // Максимальное число символов для описания каждой ошибки  
  199.     $fileCleared = false;  
  200.     $userCanClearLog = current_user_can( 'manage_options' );  
  201.  
  202.     // Очистить файл?  
  203.  
  204.     if( $userCanClearLog && isset( $_GET["slt-php-errors"] ) && $_GET["slt-php-errors"]=="clear" ){  
  205.         $handle = fopen( $logfile, "w" );  
  206.         fclose( $handle );  
  207.         $fileCleared = true;  
  208.     }  
  209.     // Читаем файл  
  210.  
  211.     if( file_exists( $logfile ) ){  
  212.         $errors = file( $logfile );  
  213.         $errors = array_reverse( $errors );  
  214.         if ( $fileCleared ) echo '<p><em>Файл очищен.</em></p>';  
  215.         if ( $errors ) {  
  216.             echo '<p>'.count( $errors ).' ошибка';  
  217.             if ( $errors != 1 ) echo 's';  
  218.             echo '.';  
  219.             if ( $userCanClearLog )
  220.                 echo ' [ <b><a href="'.get_bloginfo("url").'/wp-admin/?slt-php-errors=clear"
  221.                         onclick="return confirm(\'Вы уверенны?\');">ОЧИСТИТЬ ЛОГ ФАЙЛ</a></b> ]';  
  222.             echo '</p>';  
  223.  
  224.             echo '<div id="slt-php-errors"
  225.                   style="height:250px;overflow:scroll;padding:2px;
  226.                   background-color:#faf9f7;border:1px solid #ccc;">';  
  227.             echo '<ol style="padding:0;margin:0;">';  
  228.  
  229.             $i = 0;  
  230.  
  231.             foreach( $errors as $error ){  
  232.                 echo '<li style="padding:2px 4px 6px;border-bottom:1px solid #ececec;">';  
  233.                 $errorOutput = preg_replace( '/\[([^\]]+)\]/', '<b>[$1]</b>', $error, 1 );  
  234.                 if( strlen( $errorOutput ) > $errorLengthLimit ){  
  235.                     echo substr( $errorOutput, 0, $errorLengthLimit ).' [...]';  
  236.                 }  
  237.                 else  
  238.                     echo $errorOutput;  
  239.                 echo '</li>';  
  240.                 $i++;  
  241.                 if( $i > $displayErrorsLimit ){  
  242.                     echo '<li style="padding:2px;border-bottom:2px solid #ccc;">
  243.                    <em>Набралось больше чем '.  $displayErrorsLimit.' ошибок в файле...</em></li>';  
  244.                     break;  
  245.                 }  
  246.             }  
  247.             echo '</ol></div>';  
  248.         }  
  249.         else  
  250.             echo '<p>Ошибок пока нет.</p>';  
  251.     }  
  252.     else  
  253.         echo '<p><em>Произошла ошибка чтения лог файла.</em></p>';  
  254. }  
  255. // Добавляем виджет  
  256. //-----------------------------
  257. function slt_dashboardWidgets(){
  258. //-----------------------------  
  259.     wp_add_dashboard_widget( 'slt-php-errors', 'PHP errors', 'slt_PHPErrorsWidget' );  
  260. }  
  261. //-----------------------------
  262. add_action( 'wp_dashboard_setup', 'slt_dashboardWidgets' );  
  263.  
  264. ?>
  265.  
  266. <?php
  267.  
  268. //-------------------------------------------------------------------
  269. //                    remove meta box
  270. //-------------------------------------------------------------------
  271.  
  272. add_action('admin_init','customize_meta_boxes');  
  273.  
  274. //-----------------------------
  275. function customize_meta_boxes(){  
  276. //-----------------------------
  277.     if( current_user_can('moderate_comments') )  
  278.         return;  
  279.      
  280.     remove_meta_box('postcustom', 'post', 'normal');  
  281. }  
  282.  
  283. ?>
  284.  
  285.  
  286.  
  287. //-------------------------------------------------------------------
  288. //                    rename login page
  289. //-------------------------------------------------------------------
  290.  
  291. RewriteRule ^login$ http://site.ru/wp-login.php [NC,L]  
  292.  
  293. <?php
  294. //-------------------------------------------------------------------
  295. //                rename admin and login pages
  296. //-------------------------------------------------------------------
  297.  
  298. /* редирект с login на /wp-login.php  и с admin на /wp-admin */  
  299.  
  300. add_action('template_redirect', 'kama_login_redirect');  
  301.  
  302. //-----------------------------
  303. function kama_login_redirect(){  
  304. //-----------------------------
  305.     if( strpos($_SERVER['REQUEST_URI'], 'login')!==false )  
  306.         $loc = '/wp-login.php';  
  307.     elseif( strpos($_SERVER['REQUEST_URI'], 'admin')!==false )  
  308.         $loc = '/wp-admin/';  
  309.     if( $loc ){  
  310.         header( 'Location: '.get_option('site_url').$loc, true, 303 );  
  311.         exit;  
  312.     }  
  313. }  
  314.  
  315. ?>
  316.  
  317. <?php
  318.  
  319. //-------------------------------------------------------------------
  320. //             output errors for admins
  321. //-------------------------------------------------------------------
  322.  
  323. add_action('init', 'enable_errors');  
  324.  
  325. //-----------------------------
  326. function enable_errors(){  
  327. //-----------------------------
  328.     if( $GLOBALS['user_level'] < 5 )  
  329.         return;  
  330.     error_reporting(E_ALL ^ E_NOTICE);  
  331.     ini_set("display_errors", 1);  
  332. }  
  333.  
  334. ?>
  335.  
  336. <?php
  337.  
  338. //-------------------------------------------------------------------
  339. //        add new column in list view of custom posts
  340. //-------------------------------------------------------------------
  341.  
  342. // создаем новую колонку  
  343.  
  344. //-----------------------------
  345. add_filter('manage_edit-post_columns',
  346.                          'add_views_column', 4);  
  347.  
  348. //------------------------------------------
  349. function add_views_column( $columns ){  
  350. //------------------------------------------
  351.     $columns['views'] = 'Визиты';  
  352.  
  353.     return $columns;  
  354. }  
  355.  
  356. // заполняем колонку данными  
  357.  
  358. //-----------------------------
  359. add_filter('manage_post_posts_custom_column',
  360.                         'fill_views_column', 5, 2);
  361.  
  362. //---------------------------------------------------
  363. function fill_views_column($column_name, $post_id) {  
  364. //---------------------------------------------------
  365.     if( $column_name != 'views' )  
  366.         return;  
  367.  
  368.     echo get_post_meta($post_id, 'views', 1);  
  369. }  
  370.  
  371. // добавляем возможность сортировать колонку  
  372.  
  373. //-----------------------------
  374. add_filter('manage_edit-post_sortable_columns',
  375.                          'add_views_sortable_column');
  376.  
  377.  
  378. //------------------------------------------------------
  379. function add_views_sortable_column($sortable_columns){  
  380. //------------------------------------------------------
  381.     $sortable_columns['views'] = 'views_views';  
  382.  
  383.     return $sortable_columns;  
  384. }  
  385.  
  386.  
  387. //-----------------------------
  388. add_filter('pre_get_posts',
  389.                      'add_column_views_request');
  390.  
  391.  
  392. //------------------------------------------------
  393. function add_column_views_request( $object ){  
  394. //------------------------------------------------
  395.     if( $object->get('orderby') != 'views_views' )  
  396.         return;  
  397.  
  398.     $object->set('meta_key', 'views');  
  399.     $object->set('orderby', 'meta_value_num');  
  400. }  
  401.  
  402.  
  403.  
  404. //-----------------------------
  405. add_filter('request',
  406.                'add_column_views_request');  
  407.  
  408.  
  409. //----------------------------------------------
  410. function add_column_views_request( $vars ) {
  411. //----------------------------------------------
  412.     if ( 'views_views' == $vars['orderby'] ){  
  413.         $vars['meta_key'] = 'views';  
  414.         $vars['orderby'] = 'meta_value_num';  
  415.     }  
  416.  
  417.     return $vars;  
  418. }  
  419.  
  420. //-----------------------------
  421. add_filter( 'posts_clauses',
  422.                    'add_column_views_request', 10, 2 );  
  423.  
  424. //----------------------------------------------------------
  425. function add_column_views_request( $clauses, $wp_query ){
  426. //----------------------------------------------------------  
  427.     if( 'views_views' != $wp_query->query['orderby'] )  
  428.         return $clauses;  
  429.  
  430.     global $wpdb;  
  431.  
  432.     $clauses['join'] .= " LEFT JOIN {$wpdb->postmeta} ON {$wpdb->posts}.ID={$wpdb->postmeta}.post_id";  
  433.  
  434.     //$clauses['where'] .= " AND {$wpdb->postmeta}.meta_key='views'";  
  435.  
  436.     $clauses['orderby']  = " {$wpdb->postmeta}.meta_value+0 ";  
  437.     $clauses['orderby'] .= ( 'ASC' == strtoupper( $wp_query->get('order') ) ) ? 'ASC' : 'DESC';  
  438.  
  439.     // еще изменяемые элементы  
  440.     //$clauses['groupby']  
  441.     //$clauses['distinct']  
  442.     //$clauses['fields'] // wp_posts.*  
  443.     //$clauses['limits'] // LIMIT 0, 20  
  444.  
  445.     return $clauses;  
  446. }  
  447.  
  448. ?>
  449.  
  450. <?php
  451.  
  452. //-------------------------------------------------------------------
  453. //              подправим ширину колонки через css
  454. //-------------------------------------------------------------------
  455.  
  456. add_action('admin_head', 'add_views_column_css');  
  457.  
  458. //-----------------------------
  459. function add_views_column_css(){  
  460. //-----------------------------
  461.     echo '<style type="text/css">.column-views{width:10%;}</style>';  
  462. }  
  463.  
  464. ?>
  465.  
  466.  
  467. <?php
  468. //-------------------------------------------------------------------
  469. //             sort posts by related taxonomy
  470. //-------------------------------------------------------------------
  471.  
  472. //--------------------------------------------------
  473. function color_orderby( $orderby, $wp_query ) {
  474. //--------------------------------------------------
  475.     global $wpdb;
  476.  
  477.     if ( isset( $wp_query->query['orderby'] ) && 'color' == $wp_query->query['orderby'] ) {
  478.         $orderby = "(
  479.             SELECT GROUP_CONCAT(name ORDER BY name ASC)
  480.             FROM $wpdb->term_relationships
  481.             INNER JOIN $wpdb->term_taxonomy USING (term_taxonomy_id)
  482.             INNER JOIN $wpdb->terms USING (term_id)
  483.             WHERE $wpdb->posts.ID = object_id
  484.             AND taxonomy = 'color'
  485.             GROUP BY object_id
  486.         ) ";
  487.         $orderby .= ( 'ASC' == strtoupper( $wp_query->get('order') ) ) ? 'ASC' : 'DESC';
  488.     }
  489.  
  490.     return $orderby;
  491. }
  492. //--------------------------------------------------
  493.  
  494. add_filter( 'posts_orderby', 'color_orderby', 10, 2 );
  495.  
  496. ?>
  497.  
  498. <?php
  499.  
  500. //-------------------------------------------------------------------
  501. //        user can see only his own posts or media elements
  502. //-------------------------------------------------------------------
  503.  
  504. //---------------------------------------
  505. function ik_eyes_only( $wp_query ) {
  506. //---------------------------------------
  507.  
  508.     //are we looking at the Media Library or the Posts list?
  509.  
  510.     if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false  
  511.     || strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/edit.php' ) !== false ) {
  512.  
  513.         //user level 5 converts to Editor
  514.  
  515.         if ( !current_user_can( 'level_5' ) ) {
  516.  
  517.             //restrict the query to current user
  518.  
  519.             global $current_user;
  520.             $wp_query->set( 'author', $current_user->id );
  521.         }
  522.     }
  523. }
  524.  
  525. //---------------------------------------
  526. add_filter('parse_query', 'ik_eyes_only' );
  527.  
  528. ?>
  529.  
  530. <?php
  531.  
  532. //-------------------------------------------------------------------
  533. //        user can see only his own attachments
  534. //-------------------------------------------------------------------
  535.  
  536. $querystr = " SELECT wposts.post_date,wposts.post_content,wposts.post_title, guid
  537. FROM $wpdb->posts wposts
  538. WHERE wposts.post_author = $author
  539. AND wposts.post_type = 'attachment'
  540. ORDER BY wposts.post_date DESC";
  541.  
  542. $pageposts = $wpdb->get_results($querystr, OBJECT);
  543.  
  544. //-----------------------------------
  545.  
  546. add_action('pre_get_posts','users_own_attachments');
  547.  
  548. //---------------------------------------------------
  549. function users_own_attachments( $wp_query_obj ) {
  550. //---------------------------------------------------
  551.     global $current_user, $pagenow;
  552.  
  553.     if( !is_a( $current_user, 'WP_User') )
  554.         return;
  555.  
  556.     if( 'upload.php' != $pagenow )
  557.         return;
  558.  
  559.     if( !current_user_can('delete_pages') )
  560.  
  561.         $wp_query_obj->set('author', $current_user->id );
  562.  
  563.     return;
  564. }
  565.  
  566. ?>
  567.  
  568. <?php
  569.  
  570. //-------------------------------------------------------------------
  571. // Show only posts and media related to logged in author
  572. //-------------------------------------------------------------------
  573.  
  574. add_action('pre_get_posts', 'query_set_only_author' );
  575.  
  576. //-------------------------------------------------
  577. function query_set_only_author( $wp_query ) {
  578. //-------------------------------------------------
  579.     global $current_user;
  580.     if( is_admin() && !current_user_can('edit_others_posts') ) {
  581.         $wp_query->set( 'author', $current_user->ID );
  582.  
  583.         add_filter('views_edit-post', 'fix_post_counts');
  584.         add_filter('views_upload', 'fix_media_counts');
  585.  
  586.     }
  587. }
  588.  
  589. // Fix post counts
  590.  
  591. //-------------------------------------------------
  592.  
  593. function fix_post_counts($views) {
  594.  
  595. //-------------------------------------------------
  596.  
  597.     global $current_user, $wp_query;
  598.  
  599.     unset($views['mine']);
  600.  
  601.     $types = array(
  602.         array( 'status' =>  NULL ),
  603.         array( 'status' => 'publish' ),
  604.         array( 'status' => 'draft' ),
  605.         array( 'status' => 'pending' ),
  606.         array( 'status' => 'trash' )
  607.     );
  608.  
  609.     foreach( $types as $type ) {
  610.  
  611.         $query = array(
  612.             'author'      => $current_user->ID,
  613.             'post_type'   => 'post',
  614.             'post_status' => $type['status']
  615.         );
  616.  
  617.         $result = new WP_Query($query);
  618.  
  619.         if( $type['status'] == NULL ):
  620.  
  621.             $class = ($wp_query->query_vars['post_status'] == NULL) ? ' class="current"' : '';
  622.  
  623.             $views['all'] =
  624.  
  625.            sprintf(
  626.             '<a href="%1$s"%2$s>%4$s <span class="count">(%3$d)</span></a>',
  627.             admin_url('edit.php?post_type=post'),
  628.             $class,
  629.             $result->found_posts,
  630.             __('All')
  631.         );
  632.  
  633.  
  634.         elseif  ( $type['status'] == 'publish' ):
  635.  
  636.             $class = ($wp_query->query_vars['post_status'] == 'publish') ? ' class="current"' : '';
  637.  
  638.             $views['publish'] =
  639.  
  640.                  sprintf(
  641.                    '<a href="%1$s"%2$s>%4$s <span class="count">(%3$d)</span></a>',
  642.                     admin_url('edit.php?post_type=post'),
  643.                     $class,
  644.                     $result->found_posts,
  645.                      __('Publish')
  646.  
  647.                   );
  648.  
  649.             elseif( $type['status'] == 'draft' ):
  650.  
  651.             $class = ($wp_query->query_vars['post_status'] == 'draft') ? ' class="current"' : '';
  652.  
  653.                 $views['draft'] =
  654.  
  655.                       sprintf(
  656.                         '<a href="%1$s"%2$s>%4$s <span class="count">(%3$d)</span></a>',
  657.                          admin_url('edit.php?post_type=post'),
  658.                          $class,
  659.                         $result->found_posts,
  660.                         __('Draft')
  661.  
  662.                     );
  663.  
  664.         elseif  ( $type['status'] == 'pending' ):
  665.  
  666.                $class = ($wp_query->query_vars['post_status'] == 'pending') ? ' class="current"' : '';
  667.  
  668.                $views['pending'] =
  669.  
  670.                      sprintf(
  671.                           '<a href="%1$s"%2$s>%4$s <span class="count">(%3$d)</span></a>',
  672.                           admin_url('edit.php?post_type=post'),
  673.                          $class,
  674.                         $result->found_posts,
  675.                        __('Pending')
  676.  
  677.                      );
  678.  
  679.         elseif( $type['status'] == 'trash' ):
  680.  
  681.             $class = ($wp_query->query_vars['post_status'] == 'trash') ? ' class="current"' : '';
  682.  
  683.             $views['trash'] =
  684.  
  685.             sprintf(
  686.             '<a href="%1$s"%2$s>%4$s <span class="count">(%3$d)</span></a>',
  687.             admin_url('edit.php?post_type=post'),
  688.             $class,
  689.             $result->found_posts,
  690.             __('Trash')
  691.         );
  692.         endif;
  693.     }
  694.     return $views;
  695. }
  696.  
  697. // Fix media counts
  698. //-------------------------------------------------
  699. function fix_media_counts($views) {
  700. //-------------------------------------------------
  701.     global $wpdb, $current_user, $post_mime_types, $avail_post_mime_types;
  702.     $views = array();
  703.  
  704.     $count = $wpdb->get_results( "
  705.        SELECT post_mime_type, COUNT( * ) AS num_posts
  706.        FROM $wpdb->posts
  707.        WHERE post_type = 'attachment'
  708.        AND post_author = $current_user->ID
  709.        AND post_status != 'trash'
  710.        GROUP BY post_mime_type
  711.    ", ARRAY_A );
  712.  
  713.  
  714.     foreach( $count as $row )
  715.         $_num_posts[$row['post_mime_type']] = $row['num_posts'];
  716.  
  717.     $_total_posts = array_sum($_num_posts);
  718.  
  719.     $detached = isset( $_REQUEST['detached'] ) || isset( $_REQUEST['find_detached'] );
  720.  
  721.     if ( !isset( $total_orphans ) )
  722.         $total_orphans = $wpdb->get_var("
  723.            SELECT COUNT( * )
  724.            FROM $wpdb->posts
  725.            WHERE post_type = 'attachment'
  726.            AND post_author = $current_user->ID
  727.            AND post_status != 'trash'
  728.            AND post_parent < 1
  729.        ");
  730.  
  731.     $matches = wp_match_mime_types(array_keys($post_mime_types), array_keys($_num_posts));
  732.  
  733.     foreach ( $matches as $type => $reals )
  734.         foreach ( $reals as $real )
  735.  
  736.             $num_posts[$type] =
  737.  
  738.               ( isset( $num_posts[$type] ) ) ?
  739.                          $num_posts[$type] + $_num_posts[$real] : $_num_posts[$real];
  740.  
  741.                 $class = ( empty($_GET['post_mime_type']) &&
  742.                          !$detached && !isset($_GET['status']) ) ?
  743.  
  744.                      ' class="current"' : '';
  745.                      $views['all'] =
  746.  
  747.                     "<a href='upload.php'$class>" .
  748.                          sprintf( __('All <span class="count">(%s)</span>', 'uploaded files' ),  
  749.                         number_format_i18n( $_total_posts )) . '</a>';
  750.  
  751.                foreach ( $post_mime_types as $mime_type => $label ) {
  752.  
  753.                   $class = '';
  754.  
  755.                   if ( !wp_match_mime_types($mime_type, $avail_post_mime_types) )
  756.                       continue;
  757.  
  758.                   if ( !empty($_GET['post_mime_type']) &&
  759.                       wp_match_mime_types($mime_type, $_GET['post_mime_type']) )
  760.  
  761.                       $class = ' class="current"';
  762.  
  763.                   if ( !empty( $num_posts[$mime_type] ) )
  764.  
  765.                        $views[$mime_type] = "<a href='upload.php?post_mime_type=$mime_type'$class>" .
  766.                       sprintf( translate_nooped_plural(   $label[2], $num_posts[$mime_type] ),  
  767.                
  768.                         $num_posts[$mime_type] ) . '</a>';
  769.                      }
  770.  
  771.                    $views['detached'] =
  772.                      '<a href="upload.php?detached=1"' . ( $detached ? ' class="current"' : '' ) .
  773.                        '>' .
  774.                        sprintf( __( 'Unattached <span class="count">(%s)</span>', 'detached files' ),
  775.                        $total_orphans ) .
  776.                     '</a>';
  777.  
  778.              return $views;
  779. }
  780.  
  781. ?>
  782.  
  783. <?php
  784. //-------------------------------------------------------------------
  785. //        user can see only his own media-files
  786. //-------------------------------------------------------------------
  787.  
  788. //-------------------------------------
  789. function my_files_only( $wp_query ) {
  790. //-------------------------------------
  791.  
  792. if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false ) {
  793.  
  794.     if ( !current_user_can( 'level_5' ) ) {
  795.         global $current_user;
  796.         $wp_query->set( 'author', $current_user->id );
  797.     }
  798. } else
  799.  
  800.   if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/media-upload.php' ) !== false ) {
  801.  
  802.     if ( !current_user_can( 'level_5' ) ) {
  803.         global $current_user;
  804.         $wp_query->set( 'author', $current_user->id );
  805.     }
  806. }
  807. }
  808.  
  809. //-------------------------------------
  810. add_filter('parse_query', 'my_files_only' );
  811.  
  812.  
  813. ?>
  814.  
  815. <?php
  816.  
  817. //-------------------------------------------------------------------
  818. //              user can see only his own media
  819. //-------------------------------------------------------------------
  820.  
  821. /*
  822. Plugin Name: Manage Your Media Only
  823. Version: 0.1
  824. */
  825.  
  826. //Manage Your Media Only
  827. //---------------------------------------------------
  828. function mymo_parse_query_useronly( $wp_query ) {
  829. //---------------------------------------------------
  830.     if ( strpos( $_SERVER[ 'REQUEST_URI' ], '/wp-admin/upload.php' ) !== false ) {
  831.         if ( !current_user_can( 'level_5' ) ) {
  832.             global $current_user;
  833.             $wp_query->set( 'author', $current_user->id );
  834.         }
  835.     }
  836. }
  837.  
  838. //---------------------------------------------------
  839. add_filter('parse_query', 'mymo_parse_query_useronly' );
  840. ?>
  841.  
  842. <?php
  843. //-------------------------------------------------------------------
  844. //           redirect user to his page
  845. //-------------------------------------------------------------------
  846. global $current_user;
  847.     get_currentuserinfo();
  848.     $userredirect =  get_bloginfo('home') . "/author/" .$current_user->user_nicename;
  849.  
  850. ?>
  851. <?php
  852. //-------------------------------------------------------------------
  853. //                 remove post counts
  854. //-------------------------------------------------------------------
  855.  
  856.     function jquery_remove_counts()
  857. {
  858.     ?>
  859.     <script type="text/javascript">
  860.     jQuery(function(){
  861.         jQuery("ul.subsubsub").find("span.count").remove();
  862.     });
  863.     </script>
  864.  
  865.     <?php
  866. }
  867. add_action('admin_head', 'jquery_remove_counts');
  868. ?>
  869.  
  870. <?php
  871.  
  872. //-------------------------------------------------------------------
  873. //         replace term_id by term slug in query
  874. //-------------------------------------------------------------------
  875.  
  876. add_filter('parse_query',
  877.  
  878.               'convert_large_feature_id_to_taxonomy_term_in_query');
  879.  
  880. //----------------------------------------
  881. function
  882.        convert_large_feature_id_to_taxonomy_term_in_query($query) {
  883. //----------------------------------------
  884.     global $pagenow;
  885.  
  886.     $qv = &$query->query_vars;
  887.     if ($pagenow=='edit.php' &&
  888.             isset($qv['taxonomy']) && $qv['taxonomy']=='large_feature' &&
  889.             isset($qv['term']) && is_numeric($qv['term'])) {
  890.  
  891.         $term = get_term_by('id',$qv['term'],'large_feature');
  892.         $qv['term'] = $term->slug;
  893.     }
  894. }  
  895. ?>
  896.  
  897. <?php
  898.  
  899. //-------------------------------------------------------------------
  900. //     show dropdown box of child terms of current term
  901. //-------------------------------------------------------------------
  902.  
  903.  
  904. //     first get the current term
  905.  
  906. $current_term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
  907.  
  908. //     then set the args for wp_dropdown_categories
  909.  
  910.  $args = array(
  911.     'child_of' => $current_term->term_id,
  912.     'taxonomy' => $current_term->taxonomy,
  913.     'hide_empty' => 0,
  914.     'hierarchical' => true,
  915.     'depth'  => 2,
  916.     'title_li' => '',
  917.         'show_option_all' => All,
  918.         'hide_if_empty' => true
  919.     );
  920.  wp_dropdown_categories( $args );
  921. ?>
  922.  
  923. <?php
  924. //-------------------------------------------------------------------
  925. //          show today's posts in admin post view
  926. //-------------------------------------------------------------------
  927.  
  928. add_filter( 'views_edit-post', 'wpse_add_my_view');
  929.  
  930. //-------------------------------------
  931. function wpse_add_my_view($views){
  932. //-------------------------------------
  933.     global $post_type_object;
  934.     $post_type = $post_type_object->name;
  935.  
  936.     $y =mysql2date('Y', current_time('mysql') );
  937.     $m =mysql2date('m', current_time('mysql') );
  938.     $d =mysql2date('d', current_time('mysql') );
  939.  
  940.     $views['today'] = "<a href='edit.php?year=$y&monthnum=$m&day=$d&post_type=$post_type'>".
  941.                       __('Today','myplugin')."</a>";
  942.  
  943.     return $views;
  944. }
  945. ?>
  946.  
  947. <?php
  948.  
  949. //-------------------------------------------------------------------
  950. //            query posts with specific meta values
  951. //-------------------------------------------------------------------
  952.  
  953. query_posts( array(
  954.   'post_type' => 'product',
  955.   'meta_key' => 'price',
  956.   'meta_value' => 100,
  957.   'meta_compare' => '>'
  958. ) );
  959.  
  960. query_posts( array(
  961.     'post_type' => 'product',
  962.     'meta_query' => array(
  963.         array(
  964.             'key' => 'price',
  965.             'value' => array( 100, 200 ),
  966.             'compare' => 'BETWEEN',
  967.             'type' => 'numeric',
  968.         ),
  969.         array(
  970.             'key' => 'description',
  971.             'value' => 'round',
  972.             'compare' => 'NOT LIKE'
  973.         )
  974.     )
  975. ) );
  976.  
  977. ?>
  978. <?php
  979.  
  980. //-------------------------------------------------------------------
  981. //      check attachment mime type before sending to editor
  982. //-------------------------------------------------------------------
  983.  
  984. function WPSE_78285_Mime($html, $id) {
  985.  
  986.     //fetching attachment by post $id
  987.  
  988.     $attachment = get_post($id);
  989.     $mime_type = $attachment->post_mime_type;
  990.  
  991.     //get an valid array of images types  
  992.  
  993.     $image_exts = array( 'image/jpg', 'image/jpeg', 'image/jpe', 'image/gif', 'image/png' );
  994.  
  995.     //checking the above mime-type
  996.  
  997.     if (in_array($mime_type, $image_exts)) {
  998.  
  999.         // the image link would be great
  1000.  
  1001.         $src = wp_get_attachment_url( $id );
  1002.  
  1003.         // enter you custom output here, you will want to probably change this
  1004.  
  1005.         $html = '<a href="' . $src .  
  1006.                  '" class="your-class" data-src="/img/image1.jpg" rel="your-rel">
  1007.                 <img src="'. $src .'" /></a>';
  1008.  
  1009.         return $html; // return new $html    
  1010.     }
  1011.         return $html;
  1012. }
  1013. //-----------------------------
  1014. add_filter('media_send_to_editor', 'WPSE_78285_Mime', 20, 2);
  1015.  
  1016. ?>
  1017.  
  1018.  
  1019. <?php
  1020. //-------------------------------------------------------------------
  1021. //         add meta field (location) to attachment
  1022. //-------------------------------------------------------------------
  1023.  
  1024. //-------------------------------------------
  1025. function my_add_attachment_location_field
  1026.  
  1027.              ( $form_fields, $post ) {
  1028. //-------------------------------------------
  1029.  
  1030.     $field_value = get_post_meta( $post->ID, 'location', true );
  1031.  
  1032.     $form_fields['location'] = array(
  1033.  
  1034.         'value' => $field_value ? $field_value : '',
  1035.         'label' => __( 'Location' ),
  1036.         'helps' => __( 'Set a location for this attachment' )
  1037.     );
  1038.     return $form_fields;
  1039. }
  1040. //-------------------------------------------
  1041. add_filter( 'attachment_fields_to_edit',
  1042.  
  1043.                    'my_add_attachment_location_field', 10, 2 );
  1044.  
  1045. //-------------------------------------------
  1046. function my_save_attachment_location
  1047.  
  1048.                 ( $attachment_id ) {
  1049. //-------------------------------------------
  1050.     if ( isset( $_REQUEST['attachments'][$attachment_id]['location'] ) ) {
  1051.  
  1052.         $location = $_REQUEST['attachments'][$attachment_id]['location'];
  1053.         update_post_meta( $attachment_id, 'location', $location );
  1054.     }
  1055. }
  1056. //-------------------------------------------
  1057. add_action( 'edit_attachment',
  1058.  
  1059.                     'my_save_attachment_location' );
  1060. ?>
  1061.  
  1062. <?php
  1063.  
  1064. //-------------------------------------------------------------------
  1065. //       exclude certain categories from view on main page
  1066. //-------------------------------------------------------------------
  1067.  
  1068. //-------------------------------------
  1069. function exclude_category( $query ) {
  1070. //-------------------------------------
  1071.     if ( $query->is_home() && $query->is_main_query() ) {
  1072.         $query->set( 'cat', '-1,-1347' );
  1073.     }
  1074. }
  1075.  
  1076. //-------------------------------------
  1077. add_action( 'pre_get_posts',
  1078.                    'exclude_category' );
  1079.  
  1080. ?>
  1081.  
  1082. <?php
  1083.  
  1084. //-------------------------------------------------------------------
  1085. //         show certain types of posts on search page
  1086. //-------------------------------------------------------------------
  1087.  
  1088. //-------------------------------------
  1089. function search_filter($query) {
  1090. //-------------------------------------
  1091.   if ( !is_admin() && $query->is_main_query() ) {
  1092.  
  1093.     if ($query->is_search) {
  1094.  
  1095.       $query->set('post_type', 'post');
  1096.       $query->set('post_type', array( 'post', 'movie' ) );
  1097.     }
  1098.   }
  1099. }
  1100. //-------------------------------------
  1101. add_action('pre_get_posts',
  1102.                        'search_filter');
  1103.  
  1104. ?>
  1105.  
  1106. <?php
  1107. //-------------------------------------------------------------------
  1108. //     show 1 post on home page and 50 on movie archive page    
  1109. //-------------------------------------------------------------------
  1110.  
  1111. //-----------------------------------------
  1112. function hwl_home_pagesize( $query ) {
  1113. //-----------------------------------------
  1114.  
  1115.     if ( is_admin() || ! $query->is_main_query() )
  1116.         return;
  1117.  
  1118.     if ( is_home() ) {
  1119.  
  1120.         // Display only 1 post for the original blog archive
  1121.  
  1122.         $query->set( 'posts_per_page', 1 );
  1123.         return;
  1124.     }
  1125.  
  1126.     if ( is_post_type_archive( 'movie' ) ) {
  1127.  
  1128.         // Display 50 posts for a custom post type called 'movie'
  1129.  
  1130.         $query->set( 'posts_per_page', 50 );
  1131.         return;
  1132.     }
  1133. }
  1134. //--------------------------------
  1135. add_action( 'pre_get_posts',
  1136.                       'hwl_home_pagesize', 1 );
  1137.  
  1138. ?>
  1139.  
  1140. <?php
  1141. //-------------------------------------------------------------------
  1142. //             add categories and tags to pages
  1143. //-------------------------------------------------------------------
  1144.  
  1145. //------------------------------------
  1146. function add_page_cats_and_tags() {
  1147. //------------------------------------
  1148.     register_taxonomy_for_object_type('post_tag', 'page');
  1149.     register_taxonomy_for_object_type('category', 'page');
  1150. }
  1151.  
  1152. //------------------------------------
  1153. add_action( 'admin_init',
  1154.                         'add_page_cats_and_tags' );
  1155.  
  1156. //------------------------------------
  1157. //     show pages as well
  1158. //      on category and tag pages
  1159. //------------------------------------
  1160.  
  1161. function my_expanded_request($q) {
  1162. //------------------------------------
  1163.  
  1164.     if (isset($q['tag']) || isset($q['category_name'])) {
  1165.         $q['post_type'] = array('post', 'page');
  1166.     }
  1167.     return $q;
  1168. }
  1169.  
  1170. //------------------------------------
  1171. add_filter('request',
  1172.                     'my_expanded_request');
  1173.  
  1174.  
  1175. ?>
  1176.  
  1177. <?php
  1178. //-------------------------------------------------------------------
  1179. //            set gallery post type by default
  1180. //-------------------------------------------------------------------
  1181.  
  1182. add_filter('request',
  1183.                     'blog_art_request');  
  1184.  
  1185. //------------------------------------
  1186. function blog_art_request($q) {
  1187. //------------------------------------
  1188.     if (is_admin()) return;
  1189.  
  1190.     if (!empty($q) && !isset($q['p'])
  1191.  
  1192.            && !isset($q['page_id'])
  1193.            && !isset($q['name'])
  1194.            && !isset($q['pagename'])
  1195.            && !isset($q['post_type']))
  1196.  
  1197.            $q['post_type'] = 'gallery';
  1198.  
  1199.     return $q;
  1200. }
  1201.  
  1202. ?>
  1203.  
  1204. <?php
  1205. //-------------------------------------------------------------------
  1206. //         show gallery archive instead of post archive
  1207. //-------------------------------------------------------------------
  1208.  
  1209. add_filter('getarchives_where',
  1210.                               'blog_art_archive', 10, 2);
  1211.  
  1212. //---------------------------------------
  1213. function blog_art_archive($where, $r) {
  1214. //---------------------------------------
  1215.    return str_replace("post_type = 'post'","post_type = 'gallery'",$where);
  1216. }
  1217.  
  1218. //------------------------------------
  1219. add_filter('request',
  1220.                      'my_blog_request');
  1221.  
  1222.  
  1223. //--------------------------------------------------
  1224. //       sort posts by title on admin page
  1225. //--------------------------------------------------
  1226. function my_blog_request($q) {
  1227. //------------------------------------
  1228.     if (!is_admin()) return;
  1229.         if ($q['post_type'] == 'post') {
  1230.         $q['order'] = 'ASC';
  1231.         $q['orderby'] = 'title';
  1232.     }  
  1233.     return $q;
  1234. }
  1235.  
  1236. ?>
  1237.  
  1238. <?php
  1239. //-------------------------------------------------------------------
  1240. //                 show posts with tag = email
  1241. //-------------------------------------------------------------------
  1242.  
  1243.  $querystr = "
  1244.    SELECT $wpdb->posts.*
  1245.    FROM $wpdb->posts, $wpdb->postmeta
  1246.  
  1247.    WHERE $wpdb->posts.ID = $wpdb->postmeta.post_id
  1248.  
  1249.    AND $wpdb->postmeta.meta_key = 'tag'
  1250.    AND $wpdb->postmeta.meta_value = 'email'
  1251.  
  1252.    AND $wpdb->posts.post_status = 'publish'
  1253.    AND $wpdb->posts.post_type = 'post'
  1254.    AND $wpdb->posts.post_date < NOW()
  1255.  
  1256.    ORDER BY $wpdb->posts.post_date DESC
  1257. ";
  1258.  
  1259.  $pageposts = $wpdb->get_results($querystr, OBJECT);
  1260.  
  1261.  //--------------------------------------------
  1262.  ?>
  1263.  
  1264.  <?php if ($pageposts): ?>
  1265.  <?php global $post; ?>
  1266.  <?php foreach ($pageposts as $post): ?>
  1267.  
  1268.  <?php setup_postdata($post); ?>
  1269.  
  1270.  <div class="post" id="post-<?php the_ID(); ?>">
  1271.  
  1272.  <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
  1273.     <?php the_title(); ?></a></h2>
  1274.  
  1275.     <small><?php the_time('F jS, Y') ?> <!-- by <?php the_author() ?> --></small>
  1276.  
  1277.     <div class="entry">
  1278.        <?php the_content('Read the rest of this entry »'); ?>
  1279.     </div>
  1280.  
  1281.     <p class="postmetadata">
  1282.             Posted in <?php the_category(', ')
  1283.             ?> | <?php
  1284.                       edit_post_link('Edit', '', ' | '); ?>  
  1285.  
  1286.      <?php
  1287.             comments_popup_link('No Comments »', '1 Comment »', '% Comments »');
  1288.      ?></p>
  1289.  </div>
  1290.  
  1291.  <?php endforeach; ?>
  1292.  
  1293.  <?php else : ?>
  1294.  
  1295.     <h2 class="center">Not Found</h2>
  1296.     <p class="center">Sorry, but you are looking for something that isn't here.</p>
  1297.     <?php include (TEMPLATEPATH . "/searchform.php"); ?>
  1298.  
  1299.  <?php endif; ?>
  1300.  
  1301. <?php
  1302. //-------------------------------------------------------------------
  1303. //         remove all tabs from posts view
  1304. //-------------------------------------------------------------------
  1305. add_filter('views_edit-post', 'only_show_mine');
  1306.  
  1307. //---------------------------------------
  1308. function only_show_mine( $views ) {
  1309. //---------------------------------------
  1310.     if (!current_user_can('promote_users')) {
  1311.         $views = array(); //remove all options
  1312.         //unset($views['all']);
  1313.         //unset($views['publish']);
  1314.     }
  1315. return $views;
  1316. }
  1317. ?>
  1318.  
  1319. <?php
  1320. //-------------------------------------------------------------------
  1321. //        remove certain tabs from posts and movies view
  1322. //-------------------------------------------------------------------
  1323.  
  1324. function remove__views( $views ) {
  1325.  
  1326.     unset($views['all']);
  1327.     unset($views['publish']);
  1328.     unset($views['trash']);
  1329.  
  1330.     return $views;
  1331. }
  1332. //-------------------------------------------------
  1333. add_action( 'views_edit-post',  'remove_views' );
  1334. add_action( 'views_edit-movie', 'remove_views' );
  1335.  
  1336. // This will remove "all", "publish", and "trash" from both posts and the custom post type "movie."
  1337. // Removing these views from other post types is as simple as adding the following line:
  1338. //-------------------------------------------------
  1339. //   add_ection( 'views_edit-{post-type-slug}', 'remove_views' );
  1340.  
  1341. ?>
  1342.  
  1343. <?php
  1344.  
  1345. //-------------------------------------------------------------------
  1346. //                 tweak the tabs in users view
  1347. //-------------------------------------------------------------------
  1348.  
  1349. add_filter( 'views_users', 'modify_views_users_so_15295853' );
  1350.  
  1351. //---------------------------------------------------
  1352. function modify_views_users_so_15295853( $views )
  1353. //---------------------------------------------------
  1354. {
  1355.     // Manipulate $views
  1356.  
  1357.     return $views;
  1358. }
  1359.  
  1360. ?>
  1361.  
  1362. The content of $views in my system is:
  1363.  
  1364. Array
  1365. (
  1366.     [all] => <a href='users.php' class="current">All <span class="count">(7)</span></a>
  1367.     [administrator] =>
  1368.     <a href='users.php?role=administrator'>Administrator <span class="count">(1)</span></a>
  1369.     [editor] => <a href='users.php?role=editor'>Editor <span class="count">(1)</span></a>
  1370.     [subscriber] => <a href='users.php?role=subscriber'>Subscriber <span class="count">(5)</span></a>
  1371. )
  1372.  
  1373. <?php
  1374.  
  1375. //-------------------------------------------------------------------
  1376. //     select upcoming events in taxonomy=locations
  1377. //-------------------------------------------------------------------
  1378.  
  1379. $locations_with_upcoming_events = $wpdb->get_results("
  1380.        
  1381. SELECT DISTINCT t.*
  1382.        
  1383. FROM wp_terms AS t
  1384.      
  1385. INNER JOIN wp_term_taxonomy AS tt
  1386.    ON t.term_id = tt.term_id
  1387.      
  1388. JOIN wp_term_relationships AS tr
  1389.    ON tr.term_taxonomy_id = tt.term_taxonomy_id
  1390.      
  1391. JOIN wp_posts AS p
  1392.    ON p.ID = tr.object_id
  1393.      
  1394. JOIN wp_postmeta AS pm1
  1395.    ON (p.ID = pm1.post_id AND pm1.meta_key = 'event-date')
  1396.      
  1397. JOIN wp_postmeta AS pm2
  1398.    ON (p.ID = pm2.post_id AND pm2.meta_key = 'members-only')
  1399.      
  1400. WHERE tt.taxonomy IN ('locations')
  1401.      
  1402. AND p.post_type = 'event'
  1403.        
  1404. AND ( pm1.meta_key = 'event-date' AND CAST(pm1.meta_value AS SIGNED) >= '".time()."')
  1405.        
  1406. AND ( pm2.post_id IS NULL )
  1407.    
  1408. ORDER BY t.name ASC;");
  1409.  
  1410. ?>
  1411.  
  1412. <?php
  1413. //-------------------------------------------------------------------
  1414. //         add custom offset for posts pagination
  1415. //-------------------------------------------------------------------
  1416.  
  1417. add_action('pre_get_posts', 'myprefix_query_offset', 1 );
  1418.  
  1419. //--------------------------------------------
  1420. function myprefix_query_offset(&$query) {
  1421. //--------------------------------------------
  1422.  
  1423.     //Before anything else, make sure this is the right query...
  1424.  
  1425.     if ( ! $query->is_posts_page ) {
  1426.         return;
  1427.     }
  1428.  
  1429.     //First, define your desired offset...
  1430.  
  1431.     $offset = 10;
  1432.    
  1433.     //Next, determine how many posts per page you want (we'll use WordPress's settings)
  1434.  
  1435.     $ppp = get_option('posts_per_page');
  1436.  
  1437.     //Next, detect and handle pagination...
  1438.  
  1439.     if ( $query->is_paged ) {
  1440.  
  1441.         //Manually determine page query offset (offset + current page (minus one) x posts per page)
  1442.  
  1443.         $page_offset = $offset + ( ($query->query_vars['paged']-1) * $ppp );
  1444.  
  1445.         //Apply adjust page offset
  1446.  
  1447.         $query->set('offset', $page_offset );
  1448.  
  1449.     }
  1450.     else {
  1451.  
  1452.         //This is the first page. Just use the offset...
  1453.  
  1454.         $query->set('offset',$offset);
  1455.  
  1456.     }
  1457. }
  1458. ?>
  1459.  
  1460. WP_Query Object
  1461. (
  1462.     [query_vars] => Array
  1463.         (
  1464.             [page] =>
  1465.             [pagename] => blog
  1466.             [error] =>
  1467.             [m] => 0
  1468.             [p] => 0
  1469.             [post_parent] =>
  1470.             [post_type] =>
  1471.             [subpost] =>
  1472.             [subpost_id] =>
  1473.             [attachment] =>
  1474.             [attachment_id] => 0
  1475.             [name] =>
  1476.             [static] =>
  1477.             [page_id] => 0
  1478.             [second] =>
  1479.             [minute] =>
  1480.             [hour] =>
  1481.             [day] => 0
  1482.             [monthnum] => 0
  1483.             [year] => 0
  1484.             [w] => 0
  1485.             [category_name] =>
  1486.             [tag] =>
  1487.             [cat] =>
  1488.             [tag_id] =>
  1489.             [author_name] =>
  1490.             [feed] =>
  1491.             [tb] =>
  1492.             [paged] => 0
  1493.             [comments_popup] =>
  1494.             [meta_key] =>
  1495.             [meta_value] =>
  1496.             [preview] =>
  1497.             [s] =>
  1498.             [sentence] =>
  1499.             [fields] =>
  1500.             [category__in] => Array
  1501.                 (
  1502.                 )
  1503.  
  1504.             [category__not_in] => Array
  1505.                 (
  1506.                 )
  1507.  
  1508.             [category__and] => Array
  1509.                 (
  1510.                 )
  1511.  
  1512.             [post__in] => Array
  1513.                 (
  1514.                 )
  1515.  
  1516.             [post__not_in] => Array
  1517.                 (
  1518.                 )
  1519.  
  1520.             [tag__in] => Array
  1521.                 (
  1522.                 )
  1523.  
  1524.             [tag__not_in] => Array
  1525.                 (
  1526.                 )
  1527.  
  1528.             [tag__and] => Array
  1529.                 (
  1530.                 )
  1531.  
  1532.             [tag_slug__in] => Array
  1533.                 (
  1534.                 )
  1535.  
  1536.             [tag_slug__and] => Array
  1537.                 (
  1538.                 )
  1539.  
  1540.         )
  1541.  
  1542.     [tax_query] =>
  1543.     [meta_query] =>
  1544.     [post_count] => 0
  1545.     [current_post] => -1
  1546.     [in_the_loop] =>
  1547.     [comment_count] => 0
  1548.     [current_comment] => -1
  1549.     [found_posts] => 0
  1550.     [max_num_pages] => 0
  1551.     [max_num_comment_pages] => 0
  1552.     [is_single] =>
  1553.     [is_preview] =>
  1554.     [is_page] =>
  1555.     [is_archive] =>
  1556.     [is_date] =>
  1557.     [is_year] =>
  1558.     [is_month] =>
  1559.     [is_day] =>
  1560.     [is_time] =>
  1561.     [is_author] =>
  1562.     [is_category] =>
  1563.     [is_tag] =>
  1564.     [is_tax] =>
  1565.     [is_search] =>
  1566.     [is_feed] =>
  1567.     [is_comment_feed] =>
  1568.     [is_trackback] =>
  1569.     [is_home] => 1
  1570.     [is_404] =>
  1571.     [is_comments_popup] =>
  1572.     [is_paged] =>
  1573.     [is_admin] =>
  1574.     [is_attachment] =>
  1575.     [is_singular] =>
  1576.     [is_robots] =>
  1577.     [is_posts_page] => 1
  1578.     [is_post_type_archive] =>
  1579.     [query_vars_hash] => 41032f87127fba65fb6743b1e97d8662
  1580.     [query_vars_changed] =>
  1581.     [thumbnails_cached] =>
  1582.     [query] => Array
  1583.         (
  1584.             [page] =>
  1585.             [pagename] => blog
  1586.         )
  1587.  
  1588.     [queried_object] => stdClass Object
  1589.         (
  1590.             [ID] => 16
  1591.             [post_author] => 1
  1592.             [post_date] => 2012-01-31 17:23:57
  1593.             [post_date_gmt] => 2012-01-31 17:23:57
  1594.             [post_content] =>
  1595.             [post_title] => Blog
  1596.             [post_excerpt] =>
  1597.             [post_status] => publish
  1598.             [comment_status] => open
  1599.             [ping_status] => open
  1600.             [post_password] =>
  1601.             [post_name] => blog
  1602.             [to_ping] =>
  1603.             [pinged] =>
  1604.             [post_modified] => 2012-01-31 17:23:57
  1605.             [post_modified_gmt] => 2012-01-31 17:23:57
  1606.             [post_content_filtered] =>
  1607.             [post_parent] => 0
  1608.             [guid] =>
  1609.  
  1610.  
  1611. <?php
  1612. //-------------------------------------------------------------------
  1613. //-------------------------------------------------------------------
  1614. add_filter('found_posts', 'myprefix_adjust_offset_pagination', 1, 2 );
  1615. function myprefix_adjust_offset_pagination($found_posts, $query) {
  1616.  
  1617.     //Define our offset again...
  1618.     $offset = 10;
  1619.  
  1620.     //Ensure we're modifying the right query object...
  1621.     if ( $query->is_posts_page ) {
  1622.         //Reduce WordPress's found_posts count by the offset...
  1623.         return $found_posts - $offset;
  1624.     }
  1625. }
  1626.  
  1627. //-------------------------------------------------------------------
  1628. //                    hooks order
  1629. //-------------------------------------------------------------------
  1630. ?>
  1631.  
  1632. Loading my main page
  1633.  
  1634. 16:33:53 - action:muplugins_loaded
  1635.  
  1636. 16:33:53 - action:registered_taxonomy
  1637. 16:33:53 - action:registered_post_type
  1638.  
  1639. 16:33:54 - action:plugins_loaded
  1640. 16:33:54 - action:sanitize_comment_cookies
  1641. 16:33:54 - action:setup_theme
  1642.  
  1643. 16:33:54 - action:load_textdomain
  1644.  
  1645. 16:33:54 - filter:posts_search
  1646. 16:33:54 - action:posts_selection
  1647.  
  1648. 16:33:54 - action:register_sidebar
  1649. 16:33:54 - action:after_setup_theme
  1650. 16:33:54 - action:auth_cookie_malformed
  1651. 16:33:54 - action:auth_cookie_valid
  1652. 16:33:54 - action:set_current_user
  1653. 16:33:54 - action:init
  1654.  
  1655.  
  1656. 16:33:54 - action:widgets_init
  1657. 16:33:54 - action:wp_register_sidebar_widget
  1658. 16:33:54 - action:admin_bar_init
  1659. 16:33:54 - action:add_admin_bar_menus
  1660. 16:33:54 - action:load_textdomain
  1661.  
  1662. 16:33:54 - action:wp_loaded
  1663.  
  1664. 16:33:54 - filter:posts_search
  1665. 16:33:54 - filter:posts_where
  1666. 16:33:54 - filter:posts_join
  1667. 16:33:54 - filter:posts_where_paged
  1668. 16:33:54 - filter:posts_groupby
  1669. 16:33:54 - filter:posts_join_paged
  1670. 16:33:54 - filter:posts_orderby
  1671. 16:33:54 - filter:posts_distinct
  1672. 16:33:54 - filter:post_limits
  1673. 16:33:54 - filter:posts_fields
  1674. 16:33:54 - filter:posts_clauses
  1675. 16:33:54 - action:posts_selection
  1676. 16:33:54 - filter:posts_where_request
  1677. 16:33:54 - filter:posts_groupby_request
  1678. 16:33:54 - filter:posts_join_request
  1679. 16:33:54 - filter:posts_orderby_request
  1680. 16:33:54 - filter:posts_distinct_request
  1681. 16:33:54 - filter:posts_fields_request
  1682. 16:33:54 - filter:post_limits_request
  1683.  
  1684. 16:33:54 - filter:posts_clauses_request
  1685. 16:33:54 - filter:posts_request
  1686. 16:33:54 - filter:posts_results
  1687. 16:33:54 - filter:found_posts_query
  1688.  
  1689. 16:33:54 - filter:found_posts
  1690. 16:33:54 - filter:the_posts
  1691. 16:33:54 - action:template_redirect
  1692.  
  1693. 16:33:54 - action:get_header
  1694. 16:33:54 - action:wp_head
  1695. 16:33:54 - action:wp_enqueue_scripts
  1696. 16:33:54 - action:wp_print_styles
  1697. 16:33:54 - action:wp_print_scripts
  1698.  
  1699. 16:33:54 - action:dynamic_sidebar
  1700. 16:33:54 - filter:posts_search
  1701. 16:33:54 - action:posts_selection
  1702.  
  1703. 16:33:54 - action:begin_fetch_post_thumbnail_html
  1704. 16:33:54 - action:end_fetch_post_thumbnail_html
  1705.  
  1706. 16:33:55 - action:get_sidebar
  1707. 16:33:56 - action:get_footer
  1708. 16:33:56 - action:wp_footer
  1709. 16:33:56 - action:wp_print_footer_scripts
  1710. 16:33:56 - action:wp_before_admin_bar_render
  1711. 16:33:56 - action:wp_after_admin_bar_render
  1712. 16:33:56 - action:shutdown
  1713.  
  1714.  
  1715. Saving a post
  1716.  
  1717. 16:35:57 - action:muplugins_loaded
  1718. 16:35:57 - action:registered_taxonomy
  1719. 16:35:57 - action:registered_post_type
  1720.  
  1721. 16:35:58 - action:plugins_loaded
  1722. 16:35:58 - action:sanitize_comment_cookies
  1723. 16:35:58 - action:setup_theme
  1724. 16:35:58 - action:load_textdomain
  1725.  
  1726. 16:35:58 - filter:posts_search
  1727. 16:35:58 - action:posts_selection
  1728. 16:35:58 - action:register_sidebar
  1729.  
  1730. 16:35:58 - action:after_setup_theme
  1731. 16:35:58 - action:auth_cookie_malformed
  1732. 16:35:58 - action:auth_cookie_valid
  1733. 16:35:58 - action:set_current_user
  1734. 16:35:58 - action:init
  1735. 16:35:58 - action:widgets_init
  1736. 16:35:58 - action:wp_register_sidebar_widget
  1737.  
  1738. 16:35:58 - action:admin_bar_init
  1739. 16:35:58 - action:add_admin_bar_menus
  1740.  
  1741. 16:35:58 - action:wp_loaded
  1742. 16:35:58 - filter:posts_search
  1743. 16:35:58 - filter:posts_where
  1744. 16:35:58 - filter:posts_join
  1745. 16:35:58 - filter:posts_where_paged
  1746. 16:35:58 - filter:posts_groupby
  1747. 16:35:58 - filter:posts_join_paged
  1748. 16:35:58 - filter:posts_orderby
  1749. 16:35:58 - filter:posts_distinct
  1750. 16:35:58 - filter:post_limits
  1751. 16:35:58 - filter:posts_fields
  1752. 16:35:58 - filter:posts_clauses
  1753. 16:35:58 - action:posts_selection
  1754. 16:35:58 - filter:posts_where_request
  1755. 16:35:58 - filter:posts_groupby_request
  1756. 16:35:58 - filter:posts_join_request
  1757. 16:35:58 - filter:posts_orderby_request
  1758. 16:35:58 - filter:posts_distinct_request
  1759. 16:35:58 - filter:posts_fields_request
  1760. 16:35:58 - filter:post_limits_request
  1761. 16:35:58 - filter:posts_clauses_request
  1762. 16:35:58 - filter:posts_request
  1763. 16:35:58 - filter:posts_results
  1764. 16:35:58 - filter:found_posts_query
  1765. 16:35:58 - filter:found_posts
  1766. 16:35:58 - filter:the_posts
  1767. 16:35:58 - action:template_redirect
  1768. 16:35:58 - action:json_api-addlink-add_link
  1769. 16:35:58 - action:clean_object_term_cache
  1770. 16:35:58 - action:clean_post_cache
  1771. 16:35:58 - action:transition_post_status
  1772. 16:35:58 - action:private_to_published
  1773. 16:35:58 - action:new_to_publish
  1774.  
  1775. 16:35:58 - action:publish_videos
  1776.  
  1777. 16:35:58 - action:save_post
  1778. 16:35:58 - action:clean_object_term_cache
  1779. 16:35:58 - action:clean_post_cache
  1780.  
  1781. 16:35:58 - action:wp_insert_post
  1782.  
  1783. 16:35:58 - action:add_post_meta
  1784. 16:35:58 - action:added_post_meta
  1785.  
  1786. 16:35:58 - action:add_term_relationship
  1787. 16:35:58 - action:added_term_relationship
  1788.  
  1789. 16:35:58 - action:edit_term_taxonomy
  1790. 16:35:58 - action:edited_term_taxonomy
  1791. 16:35:58 - action:clean_term_cache
  1792. 16:35:58 - action:set_object_terms
  1793.  
  1794. 16:35:58 - action:create_term
  1795. 16:35:58 - action:create_videostags
  1796.  
  1797. 16:35:58 - action:delete_option
  1798. 16:35:58 - action:delete_option_videostags_children
  1799. 16:35:58 - action:deleted_option
  1800.  
  1801. 16:35:58 - action:add_option
  1802. 16:35:58 - action:add_option_videostags_children
  1803. 16:35:58 - action:added_option
  1804. 16:35:58 - action:clean_term_cache
  1805. 16:35:58 - action:created_term
  1806. 16:35:58 - action:created_videostags
  1807. 16:35:58 - action:add_term_relationship
  1808. 16:35:58 - action:added_term_relationship
  1809. 16:35:58 - action:edit_term_taxonomy
  1810. 16:35:58 - action:edited_term_taxonomy
  1811. 16:35:58 - action:set_object_terms
  1812. 16:35:59 - action:http_api_debug
  1813. 16:35:59 - action:set_object_terms
  1814.  
  1815. 16:35:59 - action:add_post_meta
  1816. 16:35:59 - action:added_post_meta
  1817. 16:35:59 - action:clean_object_term_cache
  1818. 16:35:59 - action:clean_post_cache
  1819.  
  1820. 16:35:59 - action:add_attachment
  1821. 16:35:59 - action:add_post_meta
  1822. 16:35:59 - action:added_post_meta
  1823.  
  1824. 16:35:59 - action:http_api_debug
  1825.  
  1826. 16:35:59 - action:set_object_terms
  1827. 16:35:59 - action:add_post_meta
  1828. 16:35:59 - action:added_post_meta
  1829.  
  1830. 16:35:59 - action:clean_object_term_cache
  1831. 16:35:59 - action:clean_post_cache
  1832.  
  1833. 16:36:00 - action:shutdown
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement