Replace Yoast With A Simple Code Snippet

SEO Yoast has been a cornerstone for SEO professionals worldwide since its introduction, providing robust features to optimize websites for search engines. However, the increasing nags and upsell prompts for the pro version have bloated the core plugin, prompting many users to seek alternatives that offer essential SEO speed functionalities without the extraneous overheads of features never used. This script provides a lightweight alternative to Yoast SEO by leveraging Yoast’s existing data structure, ensuring backwards a and forward compatibility with YOAST tables just in case you don’t like the transition.

WordPress does not inherently cater to the description meta tag, yet this tag is a crucial quality indicator in SEO, especially for writing content that attracts clicks from search results. The meta description can significantly impact a page’s click-through rate, making it essential for effective SEO. While Yoast SEO has been widely used for its ability to manage these tags, its increasing bloat with additional features and upsell prompts has become a drawback. This script, by contrast, is designed with simplicity in mind, focusing solely on handling the meta title and description of posts and pages without any extra features. Its purpose is to provide a streamlined solution for managing these essential SEO elements, ensuring efficient performance without unnecessary overhead.

How To Get Rid Of Yoast SEO in 50 Lines of Code

It’s kinda rude of me to do this YOAST for all it’s bloat code is actually a relly cool bit of software. I have the paid and free versions of the plugin and prefer it over 90% of the other SEO plugins if I have to be honest. One of the biggest things I like about YOAST SEO is it’s ability to bulk update meta data and the direct .htaccess and robots editors. But outside of that (which I usually do serverside anyway) the only real reason for me keeping YOAST on my small sites is the custom meta title and descriptions. Accordion to RankMath smite at the date of writing this Yoast SEO contains 97.1k Lines of Code with a Plugin Folder size 32.4 MB and a Memory Usage overhead of +1.62 MB Adding to Page Speed load +0.18s depending on your processor and ram factors beyond the scope of this discussion. It’s also one of the most widely deployed SEO plugins in existnce with over 10+ Million Active Installations.

All the and the core functions of YOAST and Rankmath and all the other SEO plugins that bloat lean and mean wordpress instances can be handled in a function with less than 50 lines of code.

if (is_admin()) {
    function custom_seo_meta_add_meta_boxes() {
        add_meta_box('custom_seo_meta_title', 'Custom Meta Title', 'custom_seo_meta_title_callback', ['post', 'page'], 'side');
        add_meta_box('custom_seo_meta_description', 'Custom Meta Description', 'custom_seo_meta_description_callback', ['post', 'page'], 'side');
    }
    add_action('add_meta_boxes', 'custom_seo_meta_add_meta_boxes');
    function custom_seo_meta_title_callback($post) {
        $value = get_post_meta($post->ID, '_yoast_wpseo_title', true);
        echo '<input type="text" name="custom_meta_title" value="' . esc_attr($value) . '" style="width:100%;" />';
    }
    function custom_seo_meta_description_callback($post) {
        $value = get_post_meta($post->ID, '_yoast_wpseo_metadesc', true);
        echo '<textarea name="custom_meta_description" style="width:100%;" rows="4">' . esc_textarea($value) . '</textarea>';
    }
    function custom_seo_meta_save_meta_data($post_id) {
        if (array_key_exists('custom_meta_title', $_POST)) {
            update_post_meta($post_id, '_yoast_wpseo_title', sanitize_text_field($_POST['custom_meta_title']));
        }
        if (array_key_exists('custom_meta_description', $_POST)) {
            update_post_meta($post_id, '_yoast_wpseo_metadesc', sanitize_textarea_field($_POST['custom_meta_description']));
        }
    }
    add_action('save_post', 'custom_seo_meta_save_meta_data');
}
if (!is_admin()) {
    function custom_seo_meta_output() {
        if (is_singular()) {
            global $post;
            $custom_meta_description = get_post_meta($post->ID, '_yoast_wpseo_metadesc', true);
            if (!$custom_meta_description) {
                $custom_meta_description = wp_trim_words(get_the_excerpt(), 30, '');
                $custom_meta_description = substr($custom_meta_description, 0, 160);
            }
            echo '<meta name="description" content="' . esc_attr($custom_meta_description) . '" />';
        }
    }
    add_action('wp_head', 'custom_seo_meta_output', 1);
    function custom_seo_override_wp_title($title) {
        if (is_singular()) {
            global $post;
            $custom_meta_title = get_post_meta($post->ID, '_yoast_wpseo_title', true);
            return esc_html($custom_meta_title ?: $title);
        }
        return $title;
    }
    add_filter('pre_get_document_title', 'custom_seo_override_wp_title');
    add_action('after_setup_theme', function() {
        remove_theme_support('title-tag');
    }, 1);
}

