2013 最实用的Wordpress技巧、代码片段
2013年的Wordpress发展很快。在各种论坛中有很多优秀的代码片段。我这里将我喜欢的一些技巧和片段整理出来,送给所有的Wordpress开发者;
使用剪裁而不是缩放来处理上传的图片
你是否想过,对你在Wordpress上传的图片进行剪裁成合适大小,而不是将他们缩放为合适的大小,如果想这么做,那么就看看下面的代码,你就能做到
将代码片段加入functions.php这个文件中:
// Standard Size Thumbnail
if(false === get_option("thumbnail_crop")) {
add_option("thumbnail_crop", "1"); }
else {
update_option("thumbnail_crop", "1");
}</p>
<p>// Medium Size Thumbnail
if(false === get_option("medium_crop")) {
add_option("medium_crop", "1"); }
else {
update_option("medium_crop", "1");
}</p>
<p>// Large Size Thumbnail
if(false === get_option("large_crop")) {
add_option("large_crop", "1"); }
else {
update_option("large_crop", "1");
}
来源:[weblink url="http://wp-snippet.com/snippets/activate-cropping-for-all-thumbnail-sizes/"]cropping-for-all-thumbnail[/weblink] ##在Wordpress 自动链接twitter用户名
将代码片段加入functions.php这个文件中:
function twtreplace($content) {
$twtreplace = preg_replace('/([^a-zA-Z0-9-<em>&])@([0-9a-zA-Z</em>]+)/',"$1<a href=\\"http://twitter.com/$2\\" target=\\"_blank\\" rel=\\"nofollow\\">@$2</a>",$content);
return $twtreplace;
}</p>
<p>add_filter('the_content', 'twtreplace'); <br />
add_filter('comment_text', 'twtreplace');
来源: [weblink url="http://snipplr.com/view/70977/automatically-link-twitter-usernames-in-wordpress/"]automatically-link-twitter[/weblink]
不使用插件清理wp_head()
WordPress的通过wp_head()增添了不少的东西.很多主题也会与这个函数挂钩。但有些东西是完全用不到的。这里有一个简单的清理方法,可以清理无用的内容。 将代码片段加入functions.php这个文件中:
remove_action( 'wp_head', 'rsd_link' );
remove_action( 'wp_head', 'wlwmanifest_link' );
remove_action( 'wp_head', 'wp_generator' );
remove_action( 'wp_head', 'start_post_rel_link' );
remove_action( 'wp_head', 'index_rel_link' );
remove_action( 'wp_head', 'adjacent_posts_rel_link' );
remove_action( 'wp_head', 'wp_shortlink_wp_head' );
来源:[weblink url="http://www.themelab.com/remove-code-wordpress-header/"]remove-code-wordpress-header[/weblink]
强制页面使用ssl链接
当你服务器上面启用了ssl,你可以通过以下代码来指定某个页面通过ssl来访问; 你需要做的就是将代码片段加入functions.php这个文件中,并指定特殊页面的id:
function wps_force_ssl( $force_ssl, $post_id = 0, $url = '' ) {
if ( $post_id == 25 ) {
return true
}
return $force_ssl;
}
add_filter('force_ssl' , 'wps_force_ssl', 10, 3);
来源:[weblink url="http://wpsnipp.com/index.php/functions-php/force-specific-pages-to-be-secure-ssl-https/"]specific-pages-https[/weblink]
在Wordpress之外循环访问页面
在WP之外,你需要访问文章信息,这里是一段代码,帮助你在wp之外的其他php文件上访问文章信息 将以下代码放在你需要访问的php页面下。并修改以下地方: line 4: 将你 WordPress wp-blog-header.php 文件正确路径填入. line 5: 通过 query_posts() 获取所需信息.
<?php
// Include WordPress
define('WP_USE_THEMES', false);
require('/server/path/to/your/wordpress/site/htdocs/blog/wp-blog-header.php');
query_posts('posts_per_page=1');
?></p>
<?php while (have_posts()): the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php the_excerpt(); ?>
<p><a href="<?php the_permalink(); ?>" class="red">Read more...</a></p>
<?php endwhile; ?>
来源:[weblink url="http://css-tricks.com/snippets/wordpress/run-a-loop-outside-of-wordpress/"]loop-outside-of-wordpresss[/weblink]