1,添加Google Analytics 统计

<?php
add_action('wp_footer', 'add_googleanalytics');
function add_googleanalytics() { ?>
// 把Google 统计代码复制到这里
<?php } ?>

2,给wordpress 博客添加一个 Favicon 图标。

// add a favicon to your
function blog_favicon() {
echo '<link rel="Shortcut Icon" type="image/x-icon" href="'.get_bloginfo('wpurl').'/favicon.ico" />';
}
add_action('wp_head', 'blog_favicon');

3,移除WordPress版本号。

function wpbeginner_remove_version() {
return '';
}
add_filter('the_generator', 'wpbeginner_remove_version');

4,给WordPress控制面板添加自定义logo

//hook the administrative header output
add_action('admin_head', 'my_custom_logo');
function my_custom_logo() {
echo '
<style type="text/css">
#header-logo { background-image: url('.get_bloginfo('template_directory').'/images/custom-logo.gif) !important; }
</style>
';
}

5,改变WordPress后台控制面板底部信息

function remove_footer_admin () {
echo 'Fueled by <a href="http://www.wordpress.org" target="_blank">WordPress</a> | Designed by <a href="http://www.uzzz.net" target="_blank">Uzzz Productions</a> | WordPress Tutorials: <a href="http://www.wpbeginner.com" target="_blank">WPBeginner</a></p>';
}
add_filter('admin_footer_text', 'remove_footer_admin');

6,自定义WordPress控制面板模块

add_action('wp_dashboard_setup', 'my_custom_dashboard_widgets');
function my_custom_dashboard_widgets() {
global $wp_meta_boxes;

wp_add_dashboard_widget('custom_help_widget', 'Theme Support', 'custom_dashboard_help');
}
function custom_dashboard_help() {
echo '<p>Welcome to Custom Blog Theme! Need help? Contact the developer <a href="mailto:yourusername@gmail.com">here</a>. For WordPress Tutorials visit: <a href="http://www.wpbeginner.com" target="_blank">WPBeginner</a></p>';
}

7,改变默认的 Gravatar 头像

add_filter( 'avatar_defaults', 'newgravatar' );
function newgravatar ($avatar_defaults) {
$myavatar = get_bloginfo('template_directory') . '/images/gravatar.gif';
$avatar_defaults[$myavatar] = "WPBeginner";
return $avatar_defaults;
}

8,让WordPress底部的版权时间显示的更生动

function comicpress_copyright() {
global $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
post_status = 'publish'
");
$output = '';
if($copyright_dates) {
$copyright = "&copy; " . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= '-' . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}
<?php echo comicpress_copyright(); ?>

9,给读者投稿文章添加姓名/来源

add_filter( 'the_author', 'guest_author_name' );
add_filter( 'get_the_author_display_name', 'guest_author_name' );
function guest_author_name( $name ) {
global $post;
$author = get_post_meta( $post->ID, 'guest-author', true );
if ( $author )
$name = $author;
return $name;
}

10,启用文章缩略图功能

add_theme_support( 'post-thumbnails' );
<?php the_post_thumbnail(); ?>

11,自定义WordPress 3.0 版本导航栏

add_theme_support( 'nav-menus' );
<?php wp_nav_menu( array( 'sort_column' => 'menu_order', 'container_class' => 'menu-header' ) ); ?>

12,移除WordPress默认的个人资料选项

add_filter('user_contactmethods','hide_profile_fields',10,1);
function hide_profile_fields( $contactmethods ) {
unset($contactmethods['aim']);
unset($contactmethods['jabber']);
unset($contactmethods['yim']);
return $contactmethods;
}

13,添加作者个人资料选项

function my_new_contactmethods( $contactmethods ) {
// Add Twitter
$contactmethods['twitter'] = 'Twitter';
//add Facebook
$contactmethods['facebook'] = 'Facebook';
return $contactmethods;
}
add_filter('user_contactmethods','my_new_contactmethods',10,1);
add to author.php
<?php echo $curauth->twitter; ?>

14,添加侧边栏小模块

if ( function_exists('register_sidebar') )
register_sidebar(array('name'=>'MiddleSidebar',
'before_widget' => '<li>',
'after_widget' => '</li>',
'before_title' => '<h2>',
'after_title' => '</h3>',
));
register_sidebar(array('name'=>'FooterSidebar',
'before_widget' => '<li>',
'after_widget' => '</li>',
'before_title' => '<h2>',
'after_title' => '</h3>',
));
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('MiddleSidebar') ) : ?>
<!–Default sidebar info goes here–>
<?php endif; ?>

15,优化WordPress 博客的RSS

function wpbeginner_postrss($content) {
if(is_feed()){
$content = 'This post was written by Syed Balkhi '.$content.'Check out WPBeginner';
}
return $content;
}
add_filter('the_excerpt_rss', 'wpbeginner_postrss');
add_filter('the_content', 'wpbeginner_postrss');

