wordpress常用functions代码





wordpress一些可代替插件的代码,将代码放在当前主题的functions.php文件里面即可。

/***移除wordpress后台顶部左上角图标及链接**/
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);

/***更改wordpress登录页面LOGO**/
add_filter(‘login_headerurl’,create_function(false,”return get_bloginfo(‘siteurl’);”)); //修改链接地址
add_filter(‘login_headertitle’,create_function(false,”return get_bloginfo(‘name’);”));  //修改链接地址标题提示name是站名也可以是description
function nowspark_login_head(){echo ‘<style type=”text/css”>body.login #login h1 a {background:url(http://www.tuhongwei.com/wp-admin/images/tuhongwei-login-logo.png) no-repeat 0 0 transparent;height:50px;width:310px;padding:0;margin:0 auto 1em;} </style>’ ;}
add_action(“login_head”,”nowspark_login_head”);

/***自定义wordpress后台底部版权和版本信息**/
add_filter(‘admin_footer_text’,’left_admin_footer_text’);
function left_admin_footer_text($text){
//左边信息
$text='<span id=”footer-thankyou”><a href=”http://www.tuhongwei.com“>涂红伟</a>感谢有你!</span>’;
return $text;
}
add_filter(‘update_footer’, ‘right_admin_footer_text’, 11);
function right_admin_footer_text($text){
//右边信息
$text=’Tuhongwei.com’;
return $text;
}

/***去掉wordpress仪表盘的某些模块**/
function remove_dashboard_widgets(){
global $wp_meta_boxes;
//删除wordpress开发日志模块
unset ($wp_meta_boxes[‘dashboard’] [‘side’] [‘core’] [‘dashboard_primary’]);
//删除“概览”
unset ($wp_meta_boxes[‘dashboard’][‘normal’][‘core’][‘dashboard_right_now’]);
//删除”其他wordpress新闻模块
unset ($wp_meta_boxes[‘dashboard’] [‘side’] [‘core’] [‘dashboard_secondary’]);
}
add_action(‘wp_dashboard_setup’, ‘remove_dashboard_widgets’);

/***屏蔽wordpress后台“显示选项”和“帮助”**/
function remove_screen_options() {return false;}
add_filter(‘screen_options_show_screen’,’remove_screen_options’);
add_filter(‘contextual_help’,’wpse50723_remove_help’,999,3);
function wpse50723_remove_help($old_help,$screen_id,$screen){$screen->remove_help_tabs();
return $old_help;
}

/***实现wordpress文章首行缩进2字符  **/
function xmlas_indent_txt($text){
$return = str_replace(‘<p’, ‘<p style=”text-indent:2em;”‘,$text);
return $return;
}
add_filter(‘the_content’,’xmlas_indent_txt’);

/***自定义wordpress自带标签字体大小**/
add_filter(‘widget_tag_cloud_args’,’theme_tag_cloud_args’);
function theme_tag_cloud_args($args){
$newargs=array(
‘smallest’ =>12,   //最小字号,默认为8;
‘largest’ =>12,   //最大字号,默认为22;
‘unit’ =>’pt’,   //字号单位,可以是pt、px、em或%默认为pt;
‘number’ =>45,   //显示个数,默认为45;
‘format’ =>’list’,   //列表格式可以是flat、list或array默认为flat;
‘separator’ =>”\n”,   //分隔每一项的分隔符
‘orderby’ =>’name’,   //排序方式 name或count(按标签使用次数排列)默认为name;
‘order’ =>’ASC’,   //升序或降序ASC或DESC默认为ASC
‘exclude’ =>null,   //结果中排除某些标签
‘include’ =>null,   //结果中只包含某些标签
‘link’ =>’view’ ,   //taxonomy链接,view或edit默认为view
‘taxonomy’ =>’post_tag’,   //调用哪些分类法作为标签云
);
$return=array_merge($args,$newargs);
return $return;
}

/*** 移除 WordPress 加载的JS和CSS链接中的版本号**/
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 );
remove_action( ‘wp_head’, ‘wp_generator’ );

