主题文件夹中functions.php文件
//移除 WordPress 文章标题前的“私密/密码保护”提示文字
function remove_title_prefix($content) {
return '%s';//这个不能省略
}
add_filter('private_title_format', 'remove_title_prefix');//私密
add_filter('protected_title_format', 'remove_title_prefix');//密码保护
//上传的多媒体重命名
add_filter('wp_handle_upload_prefilter', 'custom_upload_filter' );
function custom_upload_filter( $file ){
date_default_timezone_set("Asia/Shanghai");//设置下时区
$info = pathinfo($file['name']);
$ext = $info['extension'];
$filedate = date('YmdHis').rand(10,99);//为了避免时间重复,再加一段2位的随机数
$file['name'] = $filedate.'.'.$ext;
return $file;
}
//标签云样式相关修改,主要修改$newargs参数
add_filter( 'widget_tag_cloud_args', 'theme_tag_cloud_args' );
function theme_tag_cloud_args( $args ){
$newargs = array(
'smallest' => 8, //最小字号
'largest' => 22, //最大字号
'unit' => 'pt', //字号单位,可以是pt、px、em或%
'number' => 30, //显示个数
'format' => 'flat',//列表格式,可以是flat、list或array
'separator' => "\n", //分隔每一项的分隔符
'orderby' => 'name',//排序字段,可以是name或count
'order' => 'DESC', //升序或降序,ASC或DESC
'exclude' => null, //结果中排除某些标签
'include' => null, //结果中只包含这些标签
'link' => 'view', //taxonomy链接,view或edit
'taxonomy' => 'post_tag', //调用哪些分类法作为标签云
);
//$return = array_merge( $args, $newargs);//用这个的话是默认标签云样式
$return = array_merge(array( 'largest' => 22, 'number' => 4,'orderby'=>'count'), $newargs);
return $return;
}
//去除分类标志代码
//修改后重新保存“固定链接”
add_action( 'load-themes.php', 'no_category_base_refresh_rules');
add_action('created_category', 'no_category_base_refresh_rules');
add_action('edited_category', 'no_category_base_refresh_rules');
add_action('delete_category', 'no_category_base_refresh_rules');
function no_category_base_refresh_rules() {
global $wp_rewrite;
$wp_rewrite -> flush_rules();
}
// register_deactivation_hook(__FILE__, 'no_category_base_deactivate');
// function no_category_base_deactivate() {
// remove_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
// // We don't want to insert our custom rules again
// no_category_base_refresh_rules();
// }
// Remove category base
add_action('init', 'no_category_base_permastruct');
function no_category_base_permastruct() {
global $wp_rewrite, $wp_version;
if (version_compare($wp_version, '3.4', '<')) {
// For pre-3.4 support
$wp_rewrite -> extra_permastructs['category'][0] = '%category%';
} else {
$wp_rewrite -> extra_permastructs['category']['struct'] = '%category%';
}
}
// Add our custom category rewrite rules
add_filter('category_rewrite_rules', 'no_category_base_rewrite_rules');
function no_category_base_rewrite_rules($category_rewrite) {
//var_dump($category_rewrite); // For Debugging
$category_rewrite = array();
$categories = get_categories(array('hide_empty' => false));
foreach ($categories as $category) {
$category_nicename = $category -> slug;
if ($category -> parent == $category -> cat_ID)// recursive recursion
$category -> parent = 0;
elseif ($category -> parent != 0)
$category_nicename = get_category_parents($category -> parent, false, '/', true) . $category_nicename;
$category_rewrite['(' . $category_nicename . ')/(?:feed/)?(feed|rdf|rss|rss2|atom)/?$'] = 'index.php?category_name=$matches[1]&feed=$matches[2]';
$category_rewrite['(' . $category_nicename . ')/page/?([0-9]{1,})/?$'] = 'index.php?category_name=$matches[1]&paged=$matches[2]';
$category_rewrite['(' . $category_nicename . ')/?$'] = 'index.php?category_name=$matches[1]';
}
// Redirect support from Old Category Base
global $wp_rewrite;
$old_category_base = get_option('category_base') ? get_option('category_base') : 'category';
$old_category_base = trim($old_category_base, '/');
$category_rewrite[$old_category_base . '/(.*)$'] = 'index.php?category_redirect=$matches[1]';
//var_dump($category_rewrite); // For Debugging
return $category_rewrite;
}
// Add 'category_redirect' query variable
add_filter('query_vars', 'no_category_base_query_vars');
function no_category_base_query_vars($public_query_vars) {
$public_query_vars[] = 'category_redirect';
return $public_query_vars;
}
// Redirect if 'category_redirect' is set
add_filter('request', 'no_category_base_request');
function no_category_base_request($query_vars) {
//print_r($query_vars); // For Debugging
if (isset($query_vars['category_redirect'])) {
$catlink = trailingslashit(get_option('home')) . user_trailingslashit($query_vars['category_redirect'], 'category');
status_header(301);
header("Location: $catlink");
exit();
}
return $query_vars;
}
//-----------------------------------------------
/*
为特定文章添加特定css最简单的方式.
*/
/*添加自定义CSS的meta box*/
add_action('admin_menu', 'cwp_add_my_custom_css_meta_box');
/*保存自定义CSS的内容*/
add_action('save_post', 'cwp_save_my_custom_css');
/*将自定义CSS添加到特定文章(适用于Wordpress中文章、页面、自定义文章类型等)的头部*/
add_action('wp_head','cwp_insert_my_custom_css');
function cwp_add_my_custom_css_meta_box() {
add_meta_box('my_custom_css', '自定义CSS', 'cwp_output_my_custom_css_input_fields', 'post', 'normal', 'high');
add_meta_box('my_custom_css', '自定义CSS', 'cwp_output_my_custom_css_input_fields', 'page', 'normal', 'high');
}
function cwp_output_my_custom_css_input_fields() {
global $post;
echo '';
echo '';
}
function cwp_save_my_custom_css($post_id) {
if (!wp_verify_nonce($_POST['my_custom_css_noncename'], 'custom-css')) return $post_id;
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
$my_custom_css = $_POST['my_custom_css'];
update_post_meta($post_id, '_my_custom_css', $my_custom_css);
}
function cwp_insert_my_custom_css() {
if (is_page() || is_single()) {
if (have_posts()) : while (have_posts()) : the_post();
echo '';
endwhile; endif;
rewind_posts();
}
}
//-----------------------------------------------
//在后台文章列表增加一列数据
add_filter( 'manage_posts_columns', 'ashuwp_customer_posts_columns' );
function ashuwp_customer_posts_columns( $columns ) {
$columns['views'] = '浏览次数';
return $columns;
}
//输出浏览次数
add_action('manage_posts_custom_column', 'ashuwp_customer_columns_value', 10, 2);
function ashuwp_customer_columns_value($column, $post_id){
if($column=='views'){
$count = get_post_meta($post_id, 'post_views_count', true);
if(!$count){
$count = 0;
}
echo $count;
}
return;
}
//---------------------------------------------------
remove_action('wp_head','wp_shortlink_wp_head',10, 0);//删除源文件中短链接
/*Removes prev and next links*/
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head');
remove_action('wp_head', 'wp_generator');//removes meta name generator.去掉
//---------------------------------------------------
//---------------------------------------------------
add_action('init', 'html_page_permalink', -1);
/*页面,后缀加.html*/
function html_page_permalink() {
global $wp_rewrite;
if ( !strpos($wp_rewrite->get_page_permastruct(), '.html')){
$wp_rewrite->page_structure = $wp_rewrite->page_structure . '.html';
}
}
//---------------------------------------------------
///移除登陆框中wordpress官方链接
//默认的wordpress登陆框带有wordpress官方的logo和链接,通过以下函数去除链接。
add_filter( 'login_headerurl', 'custom_loginlogo_url' );
function custom_loginlogo_url($url){
return home_url( '/' );
}
//更改登陆框logo图标
//如果你对于只去除logo链接不是很知足,可以通过函数代码替换logo。
//注意:echo ”中填写自己的logo路径即可。
add_action('login_head', 'my_custom_login_logo');
function my_custom_login_logo(){
echo '';
}
//---------------------------------------------------
//------------使用smtp发邮件-------------------------
function MBT_mail_smtp( $phpmailer ) {
$phpmailer->IsSMTP();
$phpmailer->SMTPAuth = true;//启用SMTPAuth服务
$phpmailer->Port = 465;//MTP邮件发送端口,这个和下面的对应,如果这里填写25,则下面为空白
$phpmailer->SMTPSecure ="ssl";//是否验证 ssl,这个和上面的对应,如果不填写,则上面的端口须为25
$phpmailer->Host = "smtp.xxxx.com";//邮箱的SMTP服务器地址,如果是QQ的则为:smtp.exmail.qq.com
$phpmailer->Username = "[email protected]";//你的邮箱地址
$phpmailer->Password ="123456789";//你的邮箱登录密码
}
add_action('phpmailer_init', 'MBT_mail_smtp');
//下面这个很重要,得将发件地址改成和上面smtp邮箱一致才行。
function MBT_wp_mail_from( $original_email_address ) {
return '[email protected]';
}
add_filter( 'wp_mail_from', 'MBT_wp_mail_from' );
//---------------------------------------------------
//隐藏admin Bar
function hide_admin_bar($flag) {
return false;
}
add_filter('show_admin_bar','hide_admin_bar');
//---------------------------------------------------
//用户修改密码后,禁止给管理员发送邮件
if(!function_exists( 'wp_password_change_notification' ) ) {
function wp_password_change_notification( $user ) {
return;
}
}
//禁止给用户自己发修改密码成功通知邮件
add_filter( 'send_password_change_email', '__return_false' );
//关闭新用户注册通知站长的邮件
add_filter( 'wp_new_user_notification_email_admin', '__return_false' );
//---------------------------------------------------
//后台顶部bar,
//屏蔽wordpress后台(仪表盘)顶部logo
function annointed_admin_bar_remove() {
global $wp_admin_bar;
/* Remove their stuff */
$wp_admin_bar->remove_menu('wp-logo');
}
add_action('wp_before_admin_bar_render', 'annointed_admin_bar_remove', 0);
//---------------------------------------------------
//---------------------------------------------------
//适合2019/8/%post_id%.html分页链接修正
//解析url的钩子
add_filter('post_rewrite_rules', 'add_custom_post_rewrite_rules');
function add_custom_post_rewrite_rules($rules) {
$custom_rules = array('(\d+)/(\d+)/(\d+)_(\d+)\.html$' => 'index.php?p=$matches[3]&page=$matches[4]',);
//你想改的样子
$rules = array_merge($custom_rules,$rules);
return $rules;
}
//设置url钩子
add_filter('wp_link_pages_link', 'post_custom_rewrite_url');
function post_custom_rewrite_url($output){
$preg = "/(.*)\/(.*)\/(\d+)\.html\/(\d+)/";//你目前的分页链接
$output = preg_replace($preg, "$1/$2/$3_$4.html", $output);//要匹配的连接
return $output;
}
//不许跳转
add_filter( 'redirect_canonical','post_custom_redirect_url');
function post_custom_redirect_url($output){
return false;
}