16,给RSS添加缩略图

function rss_post_thumbnail($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$content = '<p>' . get_the_post_thumbnail($post->ID) .
'</p>' . get_the_content();
}
return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');

17,开启WordPress评论嵌套功能。

// enable threaded comments
function enable_threaded_comments(){
if (!is_admin()) {
if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
wp_enqueue_script('comment-reply');
}
}
add_action('get_header', 'enable_threaded_comments');

18,移除WordPress登陆面板的错误提示

add_filter('login_errors',create_function('$a', "return null;"));

19,关闭WordPress的搜索功能

function fb_filter_query( $query, $error = true ) {
if ( is_search() ) {
$query->is_search = false;
$query->query_vars[s] = false;
$query->query[s] = false;
// to error
if ( $error == true )
$query->is_404 = true;
}
}
add_action( 'parse_query', 'fb_filter_query' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );

20,启用WordPress简码功能

function showads() {
return '<div id="adsense"><script type="text/javascript"><!–
google_ad_client = "pub-XXXXXXXXXXXXXX";
google_ad_slot = "4668915978";
google_ad_width = 468;
google_ad_height = 60;
//–>
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>';
}
add_shortcode('adsense', 'showads');

21,不通过.htaccess将rss地址唯一化

function custom_feed_link($output, $feed) {
$feed_url = 'http://feeds.feedburner.com/wpbeginner';
$feed_array = array('rss' => $feed_url, 'rss2' => $feed_url, 'atom' => $feed_url, 'rdf' => $feed_url, 'comments_rss2' => '');
$feed_array[$feed] = $feed_url;
$output = $feed_array[$feed];
return $output;
}
function other_feed_links($link) {
$link = 'http://feeds.feedburner.com/wpbeginner';
return $link;
}
//Add our functions to the specific filters
add_filter('feed_link','custom_feed_link', 1, 2);
add_filter('category_feed_link', 'other_feed_links');
add_filter('author_feed_link', 'other_feed_links');
add_filter('tag_feed_link','other_feed_links');
add_filter('search_feed_link','other_feed_links');

22,启用paypal 捐赠简码

function donate_shortcode( $atts ) {
extract(shortcode_atts(array(
'text' => 'Make a donation',
'account' => 'REPLACE ME',
'for' => '',
), $atts));
global $post;
if (!$for) $for = str_replace(" "," ",$post->post_title);
return '<a href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business='.$account.'&item_name=Donation for '.$for.'">'.$text.'</a>';
}
add_shortcode('donate', 'donate_shortcode');

23,设定文章从发布到出现在RSS中的时间长短

function publish_later_on_feed($where) {
global $wpdb;
if ( is_feed() ) {
// timestamp in WP-format
$now = gmdate(‘Y-m-d H:i:s’);
// value for wait; + device
$wait = ‘10′; // integer
// http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_timestampdiff
$device = ‘MINUTE’; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR
// add SQL-sytax to default $where
$where .= ” AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, ‘$now’) > $wait “;
}
return $where;
}
add_filter(‘posts_where’, ‘publish_later_on_feed’);

24,自定义摘要输出时的符号

// custom excerpt ellipses for 2.9
function custom_excerpt_more($more) {
return '…';
}
add_filter('excerpt_more', 'custom_excerpt_more');
/* custom excerpt ellipses for 2.8-
function custom_excerpt_more($excerpt) {
return str_replace('[...]', '…', $excerpt);
}
add_filter('wp_trim_excerpt', 'custom_excerpt_more');
*/

25,自定义摘要输出的文字长度

function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');

26,显示精确评论数

add_filter('get_comments_number', 'comment_count', 0);
function comment_count( $count ) {
if ( ! is_admin() ) {
global $id;
$comments_by_type = &separate_comments(get_comments('status=approve&post_id=' . $id));
return count($comments_by_type['comment']);
} else {
return $count;
}
}

27,取消RSS输出

function fb_disable_feed() {
wp_die( __('No feed available,please visit our <a href="'. get_bloginfo('url') .'">homepage</a>!') );
}
add_action('do_feed', 'fb_disable_feed', 1);
add_action('do_feed_rdf', 'fb_disable_feed', 1);
add_action('do_feed_rss', 'fb_disable_feed', 1);
add_action('do_feed_rss2', 'fb_disable_feed', 1);
add_action('do_feed_atom', 'fb_disable_feed', 1);

28,显示Twitter 的订阅数以及其他资料

