WordPress常用代码整理

WordPress is an absolute favorite among developers and bloggers because it is super flexible and customizable. With thousands of useful plugins, you can create an amazing web page with the outstanding user experience. Not to forget that even the novices will be able to find their way around and learn how to use it in no time.

But is there a way to make your blog stand out? These fifteen WordPress hacks for a developer will help you create a functional web page that will look the way you want it to.

//wordpress上传文件重命名
function git_upload_filter($file) {
$time = date("YmdHis");
$file['name'] = $time . "" . mt_rand(1, 100) . "." . pathinfo($file['name'], PATHINFO_EXTENSION);
return $file;
}
add_filter('wp_handle_upload_prefilter', 'git_upload_filter');

//增加链接
add_filter('pre_option_link_manager_enabled','__return_true');

//加速优化
remove_action( 'wp_head', 'wp_generator' );//WordPress版本信息。
remove_action( 'wp_head', 'feed_links_extra', 3 );//去除评论feed
remove_action( 'wp_head', 'feed_links', 2 );//去除文章的feed
remove_action( 'wp_head', 'rsd_link' );//针对Blog的离线编辑器开放接口所使用
remove_action( 'wp_head', 'feed_links_extra', 3 );
remove_action( 'wp_head', 'feed_links', 2 );
remove_action('wp_head','wp_shortlink_wp_head',10,0);
remove_action('template_redirect','wp_shortlink_header',11,0);
remove_action( 'admin_print_scripts', 'print_emoji_detection_script');
remove_action( 'admin_print_styles', 'print_emoji_styles');
remove_action( 'wp_head', 'print_emoji_detection_script', 7);
remove_action( 'wp_print_styles', 'print_emoji_styles');
remove_filter( 'the_content_feed', 'wp_staticize_emoji');
remove_filter( 'comment_text_rss', 'wp_staticize_emoji');
remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email');
/**
* 移除WordPress加载的JS和CSS链接中的版本号
* https://www.wpdaxue.com/remove-js-css-version.html
*/
function wpdaxue_remove_cssjs_ver( $src ) {
if( strpos( $src, 'ver=' ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}
add_filter( 'style_loader_src', 'wpdaxue_remove_cssjs_ver', 999 );
add_filter( 'script_loader_src', 'wpdaxue_remove_cssjs_ver', 999 );
function add_editor_buttons($buttons) {
$buttons[] = 'fontselect';
$buttons[] = 'fontsizeselect';
$buttons[] = 'cleanup';
$buttons[] = 'styleselect';
$buttons[] = 'del';
$buttons[] = 'sub';
$buttons[] = 'sup';
$buttons[] = 'copy';
$buttons[] = 'paste';
$buttons[] = 'cut';
$buttons[] = 'image';
$buttons[] = 'anchor';
$buttons[] = 'backcolor';
$buttons[] = 'wp_page';
$buttons[] = 'charmap';
return $buttons;
}
add_filter("mce_buttons_2", "add_editor_buttons");
// WordPress Emoji Delete
remove_action( 'admin_print_scripts' , 'print_emoji_detection_script');
remove_action( 'admin_print_styles' , 'print_emoji_styles');
remove_action( 'wp_head' , 'print_emoji_detection_script', 7);
remove_action( 'wp_print_styles' , 'print_emoji_styles');
remove_filter( 'the_content_feed' , 'wp_staticize_emoji');
remove_filter( 'comment_text_rss' , 'wp_staticize_emoji');
remove_filter( 'wp_mail' , 'wp_staticize_emoji_for_email');

//移除Google字体
/* 
* delete google fonts
* ====================================================
*/
// Remove Open Sans that WP adds from frontend
if (!function_exists('remove_wp_open_sans')) :
function remove_wp_open_sans() {
wp_deregister_style( 'open-sans' );
wp_register_style( 'open-sans', false );
}
add_action('wp_enqueue_scripts', 'remove_wp_open_sans');

// Uncomment below to remove from admin
// add_action('admin_enqueue_scripts', 'remove_wp_open_sans');
endif;

function remove_open_sans() { 
wp_deregister_style( 'open-sans' ); 
wp_register_style( 'open-sans', false ); 
wp_enqueue_style('open-sans',''); 
} 
add_action( 'init', 'remove_open_sans' );

1. Quick Facebook Sharing

Social media is crucial for getting more audience to your website. The best way to increase your traffic is to make Facebook sharing easy. Luckily, there is a hack which will create a Share on Facebook button on your website.

Find single.php file which is located in your WordPress theme and then paste this code:

2. Login Without an Error Message

If you have a login option on your blog, you already know that once user types in an incorrect username or password, there is an error message on the page. Hackers can try to break into your web page by guessing passwords and/or usernames so the smartest thing you can do in order to protect your blog is to remove the error message because they will not know which piece of information they got right.

Remove the error message with this line of code:

3. Visual or HTML Editor?

Some users prefer visual editor while others are fans of the good old HTML editor. It all comes down to your personal preferences when it comes to posting on your WordPress page. But switching between the editors can be annoying so the best way to make this easier is to choose one to be your default.

This is the code you need to enter in your functions.php file:

For Visual Editor

For HTML Editor

4. Add Your Logo

This tip is great for developers who create or maintain their client’s blogs in WordPress. It will allow you to add your own logo to the page straight from the code. The visitors will know that you made the page and there will be a possibility of expanding your business which is always helpful.

Enter this code:

5. Latest Blog Posts

If you want to group and display the newest blog posts on your WordPress page so the visitors will have a better view of the topics you have covered lately, there is a hack for that. This will make your page cleaner and more user friendly.

If you want more posts on your front page, change 5 in showposts=5 to any number you like.

6. No Display of WordPress Version

When a hacker knows the version of WordPress you are using for your blog, it is easier to find holes and errors they can exploit for breaking into your page and stealing valuable data. You have probably noticed that WordPress will automatically display the current version but you can easily remove it from your page.

This is the code you need:

7. Choose RSS Publishing Time

If you have been using WordPress for some time now, you are aware that some mistakes can sneak into a post and you might publish it unknowingly. Yes, you can always fix a typo as soon as you see it but the first version of the post will end up in your followers’ RSS feed.

This simple line of code will hold back the post from RSS feeds as long as sixty minutes in case you want to edit something later:

8. Custom Footer Text

When making a WordPress site for a client, it is quite important to represent their brand properly. You can add a touch of personality by including a custom footer text on the dashboard beside their company’s logo.

Click on functions.php and paste this line of code:

9. Lower Number of Revisions

Revisions are a useful tool that will allow you to access previous versions of the content you posted on your website. Even though WordPress is capable of saving every single revision, having a large number of them can make you confused. It is best to lower that number and keep the last couple of versions available to you.

If you want to use this hack, open wp-config.php, paste this, and enter the number you want:

10. Better Photographs

Are you a photographer and you use your blog in order to showcase your work? Then you have noticed that WordPress will slightly lower the quality of your images so they will not look as detailed as the original version you have on your computer. But there is a hack that will go around this problem and allow you to upload your work in its full glory.

Here it is:

11. Enable Shortcode in Widgets

WordPress widgets are not capable of recognizing shortcode but their usefulness is undeniable. They will shorten up the length of long code lines and make your job as a developer a lot easier. Widgets will be able to recognize shortcode if you paste this into functions.php file:

12. Remove Widgets from Certain Pages

Everyone loves widgets because they give you extra options and make your WordPress page look better. But sometimes you need a clean design for certain areas and widgets might make the page look cluttered with unnecessary information and links. If you don’t want to edit the entire backend of a page, this simple line of code will remove them without too much effort.

For example, open the functions.php file and paste the below code to disable widgets in home page:

13. Frequency of Autosave

Autosave is a wonderful option that keeps your work secure in case your computer or browser crashes. Your text will be right there waiting for you so there is no need to panic if you are working on a long piece of text. But if you are a prolific writer who hates getting their flow interrupted every single minute with an auto save, you can change the frequency without downloading any third party software. This line of code is customizable and you can choose the exact timing.

Go to wp-config.php file, paste this, and edit it to your liking:

14. Page Number for Search Results

If you have a large variety of content on your WordPress page, the visitors might be using the search option in order to find a particular article that interests them. The standard search engine usually does not displays the number of pages related to the search. Luckily, there is a line of code that can help you out. Open search.php file and paste this:

15. Replace the Howdy

And finally, there is a way to customize the greeting message in the WordPress bar. If Howdy seems too casual for you and you want a formal greeting, go to functions.php, paste the code below, and change $newtitle to a message which is more fitting to your website:

0 Comments

Add Yours →

发表评论