WordPress开发者必知的10个代码

2014/02/22
WordPress开发者必知的10个代码

对于Wordpress开发者来说,熟知一些必要的代码是非常必要的。如果您也是一个开发者,那么下面的这十个必知的代码赶快收藏吧。对您以后的开发将是非常有用的。

一、让JPG图像质量更好

WordPress会自动把JPG图像质量压缩到90。如果您的网站对图片质量要求高的话,那么可以通过下面的函数把JPG质量调到100.

[php]
add_filter( ‘jpg_quality’, ‘high_jpg_quality’ );
function high_jpg_quality() {
return 100;
}
[/php]

二、适当的URLs

使用 esc_url()函数您的URLs形成适当的形式和具有有效的字符。

[php]
$my_url = ‘http://myawesomesite.com/?awesome=true’;
$url = esc_url( $my_url );
[/php]

三、让文本小工具支持简码

现在的主题很多提供了简码,为了让小工具支持简码,需要添加下面的代码:

[php]
add_filter( ‘widget_text’, ‘do_shortcode’ );
[/php]

四、延迟RSS发布

WordPress如何避免偶然性发布文章一文中介绍了如何在发布文章之前确认发布文章,以避免不成熟或错误的文章发布,下面的代码让你的RSS延迟发布,有异曲同工之妙。

[php]
function publish_later_on_feed($where) {
global $wpdb;
if ( is_feed() ) {
$time_now = gmdate(‘Y-m-d H:i:s’);
$time_delay = ’15’; // integer
$time_span = ‘MINUTE’; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR
$where = " AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, ‘$time_now’) > $time_delay ";
}
return $where;
}
add_filter(‘posts_where’, ‘publish_later_on_feed’);
[/php]

五、在RSS feed中显示特色图像

图片胜过千言万语,在RSS feed中显示特色图像无疑会增加文章的可读性。

[php]
add_filter(‘the_content_feed’, ‘rss_post_thumbnail’);
function rss_post_thumbnail($content) {
global $post;
if( has_post_thumbnail($post->ID) )
$content = ‘<p>’ . get_the_post_thumbnail($post->ID, ‘thumbnail’) . ‘</p>’ . $content;
return $content;
}
[/php]

六、关闭评论中的HTML支持

您可以通过关闭评论中的HTML支持来过滤垃圾评论链接。

[php]
// This will occur when the comment is posted
function plc_comment_post( $incoming_comment ) {
// convert everything in a comment to display literally
$incoming_comment[‘comment_content’] = htmlspecialchars($incoming_comment[‘comment_content’]);
// the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
$incoming_comment[‘comment_content’] = str_replace( "’", ‘'’, $incoming_comment[‘comment_content’] );
return( $incoming_comment );
}
// This will occur before a comment is displayed
function plc_comment_display( $comment_to_display ) {
// Put the single quotes back in
$comment_to_display = str_replace( ‘'’, "’", $comment_to_display );
return $comment_to_display;
}
add_filter( ‘preprocess_comment’, ‘plc_comment_post’, ”, 1 );
add_filter( ‘comment_text’, ‘plc_comment_display’, ”, 1 );
add_filter( ‘comment_text_rss’, ‘plc_comment_display’, ”, 1 );
add_filter( ‘comment_excerpt’, ‘plc_comment_display’, ”, 1 );
// This stops WordPress from trying to automatically make hyperlinks on text:
remove_filter( ‘comment_text’, ‘make_clickable’, 9 );
[/php]

在wordpress登录界面里,会显示wordpress的图标,如果你需要修改,则使用下面的代码。

[php]
add_action(‘admin_head’, ‘custom_logo’);

function custom_logo() {
echo ‘

<style type="text/css"><!–
#header-logo { background-image: url(‘.get_bloginfo(‘template_directory’).’/images/custom-logo.gif) !important; }
–></style>’;
[/php]

八、移除wordpress版本说明

这个可以增加wordpress的安全性。

[php]
<?php
// Remove the WP version for extra WordPress Security
function remove_wp_version(){
return ”;
}
add_filter(‘the_generator’, ‘remove_wp_version’);
?>
[/php]

九、自动版权日期

许多网站都具有过期的版权日期,这个我们往往容易忽略。现在我们使用一个函数,来自动更新。

[php]
function comicpress_copyright() {
global $wpdb;
$copyright_dates = $wpdb->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
post_status = ‘publish’
");
$output = ”;
if($copyright_dates) {
$copyright = "© " . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= ‘-‘ . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}
[/php]

然后调用函数:

[php]
><?php echo comicpress_copyright(); ?>
[/php]

十、登录重定向到指定的地址

使用下面的代码实现:

[php]
<?php function redirect_user_on_role() { //retrieve current user info global $current_user; get_currentuserinfo(); //If login user role is Subscriber if ($current_user->user_level == 0)
{
wp_redirect( home_url() ); exit;
}
//If login user role is Contributor
else if ($current_user->user_level > 1)
{
wp_redirect( home_url() ); exit;
}
//If login user role is Editor
else if ($current_user->user_level >8)
{
wp_redirect( home_url() ); exit;
}
// For other rolse
else
{
$redirect_to = ‘https://www.dianjin123.com/’;
return $redirect_to;
}
}
add_action(‘admin_init’,’redirect_user_on_role’);

?>
[/php]

5 thoughts on “WordPress开发者必知的10个代码

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注