Warning: All code found on this site is use at your own risk vibe. It worked for me when I wrote it, but if it breaks your stuff don’t come shout at me. Happy to help if I can, but there are no guarantees expressed and implied. This is merely an example for developers most of the code examples on this site are scripts that are used in Code Snippets functions.

The custom code ensures backward and forward compatibility with the Yoast SEO plugin by utilizing the same meta fields (_yoast_wpseo_title and _yoast_wpseo_metadesc) for storing meta titles and descriptions. This means that any meta information previously entered using Yoast SEO will be seamlessly recognized and used by the custom code, ensuring no data loss or duplication. Likewise, if Yoast SEO is re-installed in the future, it will continue to read from and write to these fields without any issues, allowing for a smooth transition and consistent SEO metadata management.

This code snippet is a custom implementation to replace the Yoast SEO plugin functionalities for managing meta titles and descriptions for WordPress posts and pages. Here’s an end-to-end explanation of what the code does:

Admin Side:

  1. Adding Meta Boxes:
    • The custom_seo_meta_add_meta_boxes function is defined to add custom meta boxes for the title and description in the post and page edit screens in the WordPress admin.
    • add_meta_box is used to add these meta boxes to the side of the post and page edit screens.
    • custom_seo_meta_title_callback and custom_seo_meta_description_callback are the callback functions that output the HTML for these meta boxes.
  2. Meta Box Callbacks:
    • custom_seo_meta_title_callback($post):
      • Retrieves the current value of the custom meta title from the post’s metadata (_yoast_wpseo_title).
      • Outputs an input field with this value, allowing the user to edit the meta title.
    • custom_seo_meta_description_callback($post):
      • Retrieves the current value of the custom meta description from the post’s metadata (_yoast_wpseo_metadesc).
      • Outputs a textarea with this value, allowing the user to edit the meta description.
  3. Saving Meta Data:
    • The custom_seo_meta_save_meta_data($post_id) function is defined to save the custom meta title and description when a post is saved.
    • It checks if the custom meta title and description fields are present in the $_POST array.
    • Uses update_post_meta to save the sanitized values into the post’s metadata.

Frontend Side:

  1. Outputting Meta Tags:
    • The custom_seo_meta_output function is defined to output the meta title and description tags in the HTML <head> section for singular posts and pages.
    • It retrieves the custom meta title and description values.
    • An anonymous function is added to the wp_head hook to output the meta tags:
      • If a custom meta title exists, it overrides the default title tag.
      • If a custom meta description exists, it outputs a meta description tag.
      • If no custom meta description exists, it generates a default description from the post excerpt and limits it to 160 characters.
  2. Overriding the Document Title:
    • The custom_seo_override_wp_title function is defined to override the document title with the custom meta title.
    • It uses the pre_get_document_title filter to change the document title if a custom meta title exists.

Conditional Checks:

  • The entire code is wrapped in conditional checks to separate the admin and frontend functionalities:
    • if (is_admin()) { ... } ensures that the admin-specific functions only run in the admin area.
    • if (!is_admin()) { ... } ensures that the frontend-specific functions only run on the frontend of the site.

Summary:

  • Admin Side:
    • Adds meta boxes for custom meta title and description.
    • Saves these values as post metadata.
  • Frontend Side:
    • Outputs custom meta title and description in the <head> section.
    • Overrides the document title with the custom meta title if it exists.

This custom implementation provides basic SEO meta management capabilities similar to Yoast SEO, focusing on meta titles and descriptions for individual posts and pages.

"The memory management on the PowerPC can be used to frighten small children."

Linus Torvalds

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top