二次开发wordpress的 function.php步骤

综合

安装完成wordpress后,最好先进行写简单的设置,当然在使用过程中还有很多其他的设置,可以慢慢的修改,现在主要说刚装完的简单配置。

1.设置wordpress文章评论中不能携带html,防止垃圾评论在主题目录下的functions.php中加上过滤代码:(站根目录/wp-content/themes/twentyfifteen/functions.php,twentyfifteen是主题目录)

function ghz_comment_escape($incoming_comment) {
$incoming_comment = strip_tags($incoming_comment);
return ($incoming_comment);
}

2.禁掉所有文档的修订版本

add_filter( ‘wp_revisions_to_keep’, ‘specs_wp_revisions_to_keep’, 10, 2 );
function specs_wp_revisions_to_keep( $num, $post ) {
return 0;
}
add_filter(‘comment_text’, ‘ghz_comment_escape’);
add_filter(‘comment_text_rss’, ‘ghz_comment_escape’);

3.从wordpress网站中移除不必要的meta-data,比如版本号之类的在主题目录下的functions.php中加入如下代码

remove_action(‘wp_head’, ‘wp_generator’);
remove_action(‘wp_head’, ‘wlwmanifest_link’);
remove_action(‘wp_head’, ‘rsd_link’);

4.wordpress在进行登录时出现错误会给出详细信息,比如用户名不存在,若存在会提示密码不正确,这样不安全修改,在主题目录下的functions.php中加入如下代码

function failed_login() {
return “用户名错误或者密码错误”;
}
add_filter(‘login_errors’, ‘failed_login’);

自定义登陆界面logo的链接

function ghz_loginlogo_link($url)
{
return “http://www.iaccepted.net”;
}
add_filter(‘login_headerurl’, ‘ghz_loginlogo_link’);

更改登陆界面logo提示文字

functions ghz_loginlogo_title()
{
return “http://www.iaccepted.net”;
}
add_filter(‘login_headertitle’, ‘ghz_loginlogo_title’);

5.停用wordpress的链接猜测功能在主题目录下的functions.php中加入如下代码

function stop_guessing($url) {
if (is_404())return false;
return $url;
}
add_filter(‘redirect_canonical’, ‘stop_guessing’);

6.优化wordpress的固定链接结构,使得更有利于SEO1.首先要设置nginx具有rewrite功能,在nginx.conf的server下添加如下内容

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;
}

重新加载nginx ./nginx -s reload

2.在wordpress的控制面板里控制台的设定->固定链接设置来变更,选择自定义:

然后填写 /%category%/%postname%

现在在访问博客文章,地址就变了

7.去掉博客底部wordpress.org链接

修改当前主题下footer.php

