PHP苦手,记录一下自定义的一些代码&操作,以免以后忘了,虽然基本都是百度的
改变未加摘要时自动截取的文章摘要长度,functions.php中添加
function new_excerpt_length($length) {
return 150;
}
add_filter("excerpt_length", "new_excerpt_length");
改变文章摘要省略符号,functions.php中添加
function new_excerpt_more($more) {
return '……';
}
add_filter('excerpt_more', 'new_excerpt_more');
改变编辑器字体,functions.php中添加
function add_fontfamily($initArray){
$initArray['font_formats'] = "微软雅黑='微软雅黑';宋体='宋体';黑体='黑体';仿宋='仿宋';楷体='楷体';隶书='隶书';幼圆='幼圆';Arial='Arial';";
return $initArray;
}
add_filter('tiny_mce_before_init', 'add_fontfamily');
改变scrollme主题首页blog展示的摘要长度,scrollme-functions.php中修改数值
<?php echo wp_trim_words(get_the_content(), 75); ?>
注册发送的邮件链接失效,原因是wordpress为链接加上的“<”、“>”被邮箱解析为地址的一部分,解决方法:
新注册邮件:wordpress/wp-includes目录的pluggable.php中,找到'<‘、’>’并删除
重设密码的邮件:在functions.php中添加
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);
显然二者可以用同一个方法但是我不会啊。。只能将百度到的整合到一起
隐藏登录后的工具栏,在functions.php中添加
show_admin_bar(false);
或者
add_filter('show_admin_bar', '__return_false');
对管理员显示
if (!current_user_can('manage_options')) {
add_filter('show_admin_bar', '__return_false');
}
对管理员和编辑显示
if(!current_user_can('edit_posts')) {
add_filter('show_admin_bar', '__return_false');
}
关于用户的优化
<?php
global $current_user;
get_currentuserinfo();
if ( is_user_logged_in() ) {
echo '<a title="管理我的账户" href="/wp-admin/profile.php">'.$current_user->display_name.'</a>';
echo ',';
echo '<a title="Logout" href="<?php echo wp_logout_url(get_permalink()); ?>">注销</a>';
} else {
echo '<a title="Login" href="<?php echo wp_login_url(get_permalink()); ?>">登陆/注册</a>';
}
?>
利用google的服务获得指定网站的图标
www.google.com/s2/favicons?domain=%url
添加页面的计数功能,functions.php
/* 访问计数 */
function record_visitors()
{
if (is_singular())
{
global $post;
$post_ID = $post->ID;
if($post_ID)
{
$post_views = (int)get_post_meta($post_ID, 'views', true);
if(!update_post_meta($post_ID, 'views', ($post_views+1)))
{
add_post_meta($post_ID, 'views', 1, true);
}
}
}
}
add_action('wp_head', 'record_visitors');
/// 函数名称:post_views
/// 函数作用:取得文章的阅读次数
function post_views($before = '(点击 ', $after = ' 次)', $echo = 1)
{
global $post;
$post_ID = $post->ID;
$views = (int)get_post_meta($post_ID, 'views', true);
if ($echo)
echo $before, number_format($views), $after;
else
return $views;
}
需要用的地方添加
<?php post_views(' ', ' 次'); ?>
nginx链接静态化配置
location / {
index index.html index.php;
if (-f $request_filename/index.html){
rewrite (.*) $1/index.html break;
}
if (-f $request_filename/index.php){
rewrite (.*) $1/index.php;
}
if (!-f $request_filename){
rewrite (.*) /index.php;
}
}
location ~ .*\.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
}
改变自带的最近评论小工具,使其显示评论内容,修改/wp-includes/widgets/class-wp-widget-recent-comments.php的代码
foreach ( (array) $comments as $comment ) {
$output .= '<li class="recentcomments">';
$output .= sprintf(_x('%1$s 在“%2$s”留言:<br>', 'widgets')
,'<span class="comment-author-link">'.get_comment_author_link($comment )
.'</span>','<a href="'.esc_url(get_comment_link($comment->comment_ID)) .'">'
.get_the_title($comment->comment_post_ID).'</a>')
.'<span>'.strip_tags($comment->comment_content).'</span></li>';
}
隐藏自带小工具“分类目录”中的指定目录,function.php中添加
function exclude_widget_categories($args)
{
$exclude = "3,6,18"; // The IDs of the excluding categories
$args["exclude"] = $exclude;
return $args;
}
add_filter("widget_categories_args","exclude_widget_categories");
简单修复数据库问题
mysqlcheck -u wordpress -p wordpress --auto-repair
3 赞
不错 收藏
重置密码那个…我找到的版本是在wp-login.php里删那对尖括号…