feat: add keywords and description setting (#204)

pull/233/head
Seaton Jiang 2020-06-13 13:32:56 +08:00
parent 38fe6e33ad
commit 5cd97255aa
4 changed files with 107 additions and 50 deletions

View File

@ -3,7 +3,7 @@
* 主题页眉
* @author Seaton Jiang <seaton@vtrois.com>
* @license MIT License
* @version 2020.04.12
* @version 2020.06.13
*/
?>
<!DOCTYPE html>
@ -14,8 +14,8 @@
<title><?php wp_title( '-', true, 'right' ); ?></title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="format-detection" content="telphone=no, email=no">
<meta name="keywords" content="<?php keywords(); ?>">
<meta name="description" itemprop="description" content="<?php description(); ?>">
<meta name="keywords" content="<?php echo keywords(); ?>">
<meta name="description" itemprop="description" content="<?php echo description(); ?>">
<meta name="theme-color" content="<?php echo kratos_option('g_chrome', '#282a2c'); ?>">
<meta itemprop="image" content="<?php echo share_thumbnail_url(); ?>"/>
<link rel="shortcut icon" href="<?php echo kratos_option('g_icon'); ?>">

View File

@ -382,4 +382,60 @@ function recover_comment_fields($comment_fields){
$comment_fields = array_merge($comment_fields ,array('comment' => $comment));
return $comment_fields;
}
add_filter('comment_form_fields','recover_comment_fields');
add_filter('comment_form_fields','recover_comment_fields');
$new_meta_boxes =
array(
"description" => array(
"name" => "seo_description",
"std" => "",
"title" => __( '描述', 'kratos' )
),
"keywords" => array(
"name" => "seo_keywords",
"std" => "",
"title" => __( '关键词', 'kratos' )
)
);
function seo_meta_boxes() {
$post_types = get_post_types();
add_meta_box( 'meta-box-id', __( 'SEO 设置', 'kratos' ), 'post_seo_callback', $post_types );
}
add_action( 'add_meta_boxes', 'seo_meta_boxes' );
function post_seo_callback( $post ) {
global $new_meta_boxes;
foreach($new_meta_boxes as $meta_box) {
$meta_box_value = get_post_meta($post->ID, $meta_box['name'].'_value', true);
if($meta_box_value == "")
$meta_box_value = $meta_box['std'];
echo '<h3 style="font-size: 14px; padding: 9px 0; margin: 0; line-height: 1.4;">'.$meta_box['title'].'</h3>';
echo '<textarea cols="60" rows="3" style="width:100%" name="'.$meta_box['name'].'_value">'.$meta_box_value.'</textarea><br/>';
}
echo '<input type="hidden" name="metaboxes_nonce" id="metaboxes_nonce" value="'.wp_create_nonce( plugin_basename(__FILE__) ).'" />';
}
function wpdocs_save_meta_box( $post_id ) {
global $new_meta_boxes;
if ( !wp_verify_nonce( $_POST['metaboxes_nonce'], plugin_basename(__FILE__) ))
return;
if ( !current_user_can( 'edit_posts', $post_id ))
return;
foreach($new_meta_boxes as $meta_box) {
$data = $_POST[$meta_box['name'].'_value'];
if($data == "")
delete_post_meta($post_id, $meta_box['name'].'_value', get_post_meta($post_id, $meta_box['name'].'_value', true));
else
update_post_meta($post_id, $meta_box['name'].'_value', $data);
}
}
add_action( 'save_post', 'wpdocs_save_meta_box' );

View File

@ -3,7 +3,7 @@
* 站点相关函数
* @author Seaton Jiang <seaton@vtrois.com>
* @license MIT License
* @version 2020.04.12
* @version 2020.06.13
*/
// 标题配置
@ -28,61 +28,54 @@ add_filter('wp_title', 'title', 10, 2);
// Keywords 配置
function keywords()
{
if (is_home() || is_front_page()) {
echo kratos_option('seo_keywords');
} elseif (is_category()) {
echo kratos_option('seo_keywords') . ',';
single_cat_title();
global $post;
if (is_home()) {
$keywords = kratos_option('seo_keywords');
} elseif (is_single()) {
echo trim(wp_title('', false)) . ',';
echo kratos_option('seo_keywords') . ',';
if (has_tag()) {
foreach (get_the_tags() as $tag) {
echo $tag->name . ',';
$keywords = get_post_meta($post->ID, "seo_keywords_value", true);
if($keywords == '') {
$tags = wp_get_post_tags($post->ID);
foreach ($tags as $tag ) {
$keywords = $keywords . $tag->name . ", ";
}
$keywords = rtrim($keywords, ', ');
}
foreach (get_the_category() as $category) {
echo $category->cat_name . ',';
} elseif (is_page()) {
$keywords = get_post_meta($post->ID, "seo_keywords_value", true);
if($keywords == '') {
$keywords = kratos_option('seo_keywords');
}
} elseif (is_search()) {
echo kratos_option('seo_keywords') . ',';
the_search_query();
} else {
echo kratos_option('seo_keywords') . ',';
echo trim(wp_title('', false));
$keywords = single_tag_title('', false);
}
return trim(strip_tags($keywords));
}
// Description 配置
function description()
{
if (is_home() || is_front_page()) {
echo trim(kratos_option('seo_description'));
} elseif (is_category()) {
$description = strip_tags(category_description());
echo trim($description);
global $post;
if (is_home()) {
$description = kratos_option('seo_description');
} elseif (is_single()) {
if (get_the_excerpt()) {
echo get_the_excerpt();
} else {
global $post;
$description = trim(str_replace(array("\r\n", "\r", "\n", " ", " "), " ", str_replace("\"", "'", strip_tags($post->post_content))));
echo mb_substr($description, 0, 220, 'utf-8');
$description = get_post_meta($post->ID, "seo_description_value", true);
if ($description == '') {
$description = get_the_excerpt();
}
} elseif (is_search()) {
echo '「';
the_search_query();
echo '」共找到 ';
global $wp_query;
echo $wp_query->found_posts;
echo ' 个记录';
if ($description == '') {
$description = str_replace("\n","",mb_strimwidth(strip_tags($post->post_content), 0, 200, "", 'utf-8'));
}
} elseif (is_category()) {
$description = category_description();
} elseif (is_tag()) {
$description = strip_tags(tag_description());
echo trim($description);
} else {
$description = strip_tags(term_description());
echo trim($description);
$description = tag_description();
} elseif (is_page()) {
$description = get_post_meta($post->ID, "seo_description_value", true);
if ($description == '') {
$description = kratos_option('seo_description');
}
}
return trim(strip_tags($description));
}
// robots.txt 配置

View File

@ -3,7 +3,7 @@ msgid ""
msgstr ""
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
"Project-Id-Version: Kratos\n"
"POT-Creation-Date: 2020-06-08 14:23+0800\n"
"POT-Creation-Date: 2020-06-13 13:30+0800\n"
"PO-Revision-Date: 2020-02-14 23:32+0800\n"
"Last-Translator: \n"
"Language-Team: \n"
@ -233,6 +233,18 @@ msgstr ""
msgid "添加表情"
msgstr ""
#: inc/theme-article.php:392
msgid "描述"
msgstr ""
#: inc/theme-article.php:397 inc/theme-options.php:218
msgid "关键词"
msgstr ""
#: inc/theme-article.php:403
msgid "SEO 设置"
msgstr ""
#: inc/theme-core.php:67
msgid "您已经赞过了"
msgstr ""
@ -441,10 +453,6 @@ msgstr ""
msgid "搜索引擎或者社交工具分享首页时抓取的图片"
msgstr ""
#: inc/theme-options.php:218
msgid "关键词"
msgstr ""
#: inc/theme-options.php:219
msgid "每个关键词之间需要用「英文逗号」分割"
msgstr ""