<div class=”site-info”>
<php do_action( ‘twentytwelve_credits’ ); ?>
<a href=”<?php echo esc_url( __( ‘http://wordpress.org/’, ‘twentytwelve’ ) ); ?>” rel=”nofollow” title=”<?php esc_attr_e( ‘Semantic Personal Publishing Platform’, ‘twentytwelve’ ); ?>”><?php printf( __( ‘Proudly powered by %s’, ‘twentytwelve’ ), ‘WordPress’ ); ?></a>
</div><!– .site-info –>

这段标签代码删掉

8.去掉功能菜单中wordpress.org

修改wp-includes下面default-widgets.php

删掉

echo apply_filters( ‘widget_meta_poweredby’, sprintf(‘<li><a href=”%s” title=”%s”>%s</a></li>’,
esc_url(__(‘https://wordpress.org/’)),
esc_attr__(‘Powered by WordPress, state-of-the-art semantic personal publishing platform.’),
_x(‘WordPress.org’, ‘meta widget link text’)
));

9.去掉后台左上角关于wordpress在当前主题下的functions.php中添加

function remove_logo($wp_toolbar) {
$wp_toolbar->remove_node(‘wp-logo’); //去掉Wordpress LOGO
}
add_action(‘admin_bar_menu’, ‘remove_logo’, 999);

10.添加彩色云标签

在主题下的functions.php中添加如下代码

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’ => 45, //显示个数
‘format’ => ‘flat’,//列表格式,可以是flat、list或array
‘separator’ => “\n”, //分隔每一项的分隔符
‘orderby’ => ‘name’,//排序字段,可以是name或count
‘order’ => ‘ASC’, //升序或降序,ASC或DESC
‘exclude’ => null, //结果中排除某些标签
‘include’ => null, //结果中只包含这些标签
‘link’ => ‘view’, //taxonomy链接,view或edit
‘taxonomy’ => ‘post_tag’ //调用哪些分类法作为标签云
);
$return = array_merge( $args, $newargs);
return $return;
}
function colorCloud($text) {
$text = preg_replace_callback(‘|<a (.+?)>|i’,’colorCloudCallback’, $text);
return $text;
}
function colorCloudCallback($matches) {
$text = $matches[1];
$color = dechex(rand(0,16777215));
$pattern = ‘/style=(\’|\”)(.*)(\’|\”)/i’;
$text = preg_replace($pattern, “style=\”color:#{$color};$2;\””, $text);
return “<a $text>”;
}
add_filter(‘wp_tag_cloud’, ‘colorCloud’, 1);

12.解决wordpress加载google字体而拖慢速度的问题

在主题下的functions.php中找到Register Google fonts for Twentyfteen

下面的三个if中

if (‘off’ !== _x(‘on’, ‘

改成 if (‘off’ !== _x(‘of’, ‘

或者在主题的functions.php中直接添加

function remove_open_sans()
{
wp_deregister_style(‘open-sans’);
wp_register_style(‘open-sans’, false);
wp_enqueue_style(‘open-sans’, ”);
}
add_action(‘init’, ‘remove_open_sans’);

同时将wp-include/script-loader.php中搜索fonts.googleapis.com找到

$open_sans_font_url=”//fonts.googleapis.com/css?

替换为$open_sans_font_url=”//fonts.useso.com/css?

13.为文章中read more连接加上nofollow属性

在主题的functions.php中加入如下代码

function add_nofollow_to_link($link)
{
return str_replace(‘<a’, ‘<a rel=”nofollow”‘, $link);
}
add_filter(‘the_content_more_lin’, ‘add_nofollow_to_link’, 0);

14.首页摘要输出在主题中content.php中修改

<div class=”entry-content”>
<?php
/* translators: %s: Name of current post */
the_content( sprintf(
__( ‘Continue reading %s’, ‘twentyfifteen’ ),
the_title( ‘<span class=”screen-reader-text”>’, ‘</span>’, false )
) );
wp_link_pages( array(
‘before’ => ‘<div class=”page-links”><span class=”page-links-title”>’ . __( ‘Pages:’, ‘twentyfifteen’ ) . ‘</span>’,
‘after’ => ‘</div>’,
‘link_before’ => ‘<span>’,
‘link_after’ => ‘</span>’,
‘pagelink’ => ‘<span class=”screen-reader-text”>’ . __( ‘Page’, ‘twentyfifteen’ ) . ‘ </span>%’,
‘separator’ => ‘<span class=”screen-reader-text”>, </span>’,
) );
?>
</div><!– .entry-content-–>

为:

<div class=”entry-content”>
<?php
if ( is_single() ) :
/* translators: %s: Name of current post */
the_content( sprintf(
__( ‘Continue reading %s’, ‘twentyfifteen’ ),
the_title( ‘<span class=”screen-reader-text”>’, ‘</span>’, false )
) );
wp_link_pages( array(
‘before’ => ‘<div class=”page-links”><span class=”page-links-title”>’ . __( ‘Pages:’, ‘twentyfifteen’ ) . ‘</span>’,
‘after’ => ‘</div>’,
‘link_before’ => ‘<span>’,
‘link_after’ => ‘</span>’,
‘pagelink’ => ‘<span class=”screen-reader-text”>’ . __( ‘Page’, ‘twentyfifteen’ ) . ‘ </span>%’,
‘separator’ => ‘<span class=”screen-reader-text”>, </span>’,
) );
else :
/* translators: %s: Name of current post */
the_excerpt( sprintf(
__( ‘Continue reading %s’, ‘twentyfifteen’ ),
the_title( ‘<span class=”screen-reader-text”>’, ‘</span>’, false )
) );
wp_link_pages( array(
‘before’ => ‘<div class=”page-links”><span class=”page-links-title”>’ . __( ‘Pages:’, ‘twentyfifteen’ ) . ‘</span>’,
‘after’ => ‘</div>’,
‘link_before’ => ‘<span>’,
‘link_after’ => ‘</span>’,
‘pagelink’ => ‘<span class=”screen-reader-text”>’ . __( ‘Page’, ‘twentyfifteen’ ) . ‘ </span>%’,
‘separator’ => ‘<span class=”screen-reader-text”>, </span>’,
) );
endif;
?>
</div><!– .entry-content-–>

15.设置摘要字数

function twenty_fifteen_excerpt_length( $length ) {
return 200;
}
add_filter( ‘excerpt_length’, ‘twenty_fifteen_excerpt_length’, 999 );

关注微信公众号

上一篇文章
霸屏广告真相
下一篇文章
最全google找客户方法
菜单