Without plugins!How to implement SEO features.|WordPress

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://frecer.com/wp-content/uploads/frecer_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="frecer" />' . "\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 Without Plugin Wordpress

Without plugins!How to implement SEO features.|WordPress

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...

Continue reading

Plant

What is Gymnocalycium vatteri?

Gymnocalycium vatteri is a cactus of the genus Gymnocalycium. Native to Argentina-Uruguay in South America, it is rich in varieties and delights horticultural enthusiasts. All of these are seedlings with a tangle of local globe seedlings. It was kept in...

Continue reading

Code 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 reading

Plant Raise

The key points are sun, wind, water and soil! How to grow succulents.

Succulents are very easy to grow compared to other plants. There are four simple points. Maintain in a sunny location. Maintain in a well-ventilated area. Water when the soil is dry. Plant in well-drained soil. As long as the above...

Continue reading

Code Speed Web Without Plugin

Without Plugin!How to LazyLoad Google Adsense to speed up display speed.

Loading Google Adsense significantly reduces page display speed. The following methods will improve the expectation of problem resolution and should be tried. 1. Remove Script tag in Google Adsense code.2. Install code for LazyLoad. Remove Script tag in Google Adsense...

Continue reading

Plant

Astrophytum asterias.

It is one of the cacti with the greatest number of persistent enthusiasts. Astrophytum asterias is particularly popular because of the large number of varieties and the ease of both hybridisation and seed production. The lack of thorns is considered...

Continue reading

Code Web Without Plugin Woocommerce Wordpress

Without Plugin!How to display just any item on the Woocommerce My Account page.

The items on the Woocommerce My Account page vary depending on the products handled and the site they are on. For example, if you want to hide the ‘Dashboard’ and ‘Downloads’ items, you can achieve this by doing the following....

Continue reading

Code Speed Web Without Plugin Wordpress

Without plugins!How to use syntax highlighting Prism.js.|WordPress

In this article, we will show you how to implement syntax highlighting in WordPress without using a plugin. Syntax highlighting is often used to display source code on websites and blog sites. This website has adopted “Prism.js” after various trials....

Continue reading

Code

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 reading

Code 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 reading

Leave a Reply

This site uses cookies to offer you a better browsing experience. By browsing this website, you agree to our use of cookies.