WordPress默认的设置不一定符合每个主题,特别是缩略图尺寸;很多客户对如何设置WordPress不甚明了,如果在主题启用时直接完成WordPress设置,对客户或许会比较友好,也免去解释的麻烦。
主题启用时设置WordPress
主题通常要用到特色图像(thumbnail),特色图像的尺寸可以在媒体中设置,媒体中还有其他一些尺寸的图片。一般来讲,thumbnail尺寸应该符合主题的需求,medium size的宽度则可以设置成比文章内容区域稍小或者相等,如果客户插入图片时,找不到一个合适的尺寸,一定很烦恼。另外,WordPress要为每个尺寸产生副本,把没用的尺寸设置为0,让有用的尺寸设置的符合主题需求,也能减少图片数量。
除此之外,我们还可以设置其他选项,选项名称可以在wp_options表中找到。
将下面的代码放到functions.php中,即可在主题启用时设置WordPress。
<?php
/**
* Change default settings on theme activation
*/
add_action( 'after_setup_theme', 'cp_theme_settings' );
function cp_theme_settings()
{
// 首先检测是否曾经设置过主题选项
$the_theme_status = get_option( 'cp_theme_setup_status' );
//如果还没设置过,运行程序更新选项
if ( $the_theme_status !== '1' ) {
// Setup Default WordPress settings
$core_settings = array(
'avatar_default' => 'mystery', // Comment Avatars使用mystery
'avatar_rating' => 'G', // Avatar等级为G
'comment_max_links' => 1, // 设置允许评论中有多少链接
'comments_per_page' => 25, // Default to 20 comments per page
'thumbnail_size_w' => 150, // 设置thumbnail的默认宽度
'thumbnail_size_h' => 150, //设置thumbnail的默认高度
'medium_size_w' => 590, // 设置中等尺寸的默认宽度
'medium_size_h' => 700, //设置中等尺寸的默认高度
'embed_size_w' => 590 //设置embed标签的默认宽度
);
//根据上面的设置更新WordPress设定
foreach ( $core_settings as $k => $v ) {
update_option( $k, $v );
}
// 删除默认的文章hello world和评论(对新安装的wordpress可以开启这一项)
wp_delete_post( 1, true );
wp_delete_post( 2, true );
wp_delete_comment( 1 );
// 设置完成,更新theme_status,告诉程序不要再重复设置
update_option( 'cp_theme_setup_status', '1' );
// 通知管理员设置已被更改
$msg = '
<div class="error">
<p>The ' . get_option( 'current_theme' ) . ' theme has changed your WordPress default <a href="' . admin_url() . 'options-general.php" title="See Settings">settings</a> and deleted default posts & comments.</p>
</div>';
add_action( 'admin_notices', $c = create_function( '', 'echo "' . addcslashes( $msg, '"' ) . '";' ) );
}
//再次启用主题时,显示消息告知主题再次启用,不更新选项。
elseif ( $the_theme_status === '1' and isset( $_GET['activated'] ) ) {
$msg = '
<div class="updated">
<p>The ' . get_option( 'current_theme' ) . ' theme was successfully re-activated.</p>
</div>';
add_action( 'admin_notices', $c = create_function( '', 'echo "' . addcslashes( $msg, '"' ) . '";' ) );
}
}
?>
应该是少了什么符号,粘贴后,大部分内容都变成注释了。
就开头有*注释,如果那里没错应该不会出现大部分变成注释的情况,我copy了下没发现啥问题啊