欢迎光临
我们一直在努力

WordPress关于functions.php能解决的事情,不要装插件

自己建WordPress的过程中收集的functions.php有用的代码,分享出来

1、
//支持中文名注册
function git_sanitize_user ($username, $raw_username, $strict) {
$username = wp_strip_all_tags( $raw_username );
$username = remove_accents( $username );
$username = preg_replace( ‘|%([a-fA-F0-9][a-fA-F0-9])|’, ”, $username );
$username = preg_replace( ‘/&.+?;/’, ”, $username ); // Kill entities
if ($strict) {
$username = preg_replace (‘|[^a-z\p{Han}0-9 _.\-@]|iu’, ”, $username);
}
$username = trim( $username );
$username = preg_replace( ‘|\s+|’, ‘ ‘, $username );
return $username;
}
add_filter (‘sanitize_user’, ‘git_sanitize_user’, 10, 3);

 

 

2、
// 评论添加@
function git_comment_add_at( $comment_text, $comment = ”) {
if( $comment->comment_parent > 0) {
$comment_text = ‘@<a href=”#comment-‘ . $comment->comment_parent . ‘”>’.get_comment_author( $comment->comment_parent ) . ‘</a> ‘ . $comment_text;
}
return $comment_text;
}
add_filter( ‘comment_text’ , ‘git_comment_add_at’, 20, 2);

add_action( ‘register_form’, ‘add_security_question’ );

 

 

3、
// 注册输入字符验证 //
function add_security_question() { ?>
<p>
<label><?php _e(‘请输入本站域名:youran.cn’) ?><br />
<input type=”text” name=”user_proof” id=”user_proof” class=”input” size=”25″ tabindex=”20″ /></label>
</p>
<?php }

add_action( ‘register_post’, ‘add_security_question_validate’, 10, 3 );
function add_security_question_validate( $sanitized_user_login, $user_email, $errors) {
// 如果没有回答
if (!isset($_POST[ ‘user_proof’ ]) || empty($_POST[ ‘user_proof’ ])) {
return $errors->add( ‘proofempty’, ‘<strong>错误</strong>: 您还没有回答问题。’ );
// 如果答案不正确
} elseif ( strtolower( $_POST[ ‘user_proof’ ] ) != ‘youran.cn’ ) {
return $errors->add( ‘prooffail’, ‘<strong>错误</strong>: 您的回答不正确。’ );
}
}

 

 

4、
//编辑器增强
function add_editor_buttons($buttons) {
$buttons[] = ‘fontselect’;
$buttons[] = ‘fontsizeselect’;
$buttons[] = ‘cleanup’;
$buttons[] = ‘styleselect’;
$buttons[] = ‘hr’;
$buttons[] = ‘del’;
$buttons[] = ‘sub’;
$buttons[] = ‘sup’;
$buttons[] = ‘copy’;
$buttons[] = ‘paste’;
$buttons[] = ‘cut’;
$buttons[] = ‘undo’;
$buttons[] = ‘image’;
$buttons[] = ‘anchor’;
$buttons[] = ‘backcolor’;
$buttons[] = ‘wp_page’;
$buttons[] = ‘charmap’;
return $buttons;
}
add_filter(“mce_buttons_3”, “add_editor_buttons”);

 

 

5、
// 添加编辑器中文字体//
function custum_fontfamily($initArray){
$initArray[‘font_formats’] = “微软雅黑=微软雅黑;宋体=宋体;黑体=黑体;仿宋=仿宋;楷体=楷体;隶书=隶书;幼圆=幼圆;Andale Mono=andale mono,times;Arial=arial,helvetica,sans-serif;Arial Black=arial black,avant garde;Book Antiqua=book antiqua,palatino;Comic Sans MS=comic sans ms,sans-serif;Courier New=courier new,courier;Georgia=georgia,palatino;Helvetica=helvetica;Impact=impact,chicago;Symbol=symbol;Tahoma=tahoma,arial,helvetica,sans-serif;Terminal=terminal,monaco;Times New Roman=times new roman,times;Trebuchet MS=trebuchet ms,geneva;Verdana=verdana,geneva;Webdings=webdings;Wingdings=wingdings,zapf dingbats”;
return $initArray;
}
add_filter(‘tiny_mce_before_init’, ‘custum_fontfamily’);
function enable_more_buttons($buttons) {
$buttons[] = ‘styleselect’;
$buttons[] = ‘fontselect’;
return $buttons;
}
add_filter(“mce_buttons”, “enable_more_buttons”);

 

 

