已经有很长时间我没有发布新的列表、超级有用的代码片段和技巧来加强你的WordPress主题或安装程序了。所以,今天我很高兴的向你展示这个漂亮的编码技巧列表,让你的WordPress博客更加强大。

不用插件追踪PostViews
有一些插件你能帮助你追踪PostViews,但实际上你真的不需要用插件来做。
这组函数需要粘帖到你的主题文件functions.php中:
function getPostViews($postID){
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
return "0 View";
}
return $count.' Views';
}
function setPostViews($postID) {
$count_key = 'post_views_count';
$count = get_post_meta($postID, $count_key, true);
if($count==''){
$count = 0;
delete_post_meta($postID, $count_key);
add_post_meta($postID, $count_key, '0');
}else{
$count++;
update_post_meta($postID, $count_key, $count);
}
}
// Remove issues with prefetching adding extra views
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);
一旦做完,你需要告诉WordPress来追踪视图使用上述函数,粘贴线下部分到single.php文件中, 就在循环里。
setPostViews(get_the_ID());
要显示视图,要用到循环里面的这个函数:
echo getPostViews(get_the_ID());
源: WP Snipp
获取用户最后登录日期
一些基于WordPress建设的网站允许用户注册。 如果你的网站允许用户注册,你可能想知道某个用户最后登录的时间。
你首先需要在functions.php文件中添加如下代码:
// set the last login date
add_action('wp_login','wpsnipp_set_last_login', 0, 2);
function wpsnipp_set_last_login($login, $user) {
$user = get_user_by('login',$login);
$time = current_time( 'timestamp' );
$last_login = get_user_meta( $user->ID, '_last_login', 'true' );
if(!$last_login){
update_usermeta( $user->ID, '_last_login', $time );
}else{
update_usermeta( $user->ID, '_last_login_prev', $last_login );
update_usermeta( $user->ID, '_last_login', $time );
}
}
// get last login date
function wpsnipp_get_last_login($user_id,$prev=null){
$last_login = get_user_meta($user_id);
$time = current_time( 'timestamp' );
if(isset($last_login['_last_login_prev'][0]) && $prev){
$last_login = get_user_meta($user_id, '_last_login_prev', 'true' );
}else if(isset($last_login['_last_login'][0])){
$last_login = get_user_meta($user_id, '_last_login', 'true' );
}else{
update_usermeta( $user_id, '_last_login', $time );
$last_login = $last_login['_last_login'][0];
}
return $last_login;
}
下面是显示用户最后登录时间的代码:
// show last login date
global $current_user;
get_currentuserinfo();
echo '<p>Previous: Login date: ' . date("Y-m-d h:m:s", wpsnipp_get_last_login($current_user->ID,true)) . '</p>';
echo '<p>Current: Login date: ' . date("Y-m-d h:m:s", wpsnipp_get_last_login($current_user->ID)) . '</p>';
来源: WP Snipp
自动将JavaScript文件移到底部
为了提高你的WordPress博客的加载速度,建议你把JavaScript文件移动到你HTML问道的最底下。不幸的是,这看起来并不那么容易,尤其是你使用一些需要将代码插入到文件头的插件时。这提供了一个简单的解决方案来自动将JavaScript文件移动到文件底部。
首先,打开你的functions.php文件然后将下面的代码黏贴上:
/**
* Filter HTML code and leave allowed/disallowed tags only
*
* @param string $text Input HTML code.
* @param string $tags Filtered tags.
* @param bool $invert Define whether should leave or remove tags.
* @return string Filtered tags
*/
function theme_strip_tags_content($text, $tags = '', $invert = false) {
preg_match_all( '/<(.+?)[\s]*\/?[\s]*>/si', trim( $tags ), $tags );
$tags = array_unique( $tags[1] );
if ( is_array( $tags ) AND count( $tags ) > 0 ) {
if ( false == $invert ) {
return preg_replace( '@<(?!(?:'. implode( '|', $tags ) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text );
}
else {
return preg_replace( '@<('. implode( '|', $tags ) .')\b.*?>.*?</\1>@si', '', $text );
}
}
elseif ( false == $invert ) {
return preg_replace( '@<(\w+)\b.*?>.*?</\1>@si', '', $text );
}
return $text;
}
/**
* Generate script tags from given source code
*
* @param string $source HTML code.
* @return string Filtered HTML code with script tags only
*/
function theme_insert_js($source) {
$out = '';
$fragment = new DOMDocument();
$fragment->loadHTML( $source );
$xp = new DOMXPath( $fragment );
$result = $xp->query( '//script' );
$scripts = array();
$scripts_src = array();
foreach ( $result as $key => $el ) {
$src = $result->item( $key )->attributes->getNamedItem( 'src' )->value;
if ( ! empty( $src ) ) {
$scripts_src[] = $src;
} else {
$type = $result->item( $key )->attributes->getNamedItem( 'type' )->value;
if ( empty( $type ) ) {
$type = 'text/javascript';
}
$scripts[$type][] = $el->nodeValue;
}
}
//used by inline code and rich snippets type like application/ld+json
foreach ( $scripts as $key => $value ) {
$out .= '<script type=" '.$key.' ">';
foreach ( $value as $keyC => $valueC ) {
$out .= "\n ".$valueC;
}
$out .= '</script>';
}
//external script
foreach ( $scripts_src as $value ) {
$out .= '<script src=" '.$value.' "></script>';
}
return $out;
}
做完后,编辑你的header.php文件。用下面的代码替换wp_head()标记:
<?php
ob_start();
wp_head();
$themeHead = ob_get_contents();
ob_end_clean();
define( 'HEAD_CONTENT', $themeHead );
$allowedTags = '<style><link><meta><title>';
print theme_strip_tags_content( HEAD_CONTENT, $allowedTags );
?>
最后,将下面的代码贴到你的footer.php文件中,在</body>标记关闭之前。
<?php theme_insert_js( HEAD_CONTENT ); ?>
源: WPRecipes
在旧文章上显示一段免责声明
如果你的博客是关于技术的,有些文章可能会在5年前有用,但当下可能就完全过时了。如果文章很旧了,你需要一个好东西来提醒你的读者他们现在读的信息可能已经过时了。
黏贴下面的代码到你的single.php文件中, 循环找到第7行编辑成你需要的文本。
<?
$ageunix = get_the_time('U');
$days_old_in_seconds = ((time() - $ageunix));
$days_old = (($days_old_in_seconds/86400));
if ($days_old > 365) {
echo '<div class="disclaimer ">DISCLAIMER: this post is older than one year and may not be up to date with latest WordPress version.</div>';
}
?>
源: WPRecipes
用默认内容预先填充编辑器
如果你经常用一个特定的布局发布列表、评论或其它类型的文章,这个技巧能帮你节省大量的时间。因为它预先用你选择的默认内容填充到你的WordPress编辑器,并支持各种文章类型。
将下面的代码复制到functions.php文件中。 改变$content的值然后保存,就完事了。
add_filter( 'default_content', 'pu_default_editor_content' );
function pu_default_editor_content( $content ) {
global $post_type;
switch( $post_type )
{
case 'post':
$content = 'Default content for blog posts.';
break;
case 'page':
$content = 'Default content for pages.';
break;
case 'portfolio':
$content = 'Default content for your portfolio pages.';
break;
case 'products':
$content = 'Default content for blog posts.';
break;
}
return $content;
}
源: Paulund
希望这些对你有帮助!