禁用自动生成的图片尺寸

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 禁用自动生成的图片尺寸
function shapeSpace_disable_image_sizes($sizes) {
unset($sizes['thumbnail']); // disable thumbnail size
unset($sizes['medium']); // disable medium size
unset($sizes['large']); // disable large size
unset($sizes['medium_large']); // disable medium-large size
unset($sizes['1536x1536']); // disable 2x medium-large size
unset($sizes['2048x2048']); // disable 2x large size return $sizes;
}
add_action('intermediate_image_sizes_advanced', 'shapeSpace_disable_image_sizes');
// 禁用缩放尺寸
add_filter('big_image_size_threshold', '__return_false');
// 禁用其他图片尺寸
function shapeSpace_disable_other_image_sizes() {
remove_image_size('post-thumbnail'); // disable images added via set_post_thumbnail_size() remove_image_size('another-size'); // disable any other added image sizes
}
add_action('init', 'shapeSpace_disable_other_image_sizes');

后台文章列表搜索支持 ID

1
2
3
4
5
6
7
8
9
10
11
12
13
// 后台文章列表搜索支持 ID
add_filter('posts_clauses', function ($clauses, $wp_query){
if($wp_query->is_main_query() && $wp_query->is_search()){
global $wpdb;
$search_term = $wp_query->query['s'];
if(is_numeric($search_term)){
$clauses['where'] = str_replace('('.$wpdb->posts.'.post_title LIKE', '('.$wpdb->posts.'.ID = '.$search_term.') OR ('.$wpdb->posts.'.post_title LIKE', $clauses['where']);
}elseif(preg_match("/^(d+)(,s*d+)*$/", $search_term)){
$clauses['where'] = str_replace('('.$wpdb->posts.'.post_title LIKE', '('.$wpdb->posts.'.ID in ('.$search_term.')) OR ('.$wpdb->posts.'.post_title LIKE', $clauses['where']);
}
}
return $clauses;
}, 2, 2);

Wordpress文章等ID重排序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
require_once( './wp-config.php' );
function change_post_id($id)
{
global $convertedrows, $wpdb;
/** 修改文章ID关联的类别、标签、自定义字段、评论各表,prefix是您安装时设置的数据库表前缀 */
$wpdb->query( 'update ' . $wpdb->prefix .'posts set ID = ' . $convertedrows . ' where ID = ' . $id );
$wpdb->query( 'update ' . $wpdb->prefix .'term_relationships set object_id = ' . $convertedrows . ' where object_id = ' . $id );
$wpdb->query( 'update ' . $wpdb->prefix .'postmeta set post_id = ' . $convertedrows . ' where post_id = ' . $id );
$wpdb->query( 'update ' . $wpdb->prefix .'comments set comment_post_ID = ' . $convertedrows . ' where comment_post_ID = ' . $id );
$convertedrows++;
}
/** ID默认由1开始 */
$convertedrows = 1;
/** 查询数据库文章表所有记录 */
$sql_query = 'SELECT ID FROM ' . $table_prefix . 'posts ORDER BY ID ASC';
$all_post_ids = $wpdb->get_results( $sql_query );
/** 有返回值时则执行循环 */
if ( is_array( $all_post_ids ) ) {
foreach ( $all_post_ids as $post_id ) {
change_post_id( $post_id->ID );
}
}
/** 重新设置文章ID自动增加的起点 */
$wpdb->query('alter table ' . $table_prefix .'posts AUTO_INCREMENT = ' . $convertedrows);
echo 'Total:'. $convertedrows .', It\'s ok!!! by www.cgfans.cn ';
?>

新建任意*.php名后放入根目录运行即可。

文章最后更新时间/全局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 文章最后更新时间/全局
function my_last_updated_date( $content ) {
$u_time = get_the_time( 'U' );
$u_modified_time = get_the_modified_time( 'U' );
$custom_content = '';
if ( $u_modified_time >= $u_time + 86400 ) {
$updated_date = get_the_modified_time( 'Y年n月j日' );
$custom_content .= '<p class="last-updated entry-meta" style="border:#f5f6f7 0px solid; border-radius:4px 4px 4px 4px; background-color:#f7f7f7 font-family:arial;color:#787c80;font-size:15px;">最后更新于 ' . $updated_date . ' - www.cgfans.cn</p>';
}
$custom_content .= $content;

return $custom_content;
}
add_filter( 'the_content', 'my_last_updated_date' );