function rarst_twitter_user( $username, $field, $display = false ) {
$interval = 3600;
$cache = get_option('rarst_twitter_user');
$url = 'http://api.twitter.com/1/users/show.json?screen_name='.urlencode($username);
if ( false == $cache )
$cache = array();
// if first time request add placeholder and force update
if ( !isset( $cache[$username][$field] ) ) {
$cache[$username][$field] = NULL;
$cache[$username]['lastcheck'] = 0;
}
// if outdated
if( $cache[$username]['lastcheck'] < (time()-$interval) ) {
// holds decoded JSON data in memory
static $memorycache;
if ( isset($memorycache[$username]) ) {
$data = $memorycache[$username];
}
else {
$result = wp_remote_retrieve_body(wp_remote_request($url));
$data = json_decode( $result );
if ( is_object($data) )
$memorycache[$username] = $data;
}
if ( is_object($data) ) {
// update all fields, known to be requested
foreach ($cache[$username] as $key => $value)
if( isset($data->$key) )
$cache[$username][$key] = $data->$key;
$cache[$username]['lastcheck'] = time();
}
else {
$cache[$username]['lastcheck'] = time()+60;
}
update_option( 'rarst_twitter_user', $cache );
}
if ( false != $display )
echo $cache[$username][$field];
return $cache[$username][$field];
}
echo rarst_twitter_user('wpbeginner', 'name').' has '.
rarst_twitter_user('wpbeginner', 'followers_count').' followers after '.
rarst_twitter_user('wpbeginner', 'statuses_count').' updates.';

29,禁用WordPress评论区的HTML

// This will occur when the comment is posted
function plc_comment_post( $incoming_comment ) {
// convert everything in a comment to display literally
$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
// the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
$incoming_comment['comment_content'] = str_replace( "'", '&amp;apos;', $incoming_comment['comment_content'] );
return( $incoming_comment );
}
// This will occur before a comment is displayed
function plc_comment_display( $comment_to_display ) {
// Put the single quotes back in
$comment_to_display = str_replace( '&amp;apos;', "'", $comment_to_display );
return $comment_to_display;

30,防止 WordPress 遭受恶意 URL 请求

/* Plugin Name: Block Bad Queries */
if (strlen($_SERVER['REQUEST_URI']) > 255 ||
strpos($_SERVER['REQUEST_URI'], "eval(") ||
strpos($_SERVER['REQUEST_URI'], "base64")) {
@header("HTTP/1.1 414 Request-URI Too Long");
@header("Status: 414 Request-URI Too Long");
@header("Connection: Close");
@exit;
}

31,在WordPress编辑器中添加内容

add_filter( 'default_content', 'my_editor_content' );
function my_editor_content( $content ) {
$content = "Hello World!";
return $content;
}

32,把 WordPress 自定义日志类型添加到 Feed

add_filter('pre_get_posts', 'add_microblog_2_feed' );
function add_microblog_2_feed( $query ) {
if ( is_feed()){
$query->set('post_type', array( 'post', 'm'));
}
return $query;
}

33,如何获取日志内容的第一段

function get_first_p($post){
//如果是使用 Windows Live Writer 这些工具写日志,可能使用<p>和</p>进行分段
if(preg_match('/<p>(.*)<\/p>/iU',trim(strip_tags($post->post_content,"<p>")),$matches)){
return $matches[1];
} else {
//如果直接在 WordPress 写日志,使用换行符(\n)来分段
$post_content = explode("\n",trim(strip_tags($post->post_content)));
return $post_content ['0'];
}
}

34,自动替换dropbox的外链地址

// 自动替换Dropbox的外链地址
function replace_dropbox($content) {
return preg_replace('/https?:\/\/dl.dropbox.com\/+/i','https://dl-web.dropbox.com/',$content);
}
add_filter('the_content', 'replace_dropbox');
//禁用半角符号自动转换为全角
remove_filter('the_content', 'wptexturize');

35,Wordpress 去掉评论中链接

add_filter('comment_text', 'no_url_text', 10);
function no_url_text($text = ''){
$text = preg_replace("/<a[^>]*href=[^>]*>|<\/[^a]*a[^>]*>/i","",$text);
echo $text;
}

36,防止他人冒充自己

/**
* when comment check the comment_author comment_author_email
* @param unknown_type $comment_author
* @param unknown_type $comment_author_email
* @return unknown_type
*/
function CheckEmailAndName(){
global $wpdb;
$comment_author       = ( isset($_POST['author']) )  ? trim(strip_tags($_POST['author'])) : null;
$comment_author_email = ( isset($_POST['email']) )   ? trim($_POST['email']) : null;
if(!$comment_author || !$comment_author_email){
return;
}
$result_set = $wpdb->get_results("SELECT display_name, user_email FROM $wpdb->users WHERE display_name = '" . $comment_author . "' OR user_email = '" . $comment_author_email . "'");
if ($result_set) {
if ($result_set[0]->display_name == $comment_author){
$errorMessage =  __('警告: 您不能用这个昵称,因为这是我的名字!乖,不要捣乱(*^__^*)……');
}else{
$errorMessage = __('警告: 您不能使用该邮箱地址,因为这是我的邮箱!乖,不要捣乱(*^__^*)……');
}
fail($errorMessage);
}
}
add_action('pre_comment_on_post', 'CheckEmailAndName');

via:http://014.cc/948