给WP开发爱好者的12个提示案例分享

1.js与CSS的引入

wp_enqueue_script()与wp_enqueue_style()

http://codex.wordpress.org/Function_Reference/wp_enqueue_script#Default_scripts_included_with_WordPress

2.不用Wordpress自带的某些脚本

  1. function my_scripts_method() {   
  2.     wp_deregister_script( 'jquery' );   
  3.     wp_register_script( 'jquery', get_template_directory_uri() . '/js/jquery-new.js');   
  4.     wp_enqueue_script( 'jquery' );   
  5. }   
  6. add_action('wp_enqueue_scripts', 'my_scripts_method');  

3.指定jpg/jpeg图片的质量

  1. add_filter( 'jpeg_quality', 'sl_jpeg_quality' );   
  2. function sl_jpeg_quality() {   
  3.     return 100;   
  4. }  

4.Session设置

  1. add_action( 'init', 'sl_session_start' );   
  2. function sl_session_start() {   
  3.     if ( !session_id() ) {   
  4.         session_start();   
  5.     }   
  6. }  

5.Wordpress原生的页面分页导航

  1. // Pagination for a WordPress loop   
  2. $list = new WP_Query( $query_args );   
  3. $pagination = array(   
  4.     'base'       => str_replace( 99999, '%#%', get_pagenum_link( 99999 ) ),   
  5.     'format'     => '?paged=%#%',   
  6.     'current'    => max( 1, get_query_var( 'paged' ) ),   
  7.     'total'      => $list->max_num_pages,   
  8.     'next_text'  => 'next',   
  9.     'prev_text'  => 'previous'   
  10. );   
  11. echo '<div class="pagination primary-links">' . paginate_links( $pagination ) . '</div>';   
  12.   
  13. // Pagination for anything   
  14. $list = range(1, 100);   
  15. $items_per_page = 12;   
  16. $pagination = array(   
  17.     'base'       => get_bloginfo( 'url' ) . '/mypage/%_%',   
  18.     'format'     => '?paged=%#%',   
  19.     'current'    => $_GET['current_page'],   
  20.     'total'      => ceil( max($list) / $items_per_page ),   
  21.     'next_text'  => 'go forth',   
  22.     'prev_text'  => 'go back'   
  23. );   
  24. echo '<div class="pagination primary-links">' . paginate_links( $pagination ) . '</div>';  

6.文件上传

  1. $upload = wp_upload_bits( $_FILES['myfile']['name'], null, file_get_contents$_FILES['myfile']['tmp_name'] ) );   
  2. echo 'Well uploaded! The path to this file is ' . $upload['file'] . ' and the url to this file is ' . $upload['url'];  
 

7.Wordpress自定义用户资料配置项

  1. /////Add User Profile item   
  2. add_action( 'show_user_profile', 'sl_profile_fields' );   
  3. add_action( 'edit_user_profile', 'sl_profile_fields' );   
  4.   
  5. function sl_profile_fields( $user ) {   
  6. ?>   
  7.   
  8.     <h3>社交网络</h3>   
  9.   
  10.     <table class="form-table">   
  11.   
  12.         <tr>   
  13.             <th><label for="weibo">SINA-WEIBO</label></th>   
  14.   
  15.             <td>   
  16.                 <input type="text" name="weibo" id="weibo" value="<?php echo esc_attr( get_the_author_meta( 'weibo', $user->ID ) ); ?>" /><br />   
  17.                 <span class="description">新浪微博链接</span>   
  18.             </td>   
  19.         </tr>   
  20.     </table>   
  21.   
  22. <?php   
  23. }   
  24. add_action( 'personal_options_update', 'sl_save_profile_fields' );   
  25. add_action( 'edit_user_profile_update', 'sl_save_profile_fields' );   
  26.   
  27. function sl_save_profile_fields( $user_id ) {   
  28.     if ( !current_user_can( 'edit_user', $user_id ) )   
  29.         return false;   
  30.     update_user_meta( $user_id, 'weibo', $_POST['weibo'] );   
  31.   
  32. }   

8.Wordpress侧栏文本小工具支持短代码

  1. add_filter( 'widget_text', 'do_shortcode' );  

9.Wordpress自定义回滚版本

  1. // To remove revisions   
  2. define( 'WP_POST_REVISIONS', FALSE );   
  3.   
  4. // To limit them   
  5. define( 'WP_POST_REVISIONS', 5 );  

10.个性化显示作者的评论

  1. li.bypostauthor {   
  2.     background:#fafafa;   
  3.     color:#555;   
  4. }  

11.缓存页面内容

  1. //Storing Your Whole Page In A Variable   
  2. function callback($buffer) {   
  3.     return $buffer;   
  4.     }   
  5. function buffer_start() {   
  6.     ob_start("callback");   
  7.     }   
  8. function buffer_end() {   
  9.     ob_end_flush();   
  10.     }   
  11. add_action('wp_head', 'buffer_start');   
  12. add_action('wp_footer', 'buffer_end');  

12.自定义Wordpress短代码

  1. ///Storing Your Whole Page In A Variable   
  2. function callback($buffer) {   
  3.     return $buffer;   
  4.     }   
  5. function buffer_start() {   
  6.     ob_start("callback");   
  7.     }   
  8. function buffer_end() {   
  9.     ob_end_flush();   
  10.     }   
  11. add_action('wp_head', 'buffer_start');   
  12. add_action('wp_footer', 'buffer_end');  

此外:

  1. remove_filter ('single_post_title', 'wptexturize');   
  2. remove_filter ('bloginfo', 'wptexturize');   
  3. remove_filter ('wp_title', 'wptexturize');     
  4. remove_filter ('category_description', 'wptexturize');   
  5. remove_filter ('list_cats', 'wptexturize');   
  6. remove_filter ('comment_author', 'wptexturize');   
  7. remove_filter ('comment_text', 'wptexturize');   
  8. remove_filter ('the_title', 'wptexturize');   
  9. remove_filter ('the_content', 'wptexturize');   
  10. remove_filter ('the_excerpt', 'wptexturize');  
原文地址:https://www.cnblogs.com/lanxin258/p/3508446.html