文章开头结尾增加固定内容

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// 文章开头增加固定内容
add_filter('the_content', 'add_zm_content_beforde');
function add_zm_content_beforde( $content ) {
if( !is_feed() && !is_home() && is_singular() && is_main_query() ) {
$before_content = '在文章内容开头添加固定内容';
$zm = $before_content . $content;
}
return $zm;
}

// 文章结尾增加固定内容
add_filter('the_content', 'add_zm_content_after');
function add_zm_content_after( $content ) {
if( !is_feed() && !is_home() && is_singular() && is_main_query() ) {
$after_content = '在文章内容末尾添加固定内容';
$zm = $content . $after_content;
}
return $zm;
}

// 文章开头结尾增加固定内容
add_filter('the_content', 'add_zm_content_before_and_after');
function add_zm_content_before_and_after( $content ) {
if( !is_feed() && !is_home() && is_singular() && is_main_query() ) {
$after_content = '在文章内容末尾添加固定内容';
$before_content = '在文章内容开头添加固定内容';
$zm = $before_content . $content . $after_content;
}
return $zm;
}

// 只在自定义文章类型“books”文章末尾添加固定内容
add_filter('the_content', 'add_zm_content_after_books_custom_post_type');
function add_zm_content_after_books_custom_post_type( $content ) {
if (is_singular( "books" )){
$new_books_content = '只在自定义帖子类型“books”文章末尾添加固定内容';
$aftercontent = $new_books_content;
$zm = $content . $aftercontent;
return $zm;
} else {
return $content;
}
}

禁用所有文章类型的修订版本

1
2
3
//禁用所有文章类型的修订版本
add_filter( 'wp_revisions_to_keep', 'fanly_wp_revisions_to_keep', 10, 2 );
function fanly_wp_revisions_to_keep( $num, $post ) { return 0;}

隐藏登陆错误提示

1
2
3
4
function no_wordpress_errors(){
return 'Something is wrong!';
}
add_filter( 'login_errors', 'no_wordpress_errors' );

关闭搜索功能

1
2
3
4
5
6
7
8
9
10
11
12
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;" ) );

改变文章摘要长度

1
2
3
4
functionnew_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function wpb_custom_logo() {
echo '
<style type="text/css">
#wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon:before {
background-image: url(' . get_bloginfo('stylesheet_directory') . '/images/custom-logo.png) !important;
background-position: 0 0;
color:rgba(0, 0, 0, 0);
}
#wpadminbar #wp-admin-bar-wp-logo.hover > .ab-item .ab-icon {
background-position: 0 0;
}
</style>
';
}
//hook into the administrative header output
add_action('wp_before_admin_bar_render', 'wpb_custom_logo');

第二种:https://www.npc.ink/18008.html

自定义后台底部信息

1
2
3
4
function remove_footer_admin () {
echo 'Fueled by <a href="http://www.wordpress.org" target="_blank">WordPress</a> | WordPress Tutorials: <a href="https://www.wpbeginner.com" target="_blank">WPBeginner</a></p>';
}
add_filter('admin_footer_text', 'remove_footer_admin');

评论用户随机显示本地图像

1
2
3
4
5
6
7
8
9
10
11
add_filter( 'get_avatar' , 'local_random_avatar' , 1 , 5 );
function local_random_avatar( $avatar, $id_or_email, $size, $default, $alt) {
if ( ! empty( $id_or_email->user_id ) ) {
$avatar = ''.get_template_directory_uri().'/avatar/admin.jpg';
}else{
$random = mt_rand(1, 10);
$avatar = ''.get_template_directory_uri().'/avatar/'. $random .'.jpg';
}
$avatar = "<img alt='{$alt}' src='{$avatar}' class='avatar avatar-{$size} photo' height='{$size}' width='{$size}' />";
return $avatar;
}

其中:mt_rand(1, 10);数字为随机图片张数可以自行修改。在当前主题根目录下,新建一个名称为avatar的文件夹,里面放10张名称连续的jpg图片,比如1.jpg、2.jpg、3.jpg………,和一张用于管理员的图片,名称为admin.jpg。添加上述代码后,只有管理员是显示固定的图片,其他留言者都是随机显示预先准备的头像图片,包括注册用户。

文章底部版权信息提示