/***完整的删除WordPress的版本号**/
function wpbeginner_remove_version() {
return ”;
}
add_filter(‘the_generator’, ‘wpbeginner_remove_version’);

/**让wordpress友情链接只首页显示*/
function rbt_friend_links($output){
if (!is_home()|| is_paged()){
$output = “”;
}
return $output;
}
add_filter(‘wp_list_bookmarks’,’rbt_friend_links’);

/**去掉wordpress评论框上的网址*/
add_filter(‘comment_form_default_fields’, ‘unset_url_field’);
function unset_url_field($fields){
if(isset($fields[‘url’]))
unset($fields[‘url’]);
return $fields;
}

/*** 修复 WordPress 找回密码提示“抱歉,该key似乎无效”**/
function reset_password_message( $message, $key ) {
if ( strpos($_POST[‘user_login’], ‘@’) ) {
$user_data = get_user_by(’email’, trim($_POST[‘user_login’]));
} else {
$login = trim($_POST[‘user_login’]);
$user_data = get_user_by(‘login’, $login);
}
$user_login = $user_data->user_login;
$msg = __(‘有人要求重设如下帐号的密码:’). “\r\n\r\n”;
$msg .= network_site_url() . “\r\n\r\n”;
$msg .= sprintf(__(‘用户名:%s’), $user_login) . “\r\n\r\n”;
$msg .= __(‘若这不是您本人要求的,请忽略本邮件,一切如常。’) . “\r\n\r\n”;
$msg .= __(‘要重置您的密码,请打开下面的链接:’). “\r\n\r\n”;
$msg .= network_site_url(“wp-login.php?action=rp&key=$key&login=” . rawurlencode($user_login), ‘login’) ;
return $msg;
}
add_filter(‘retrieve_password_message’, reset_password_message, null, 2);

/***给wordpress编辑器添加下载按钮***/
function download($atts, $content = null) {
return ‘<a href=”‘.$content.'” rel=”external”
target=”_blank” title=”下载地址”>
<img src=”https://www.tuhongwei.com/wp-content/uploads/download.png” border=”0″ width=”242″ height=”62″ /></a>’;}
add_shortcode(“download”, “download”);
add_action(‘after_wp_tiny_mce’, ‘bolo_after_wp_tiny_mce’);
function bolo_after_wp_tiny_mce($mce_settings) {
?>
<script type=”text/javascript”>
QTags.addButton( ‘download’, ‘下载按钮’, “ ” );
function bolo_QTnextpage_arg1() {
}
</script>
<?php }

/**自定义wordpress用户注册地址**/
function sj_redir_login() {
wp_redirect(bloginfo(‘url’).’http://www.tuhongwei.com/member.php?mod=register‘); //这个地址可以随意修改
exit;
}
add_action( ‘login_enqueue_scripts’, ‘sj_redir_login’ );

/* 自动将wordpress图片转换为 https */
function my_content_manipulator($content){
if( is_ssl() ){
$content = str_replace(‘http://www.tuhongwei.com/wp-content/uploads‘, ‘https://www.tuhongwei.com/wp-content/uploads‘, $content);
}
return $content;
}
add_filter(‘the_content’, ‘my_content_manipulator’);

/**去除谷歌字体**/
if (!function_exists('remove_wp_open_sans')) :
function remove_wp_open_sans() {
wp_deregister_style( 'open-sans' );
wp_register_style( 'open-sans', false );
}
/**前台删除Google字体CSS**/
add_action('wp_enqueue_scripts', 'remove_wp_open_sans');
/**后台删除Google字体CSS**/
add_action('admin_enqueue_scripts', 'remove_wp_open_sans');
endif;

注:如果复制的代码不能用请检测代码中的标点符号,所有标点符号必须是英文半角符号。