给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自带的某些脚本
- function my_scripts_method() {
- wp_deregister_script( 'jquery' );
- wp_register_script( 'jquery', get_template_directory_uri() . '/js/jquery-new.js');
- wp_enqueue_script( 'jquery' );
- }
- add_action('wp_enqueue_scripts', 'my_scripts_method');
3.指定jpg/jpeg图片的质量
- add_filter( 'jpeg_quality', 'sl_jpeg_quality' );
- function sl_jpeg_quality() {
- return 100;
- }
4.Session设置
- add_action( 'init', 'sl_session_start' );
- function sl_session_start() {
- if ( !session_id() ) {
- session_start();
- }
- }
5.Wordpress原生的页面分页导航
- // Pagination for a WordPress loop
- $list = new WP_Query( $query_args );
- $pagination = array(
- 'base' => str_replace( 99999, '%#%', get_pagenum_link( 99999 ) ),
- 'format' => '?paged=%#%',
- 'current' => max( 1, get_query_var( 'paged' ) ),
- 'total' => $list->max_num_pages,
- 'next_text' => 'next',
- 'prev_text' => 'previous'
- );
- echo '<div class="pagination primary-links">' . paginate_links( $pagination ) . '</div>';
- // Pagination for anything
- $list = range(1, 100);
- $items_per_page = 12;
- $pagination = array(
- 'base' => get_bloginfo( 'url' ) . '/mypage/%_%',
- 'format' => '?paged=%#%',
- 'current' => $_GET['current_page'],
- 'total' => ceil( max($list) / $items_per_page ),
- 'next_text' => 'go forth',
- 'prev_text' => 'go back'
- );
- echo '<div class="pagination primary-links">' . paginate_links( $pagination ) . '</div>';
6.文件上传
- $upload = wp_upload_bits( $_FILES['myfile']['name'], null, file_get_contents( $_FILES['myfile']['tmp_name'] ) );
- echo 'Well uploaded! The path to this file is ' . $upload['file'] . ' and the url to this file is ' . $upload['url'];
7.Wordpress自定义用户资料配置项
- /////Add User Profile item
- add_action( 'show_user_profile', 'sl_profile_fields' );
- add_action( 'edit_user_profile', 'sl_profile_fields' );
- function sl_profile_fields( $user ) {
- ?>
- <h3>社交网络</h3>
- <table class="form-table">
- <tr>
- <th><label for="weibo">SINA-WEIBO</label></th>
- <td>
- <input type="text" name="weibo" id="weibo" value="<?php echo esc_attr( get_the_author_meta( 'weibo', $user->ID ) ); ?>" /><br />
- <span class="description">新浪微博链接</span>
- </td>
- </tr>
- </table>
- <?php
- }
- add_action( 'personal_options_update', 'sl_save_profile_fields' );
- add_action( 'edit_user_profile_update', 'sl_save_profile_fields' );
- function sl_save_profile_fields( $user_id ) {
- if ( !current_user_can( 'edit_user', $user_id ) )
- return false;
- update_user_meta( $user_id, 'weibo', $_POST['weibo'] );
- }
8.Wordpress侧栏文本小工具支持短代码
- add_filter( 'widget_text', 'do_shortcode' );
9.Wordpress自定义回滚版本
- // To remove revisions
- define( 'WP_POST_REVISIONS', FALSE );
- // To limit them
- define( 'WP_POST_REVISIONS', 5 );
10.个性化显示作者的评论
- li.bypostauthor {
- background:#fafafa;
- color:#555;
- }
11.缓存页面内容
- //Storing Your Whole Page In A Variable
- function callback($buffer) {
- return $buffer;
- }
- function buffer_start() {
- ob_start("callback");
- }
- function buffer_end() {
- ob_end_flush();
- }
- add_action('wp_head', 'buffer_start');
- add_action('wp_footer', 'buffer_end');
12.自定义Wordpress短代码
- ///Storing Your Whole Page In A Variable
- function callback($buffer) {
- return $buffer;
- }
- function buffer_start() {
- ob_start("callback");
- }
- function buffer_end() {
- ob_end_flush();
- }
- add_action('wp_head', 'buffer_start');
- add_action('wp_footer', 'buffer_end');
此外:
- remove_filter ('single_post_title', 'wptexturize');
- remove_filter ('bloginfo', 'wptexturize');
- remove_filter ('wp_title', 'wptexturize');
- remove_filter ('category_description', 'wptexturize');
- remove_filter ('list_cats', 'wptexturize');
- remove_filter ('comment_author', 'wptexturize');
- remove_filter ('comment_text', 'wptexturize');
- remove_filter ('the_title', 'wptexturize');
- remove_filter ('the_content', 'wptexturize');
- remove_filter ('the_excerpt', 'wptexturize');