img

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//在所有文章底部添加自定义内容
function add_after_post_content($content) {
if(!is_feed() && !is_home() && is_singular() && is_main_query()) {
$content .= '
<style>
.hh_wenzhangjieshu{padding:30px 0;text-align: center;border-top: double rgba(70, 139, 230, 0.14);}
.hh_wenzhangjieshu img{border:0;text-align:center;box-shadow:none;}
.hh_content_footer{width:100%;height:200px;box-shadow: 0 0px 1px #d9e0eae3;margin-top:0px}
.hh_content_footer .item_top_left{padding:20px;float:left;}
.hh_content_footer .item_top_left p{line-height:14px;font-weight:600;font-size:12px;padding-left: 7px;}
.hh_content_footer .item_top_left h3{color:#f7516c;font-size:18px;font-weight:600;}
.hh_content_footer .item_top_right{float:right;padding: 20px 50px 20px 20px;}
.hh_content_footer .item_top_right .hh_erweima{width:80px;height:80px;background:#f7f7f7;float:left;margin-right:15px;margin-top:15px;padding: 6px;}
.hh_content_footer .item_top_right .hh_qqqun{float:left;margin-left: 20px;}
.hh_content_footer .item_top_right .hh_qqqun img{width: 80px;height: 80px;margin: 0 51px 0 0;float: left;}
.hh_content_footer .item_top_right .hh_qqqun .spanQ1{background:#f4f8ff;}
.hh_content_footer .item_top_right .hh_qqqun .spanQ3{margin: 20px 0 10px;}
.hh_content_footer .item_top_right .hh_qqqun .spanQ2{margin: 10px 0 0;}
.hh_content_footer .item_top_right .hh_qqqun a{display:block;background:#f4f8ff;line-height:30px;border-radius: 15px;margin-top:15px;text-align:center}
.hh_content_footer .item_top{width:100%;height:118px;}
.hh_content_footer .item_bottom {width:100%;float: right;}
.hh_content_footer .item_bottom .item_bottom_left{float:left;margin-left: 27px;}
.hh_content_footer .item_bottom .item_bottom_left a{float:left;text-align: center;padding: 7px 20px;display:block;margin-right:20px;color:#fff;font-weight:600;border-radius: 4px;position: relative;top:-5px;}
.hh_content_footer .item_bottom .item_bottom_right{float:right;font-weight:600;color:#666;margin-right: 44px;}
.hh_content_footer .item_bottom:before{width:100%;height:1px;background: #e6f0ff;display:block;margin-bottom: 20px;}
.hh_content_footer .hh_button_jianbian01{background:linear-gradient(to left,#70a4fe,#4583ec)}
.hh_content_footer .hh_button_jianbian02{background:linear-gradient(to left,#fe9369,#f7516c)}
.hh_content_footer .hh_button_jianbian03{background:linear-gradient(to left,#c867ff,#8a92fb)}
.hh_content_footer .hh_button_jianbian01:hover{background:linear-gradient(to left,#4583ec,#70a4fe)}
.hh_content_footer .hh_button_jianbian02:hover{background:linear-gradient(to left,#f7516c,#fe9369)}
.hh_content_footer .hh_button_jianbian03:hover{background:linear-gradient(to left,#8a92fb,#c867ff)}
@media (max-width:767px){
.hh_content_footer{height:360px;}
.hh_content_footer .item_top_right{float: none;position: relative;top: -23px;}
.hh_content_footer .item_bottom{margin-top:30px}
.hh_content_footer .item_bottom .item_bottom_right{float:none;margin-top: 61px;text-align: center;}
}
.hh_qqqun a img{position: relative;top: 7px;right: 2px;}
</style>
<div style="text-align: center;" class="hh_wenzhangjieshu"><img src="图片URL"/></div>
<div class="hh_content_footer">
<div class="item_top">
<div class="item_top_left">
<h3>【Npcink】温馨提醒:</h3>
<p>本站所有文章都可以进行提问,有问必答,欢迎骚扰!</p>
<p>如果您喜欢我们,请收藏本站,您将得到更好的服务。 如有侵权请邮件与我们联系处理。 </p>
</div>
<div class="item_top_right"><div class="hh_qqqun">
<br>
<img src="图片URL">
<!-- <span class="spanQ1 spanQ3">QQ交流群</span>
<br>
<span class="spanQ1 spanQ2">QQ交流群</span> -->
</div></div>
</div>
<div class="item_bottom">
<div class="item_bottom_left">
<a href="/bubble" class="hh_button_jianbian01" target="_blank">投诉建议</a>
<a href="/vips" class="hh_button_jianbian02" target="_blank">购买会员</a>
<a href="/shop" class="hh_button_jianbian03" target="_blank">服务商城</a>
</div>
<div class="item_bottom_right">
© 转载请联系作者,私自转载视为侵权!
</div>
</div>
</div>';
}
return $content;
}
add_filter('the_content', 'add_after_post_content');
/*移动端隐藏上述部份版权声明-放置于主题根目录styel.css中*/
@media (max-width: 767px){
.hh_content_footer .item_bottom {display:none;}
.hh_content_footer .item_top_right {display:none;}
.hh_content_footer {
height: 2px!important;
}
.hh_wenzhangjieshu {display:none;}
}
/*移动端隐藏上述版权声明-放置于主题根目录styel.css中*/
@media (max-width: 767px){
.hh_wenzhangjieshu {display:none;}
.hh_content_footer{display:none;}
}

支持SVG图片上传

1
2
3
4
5
6
//支持svg上传
add_filter('upload_mimes', 'wpjam_upload_mimes');
function wpjam_upload_mimes($mimes = array()) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}

WordPress 安全:禁用 wp-login.php 登录页面,防止被机器扫描爆破

1
2
3
4
5
6
7
8
9
10
//WordPress 安全:彻底禁用 wp-login.php 登录页面,防止被机器扫描爆破
add_action('init','lxtx_disable_wplogin_page');
function lxtx_disable_wplogin_page(){
global $pagenow;
if( 'wp-login.php' == $pagenow && !is_user_logged_in() ) {
$url = home_url();
wp_redirect($url);
exit();
}
}

此方法直接封禁 wp-login.php 页面的各种请求,各种扫描爆破登录也就无效了。当然,默认的 WP 登录页面也不能进入了;对于那些使用前台弹窗登录或自定义登录页面的高级主题来说,不会收到任何影响。

经典编辑器增加一个按钮快速输入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// 添加自定义编辑器按钮
function appthemes_add_quicktags() {
?>
<script type="text/javascript">
if ( typeof QTags != 'undefined' ) {
QTags.addButton( 'hr', 'hr', '<hr />', '' ); //添加分隔线
QTags.addButton( 'h1', 'h1', '<h1>', '</h1>' ); //快捷输入h1标签
QTags.addButton( 'TKLS', '一键复制', '<button class="itemCopy red_tkl button_tkl" id="TKLS" type="button" data-clipboard-text="替换成需要复制的内容">一键复制</button>' ); //添加一键复制
QTags.addButton( 'TKLS', '打开链接并复制', '<button class="itemCopy red_tkl button_tkl" id="TKLS" type="button" data-clipboard-text="需要复制的内容"><a style="color: #ffffff;" href="需要打开的链接地址" onClick="copyUrl2()" target="_blank" rel="noopener noreferrer">按钮显示的文字</button>' ); //添加打开链接并复制
// QTags.addButton( '按钮的ID', '显示的名称', '点一下输入的内容', '再点一下输入的内容(此留空则一次输入全部内容)' );
}
</script>
<?php
}
add_action('admin_print_footer_scripts', 'appthemes_add_quicktags' );

百度主动推送

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
百度实时推送
https://ziyuan.baidu.com/linksubmit/
*/
function mee_post_baidu($post_id,$post){
$PostUrl = get_permalink($post_id);
$urls=array($PostUrl);
$api = '你的api调用地址';
$ch = curl_init();//主机需要支持curl
$options = array(
CURLOPT_URL => $api,
CURLOPT_POST => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => implode("\n", $urls),
CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
);
curl_setopt_array($ch, $options);
curl_exec($ch);
}
add_action('publish_post', 'mee_post_baidu');

文章别名为文章ID

1.修改文章固定连接为

1
/%postname%.html

2.修改就文章的别名为文章ID

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 修改WordPress旧文章别名为文章ID
function Bing_post_name_id(){
query_posts( 'posts_per_page=-1' );
while( have_posts() ){
the_post();
$post_id = $GLOBALS['post']->ID;
wp_update_post( array(
'ID' => $post_id,
'post_name' => $post_id
) );
}
wp_reset_query();
}
if( $_GET['post_name_id'] == 'yes' ) add_action( 'init', 'Bing_post_name_id' );

3.新文章自动使用文章ID作为别名

  • 将下边代码放入functions.php 即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/**
* WordPress新文章自动使用ID作为别名
* 作用:即使你设置固定连接结构为 %postname% ,仍旧自动生成 ID 结构的链接
*/
add_action( 'save_post', 'using_id_as_slug', 10, 2 );
function using_id_as_slug($post_id, $post){
global $post_type;
if($post_type=='post'){ //只对文章生效
// 如果是文章的版本,不生效
if (wp_is_post_revision($post_id))
return false;
// 取消挂载该函数,防止无限循环
remove_action('save_post', 'using_id_as_slug' );
// 使用文章ID作为文章的别名
wp_update_post(array('ID' => $post_id, 'post_name' => $post_id ));
// 重新挂载该函数
add_action('save_post', 'using_id_as_slug' );
}
}