Learn how to introduce policy features in WordPress without using a plugin.
Plug-in non-use has various advantages, such as reducing server load and maintaining site display speed.
There are many diverse plugins for WordPress, and for SEO, there are excellent plugins such as All in One SEO Pack and Yoast SEO.
However, SEO plug-ins are particularly heavy.
If you are aiming for high search engine rankings by enriching your content, but people leave your website because of slow display speeds.There is nothing you can do about it.
So we will introduce a “light”, “fast” and “easy to install” implementation method.
//Setting up SEO custom fields.
class Custom_Field_4536 {
function __construct() {
add_filter( 'document_title_parts', [ $this, 'title_update' ] );
if(!is_admin()) return;
add_action( 'add_meta_boxes', [ $this, 'init' ] );
add_action( 'transition_post_status', [ $this, 'save' ], 10, 3 );
}
function init() {
$list = [
'SEO対策' => 'seo_custom_fields',
];
$args = [
'public' => true,
'_builtin' => false,
];
$post_types = get_post_types( $args, 'names' );
foreach($list as $title => $id) {
add_meta_box( $id, $title, $id, 'post', 'normal', 'high');
add_meta_box( $id, $title, $id, 'page', 'normal', 'high');
foreach ( $post_types as $post_type ) {
add_meta_box( $id, $title, $id, $post_type, 'normal', 'high');
}
}
}
function save($new_status, $old_status, $post) {
if (($old_status == 'auto-draft'
|| $old_status == 'draft'
|| $old_status == 'pending'
|| $old_status == 'future')
&& $new_status == 'publish') {
return $post;
} else {
add_action('save_post', function($post_id) {
$list = [
'keywords',
'description',
'noindex',
'nofollow',
'sns_title',
'seo_title',
];
foreach($list as $name) {
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return $post_id;
if(isset($_POST['action']) && $_POST['action'] == 'inline-save') return $post_id;
if(!empty($_POST[$name]))
update_post_meta($post_id, $name, $_POST[$name] );
else delete_post_meta($post_id, $name);
}
});
}
}
function title_update($title) {
global $post;
$seo_title = get_post_meta($post->ID,'seo_title',true);
if(is_singular() && $seo_title) $title['title'] = $seo_title;
return $title;
}
}
new Custom_Field_4536();
// Creation of configuration items.
function seo_custom_fields() {
global $post;
$list = [
'seo_title' => 'Titles for SEO (title tag rewrite).',
'sns_title' => 'Titles for social networking.',
];
foreach($list as $name => $description) { ?>
<div class="input-wrapper-4536">
<div id="<?php echo $name; ?>-counter" class="counter"></div>
<label for="<?php echo $name; ?>" class="label-4536"><?php echo $description; ?></label>
<input name="<?php echo $name; ?>" id="<?php echo $name; ?>" value="<?php echo esc_html(get_post_meta($post->ID, $name, true)); ?>" size="60" class="input-4536" type="text" />
<div style="clear:both;"></div>
</div>
<?php } ?>
<div class="input-wrapper-4536">
<div id="description-counter" class="counter"></div>
<label for="description" class="label-4536">Page description.(Recommended: about 160 characters.)※If nothing is entered, the first character is automatically used.</label>
<textarea name="description" id="description" cols="60" rows="6" class="input-4536"><?php echo esc_html(get_post_meta($post->ID, 'description', true)); ?></textarea>
<div style="clear:both;"></div>
</div>
<?php $list = [
'keywords' => 'Keywords (comma-separated).',
];
foreach($list as $name => $description) { ?>
<div class="input-wrapper-4536">
<label for="<?php echo $name; ?>" class="label-4536"><?php echo $description; ?></label>
<input name="<?php echo $name; ?>" id="<?php echo $name; ?>" value="<?php echo esc_html(get_post_meta($post->ID, $name, true)); ?>" size="60" class="input-4536" type="text" />
<div style="clear:both;"></div>
</div>
<?php }
$list = [
'noindex' => 'NOINDEX(Blocks the display in search results.)',
'nofollow' => 'NOFOLLOW(Exclude links.)In most cases, this does not need to be checked.',
];
foreach($list as $name => $description) {
$check = (get_post_meta($post->ID, $name ,true) == 1) ? 'checked' : '/' ; ?>
<div class="input-wrapper-4536">
<label for="<?php echo $name; ?>" class="label-4536"><?php echo $description; ?></label>
<span class="checkbox-4536">
<input type="checkbox" name="<?php echo $name; ?>" id="<?php echo $name; ?>" value="1" <?php echo $check; ?> >
</span>
<div style="clear:both;"></div>
</div>
<?php } ?>
<style>
.input-wrapper-4536 {
margin: 20px 0;
position: relative;
}
.label-4536 {
float: left;
margin: 0;
width: 37%;
padding: 3px 30px 3px 0;
-webkit-box-sizing: border-box;
box-sizing: border-box;
}
.input-4536 {
float: left;
width: 63%;
margin: 0;
}
.checkbox-4536 {
float: left;
margin-top: 3px;
}
.counter {
text-align: right;
width: 100%;
}
.title-counter-length-over {
color: #f00;
font-weight: bold;
}
</style>
<?php }
// Reflecting settings.
add_action( 'wp_head', function() {
global $post;
$keywords = get_post_meta($post->ID,'keywords',true);
$noindex = get_post_meta($post->ID,'noindex',true);
$nofollow = get_post_meta($post->ID,'nofollow',true);
$robots = '';
if($noindex && $nofollow) $robots = '<meta name="robots" content="noindex,nofollow">';
if($noindex && !$nofollow) $robots = '<meta name="robots" content="noindex,follow">';
if(!$noindex && $nofollow) $robots = '<meta name="robots" content="nofollow">';
if(description_4536()) echo '<meta name="description" content="'.description_4536().'">';
if(is_singular()) { //Article page.
echo $robots;
if($keywords) echo '<meta name="keywords" content="'.$keywords.'">';
}
});
// character counter.
function text_counter_4536() { ?>
<script type="text/javascript">
var list = [
'#seo_title',
'#description',
'#sns_title'
];
$(function() {
$.each(list, function(index,value) {
function counter_4536() {
var length = $(value).val().length;
$(value + '-counter').html(length);
}
counter_4536();
$(value).on('keydown keyup keypress change', counter_4536);
});
});
</script>
<?php }
add_action( 'admin_head-post.php', 'text_counter_4536' );
add_action( 'admin_head-post-new.php', 'text_counter_4536' );
//Discretionary settings.
function description_4536() {
if(is_home()) {
$description = get_bloginfo('description') ;
} elseif(is_singular() || is_front_page()) { // Article page.
//Descriptions set in custom fields.
$custom_description = get_post_meta(get_the_ID(), 'description', true);
$custom_description = strip_tags(str_replace(array("\r\n", "\r", "\n"), '', $custom_description));
$custom_description = mb_strimwidth($custom_description, 0, 320, "...", "utf-8");
$auto_description = get_post(get_the_ID())->post_content;
$auto_description = strip_tags(str_replace(array("\r\n", "\r", "\n"), '', $auto_description));
$auto_description = mb_strimwidth($auto_description, 0, 320, "...", "utf-8");
//Change the description to be read according to the conditions.
$post_description = ($custom_description) ? $custom_description : $auto_description ;
$description = $post_description;
} elseif(is_category()) { // Category page.
if(term_description()) { //If you are entering a category description.
$description = term_description();
} else { //In the absence of a category description.
$description = single_cat_title('', false).'Article listings in.';
}
} elseif(is_tag()) { // Tag Page.
if(term_description()) { //If you have entered a tag description.
$description = term_description();
} else { //If there is no tag description.
$description = single_tag_title('', false).'Article listings in.';
}
} elseif(is_day()) {
$description = get_the_time('Y年m月d日').'Article listings in.';
} elseif(is_month()) {
$description = get_the_time('Y年m月').'Article listings in.';
} elseif(is_year()) {
$description = get_the_time('Y年').'Article listings in.';
} elseif(is_author()) {
$description = get_queried_object()->display_name.'Article listings in.';
}
if($description) return esc_html($description);
}
// Add item to user settings.
add_filter('user_contactmethods', function($sns) {
$sns['twitter'] = 'Twitter(twitter.com/since)';
$sns['fb_app_id'] = 'Facebook App ID';
return $sns;
});
// Output OGP tag/Twitter card settings.
function my_meta_ogp() {
if( is_front_page() || is_home() || is_singular() ){
global $post;
$ogp_title = '';
$ogp_descr = '';
$ogp_url = '';
$ogp_img = '';
$insert = '';
if( is_singular() ) { //Articles & Fixed pages.
setup_postdata($post);
$ogp_title = $post->post_title;
$ogp_descr = mb_substr(get_the_excerpt(), 0, 100);
$ogp_url = get_permalink();
wp_reset_postdata();
} elseif ( is_front_page() || is_home() ) { //Top page.
$ogp_title = get_bloginfo('name');
$ogp_descr = get_bloginfo('description');
$ogp_url = home_url();
}
//og:type
$ogp_type = ( is_front_page() || is_home() ) ? 'website' : 'article';
//og:image
if ( is_singular() && has_post_thumbnail() ) {
$ps_thumb = wp_get_attachment_image_src( get_post_thumbnail_id(), 'full');
$ogp_img = $ps_thumb[0];
} else {
$ogp_img = 'https://daikici.com/wp-content/uploads/daikici_og.png';
}
//Summarise the OGP tags to be output.
$insert .= '<meta property="og:title" content="'.esc_attr($ogp_title).'" />' . "\n";
$insert .= '<meta property="og:description" content="'.esc_attr($ogp_descr).'" />' . "\n";
$insert .= '<meta property="og:type" content="'.$ogp_type.'" />' . "\n";
$insert .= '<meta property="og:url" content="'.esc_url($ogp_url).'" />' . "\n";
$insert .= '<meta property="og:image" content="'.esc_url($ogp_img).'" />' . "\n";
$insert .= '<meta property="og:site_name" content="'.esc_attr(get_bloginfo('name')).'" />' . "\n";
$insert .= '<meta name="twitter:card" content="summary_large_image" />' . "\n";
$insert .= '<meta name="twitter:site" content="daikici" />' . "\n";
$insert .= '<meta property="og:locale" content="ja_JP" />' . "\n";
//Facebook app_id (if set)
$insert .= '<meta property="fb:app_id" content="226024900000000">' . "\n";
//If app_id is not set, delete up to this point.
echo $insert;
}
} //END my_meta_ogp
add_action('wp_head','my_meta_ogp');//OGP output to head.
Default image settings.
Line 252.
If no eye-catching image is set for an individual page, the image specified here will be displayed.
Change to any.
Twitter settings.
Line 262.
If you have a Twitter account associated with the site, please change anything after the @ symbol to anything you wish.
Facebook Settings.
Line 267.
Get a Facebook app ID and change it to any one you want.
Top page display settings.
Go to ‘Settings > General’ in the WordPress administration panel and enter a title and tagline for your site.
What you let them enter here will be displayed in the search engine.
Individual page display settings.
If you want to set SEO for individual pages, such as fixed pages or post pages, there are boxes at the bottom of the individual pages, so please enter the necessary information in the respective boxes.
Conclusion.
In this article, we showed you how to implement SEO features in WordPress without using plug-ins.
The work itself does not take long and can be implemented quickly, so please refer to this if you like.
Thank you for watching until the end.
Code Web Wordpress
How to save rewriting links.WordPress
If you are building multiple WordPress sites, you face the problem of having to ‘rewrite URLs’ when duplicating them. The following code is recommended to solve this problem before it happens. Please describe it in the theme’s ‘functions.php’ file. /*...
Continue readingCDN Security Speed Web
Does CDN delivery with Cloudflare reduce AdSense revenue?Various setting methods.
1. The impact of Adsense.2. The cause is that individual IPs are indistinguishable through Cloudflare (they are all the same IP through Cloudflare).3. How to make Nginx recognise individual IPs differentially.4. How to make apache recognise individual IPs separately. The...
Continue readingCode Web Without Plugin Wordpress
Without Plugin!Easy copy and paste. how to add a site name shortcode to WordPress.
It is very time-consuming to rewrite site names when producing multiple sites. As a way of preventing this from happening, it is recommended to set up a system to call the name of the site by a shortcode. Write the...
Continue readingCode
How to unzip a zip file in the server.
Assume the file you want to unzip is ‘example.zip’. 1. 1:Upload the zip file you want to unzip.2. 2:Creating and uploading unzip.php.3. 2:Access unzip.php.4. 3:Deployment of zip.5. 4:Finish. 1:Upload the zip file you want to unzip. Upload the zip file...
Continue readingCode Speed Web Wordpress
How to score 100 on the Google Speed Test.How to create better web vitals with WordPress themes and plugins. ‘Checklist for developers’.
This post explains how to get a score of 100 on the google speed test. 1. 1.Files2. 2.Fonts and typography3. 3.CSS framework4. 4.Conditional asset loading4.1. Inline styles4.2. Inline scope4.3. Using transients to store conditions4.4. Checking by name for loading5. 5.Image...
Continue readingCode Security Web Wordpress
How to make all pages permanently SSL.WordPress
Has SSL been installed on your website? The introduction of SSL is recommended for all websites because of its security and SEO advantages. Full-page SSL is particularly desirable for shopping and crowdfunding websites where personal data and payments are involved....
Continue readingCode Web Without Plugin Wordpress
Without plugins!Copy and paste, 3 minutes, super easy!How to implement dark mode.WordPress
Implement dark mode in WordPress.No plugin is required. 1. 1.JavaScript description.2. 2.Css description.3. 3.HTML description. 1.JavaScript description. Put the following js code in the header. document.addEventListener('DOMContentLoaded', function() { const toggleButtons = document.querySelectorAll('.toggle-mode'); toggleButtons.forEach(function(button) { button.addEventListener('click', function() { const currentMode =...
Continue readingCDN Security Speed
DNS configuration with Cloudflare.Set up bombastic DNS for a more comfortable IT life.
DNS configuration with Cloudflare not only speeds up browser start-up, but also contributes to improved security. Please give it a try. 1. 1.1.1.12. 1.1.1.1 family oriented.2.1. Blocks malware.2.2. Blocks malware and adult content. 1.1.1.1 1.1.1.1 is Cloudflare’s public DNS resolver.It...
Continue readingPlant
Copiapoa cinerea var. cinerea
Copiapoa cinerea is known as the ‘white cactus’ and is arguably the most popular cactus in Japan. Individuals with particularly white skin and pitch-black spines fetch high prices. 1. Synonym2. Origin3. Habitat4. Description5. Farming6. Way of increasing Synonym Below are...
Continue readingPlant
What is a cactus. Origin and characteristics of ‘Cactos’ [1].
The word ‘Cactus’ derives from the Greek word ‘Kàktos’. The term was used by Theophrastus in his book HistoriaPlantarum to describe ‘unknown thorny plants’. The term ‘Cactus’ was subsequently adopted by the renowned botanist Linneo to define the American thorny...
Continue reading