6、
//// ————— 添加用户IP信息及注册信息 ————- /////**
* add the register record.
*/
function get_client_ip() {
if (getenv(“HTTP_CLIENT_IP”) && strcasecmp(getenv(“HTTP_CLIENT_IP”), “unknown”))
$ip = getenv(“HTTP_CLIENT_IP”);
else if (getenv(“HTTP_X_FORWARDED_FOR”) && strcasecmp(getenv(“HTTP_X_FORWARDED_FOR”),
“unknown”))
$ip = getenv(“HTTP_X_FORWARDED_FOR”);
else if (getenv(“REMOTE_ADDR”) && strcasecmp(getenv(“REMOTE_ADDR”), “unknown”))
$ip = getenv(“REMOTE_ADDR”);
else if (isset ($_SERVER[‘REMOTE_ADDR’]) && $_SERVER[‘REMOTE_ADDR’] && strcasecmp($_SERVER[‘REMOTE_ADDR’], “unknown”))
$ip = $_SERVER[‘REMOTE_ADDR’];
else
$ip = “unknown”;
return ($ip);
}
// 创建一个新字段存储用户注册时的IP地址
add_action(‘user_register’, ‘log_ip’);
function log_ip($user_id){
$ip = get_client_ip();
update_user_meta($user_id, ‘signup_ip’, $ip);
}
// 创建新字段存储用户登录时间和登录IP
add_action( ‘wp_login’, ‘insert_last_login’ );
function insert_last_login( $login ) {
global $user_id;
$user = get_userdatabylogin( $login );
update_user_meta( $user->ID, ‘last_login’, current_time( ‘mysql’ ) );
$last_login_ip = get_client_ip();
update_user_meta( $user->ID, ‘last_login_ip’, $last_login_ip);
}
// 添加额外的栏目
add_filter(‘manage_users_columns’, ‘add_user_additional_column’);
function add_user_additional_column($columns) {
$columns[‘user_nickname’] = ‘昵称’;
$columns[‘user_url’] = ‘网站’;
$columns[‘reg_time’] = ‘注册时间’;
$columns[‘signup_ip’] = ‘注册IP’;
$columns[‘last_login’] = ‘上次登录’;
// 打算将注册IP和注册时间、登录IP和登录时间合并显示,所以我注销下面两行
/*$columns[‘signup_ip’] = ‘注册IP’;*/
$columns[‘last_login_ip’] = ‘登录IP’;
unset($columns[‘name’]);//移除“姓名”这一栏,如果你需要保留,删除这行即可
return $columns;
}
//显示栏目的内容
add_action(‘manage_users_custom_column’, ‘show_user_additional_column_content’, 10, 3);
function show_user_additional_column_content($value, $column_name, $user_id) {
$user = get_userdata( $user_id );
// 输出“昵称”
if ( ‘user_nickname’ == $column_name )
return $user->nickname;
// 输出用户的网站
if ( ‘user_url’ == $column_name )
return ‘<a href=”‘.$user->user_url.'” target=”_blank”>’.$user->user_url.'</a>’;
// 输出注册时间和注册IP
if(‘reg_time’ == $column_name ){
return get_date_from_gmt($user->user_registered) ;
}
// 输出注册时间和注册IP
if(‘signup’ == $column_name ){
return get_user_meta( $user->ID, ‘signup_ip’, true);
}
// 输出最近登录时间和登录IP
if ( ‘last_login’ == $column_name && $user->last_login ){
return get_user_meta( $user->ID, ‘last_login’, ture );
}

// 输出最近登录时间和登录IP
if ( ‘last_login_ip’ == $column_name ){
return get_user_meta( $user->ID, ‘last_login_ip’, ture );
}
return $value;
}

// 默认按照注册时间排序
add_filter( “manage_users_sortable_columns”, ‘cmhello_users_sortable_columns’ );
function cmhello_users_sortable_columns($sortable_columns){
$sortable_columns[‘reg_time’] = ‘reg_time’;
return $sortable_columns;
}
add_action( ‘pre_user_query’, ‘cmhello_users_search_order’ );
function cmhello_users_search_order($obj){
if(!isset($_REQUEST[‘orderby’]) || $_REQUEST[‘orderby’]==’reg_time’ ){
if( !in_array($_REQUEST[‘order’],array(‘asc’,’desc’)) ){
$_REQUEST[‘order’] = ‘desc’;
}
$obj->query_orderby = “ORDER BY user_registered “.$_REQUEST[‘order’].””;
}
}

 

7、
//** 替换WordPress登录logo图标 *//
function custom_loginlogo() {
echo ‘<style type=”text/css”>
h1 a {background-image: url(‘.get_bloginfo(‘template_directory’).’/login_logo.png) !important; }
</style>’;
}
add_action(‘login_head’, ‘custom_loginlogo’);
8、
//禁止全英文和日文评论
function syz_comment_post( $incoming_comment ) {
$pattern = ‘/[一-龥]/u’;
if(!preg_match($pattern, $incoming_comment[‘comment_content’])) {
wp_die( “请写汉字吧,站长不习惯阅读英文!Please write Chinese characters, I am not used to reading English!” );
}
return( $incoming_comment );
}
add_filter(‘preprocess_comment’, ‘syz_comment_post’);
9
//给图片自动添加ALT和TITLE
function image_alt_tag($content){
global $post;preg_match_all(‘/<img (.*?)\/>/’, $content, $images);
if(!is_null($images)) {foreach($images[1] as $index => $value)
{$new_img = str_replace(‘<img’, ‘<img alt=”‘.get_the_title().’-‘.get_bloginfo(‘name’).'” title=”‘.get_the_title().’-‘.get_bloginfo(‘name’).'”‘, $images[0][$index]);
$content = str_replace($images[0][$index], $new_img, $content);}}
return $content;
}
add_filter(‘the_content’, ‘image_alt_tag’, 99999);
 收藏 (0) 打赏

您可以选择一种方式赞助本站

支付宝扫一扫赞助

微信钱包扫描赞助

未经允许不得转载:源码猫 » WordPress关于functions.php能解决的事情,不要装插件

分享到: 更多 (0)
avatar

热门文章

  • 评论 抢沙发

    评论前必须登录!

    立即登录   注册

    切换注册

    登录

    忘记密码 ?

    您也可以使用第三方帐号快捷登录

    切换登录

    注册

    我们将发送一封验证邮件至你的邮箱, 请正确填写以完成账号注册和激活