Customize WordPress’ Generator Meta Tag

By default, WordPress add its name and the current version to the HTML generator meta tag. You can find it e.g. by opening the source code of a page within the browser and search for meta name=”generator”

In case you want to do not want this for whatever reason, it is quite simple to either remove or customize the generator tag. The function get_the_generator of wp-includes/general-template.php adds a filter called get_the_generator_{$type} (where {$type} is the type of output to return: html, xhtml, atom, rss2, rdf, comment, export).

Change the Tag Value

To change the value of the tag, I created another plugin. Here is the code:

<?php
/*
Plugin Name: Instance Factory Change Generator Tag
Plugin URI: https://instance-factory.com/?p=2141
Description: Sets the generator tag to a customized value
Author: Instance Factory, a project of the proccelerate GmbH
Author URI: https://proccelerate.de
Version: 1.0
*/

/* Disable direct access to this file */
if ( ! defined( 'ABSPATH' ) ) exit;
/* OK, start editing! Happy publishing */

/**
 * Creates the generator XML or Comment for RSS, ATOM, etc.
 *
 * Returns the customized generator tag for the requested output type.
 *
 * @param string $generator The tag content for the generator created by WordPress' default function.
 * @param string $type The type of output to return - (html|xhtml|atom|rss2|rdf|comment|export).
 * @return string|void The generator tag content.
 */
if (!function_exists('ifb_change_generator')) :
    function ifb_change_generator($generator, $type)
    {
        switch ($type) {
            case 'html':
                $generator = '<meta name="generator" content="Instance Factory Blog Engine">';
                break;
            case 'xhtml':
                $generator = '<meta name="generator" content="Instance Factory Blog Engine" />';
                break;
            case 'atom':
                $generator = '<generator uri="https://instance-factory.com"' . '">Instance Factory</generator>';
                break;
            case 'rss2':
                $generator = '<generator>' . esc_url_raw('https://instance-factory.com') . '</generator>';
                break;
            case 'rdf':
                $generator = '<admin:generatorAgent rdf:resource="' . esc_url_raw('https://instance-factory.com') . '" />';
                break;
            case 'comment':
                $generator = '<!-- generator="Instance Factory Blog Engine" -->';
                break;
            case 'export':
                $generator = '<!-- generator="Instance Factory Blog Engine"' . '" created="' . gmdate('Y-m-d H:i') . '" -->';
                break;
        }

        return $generator;
    }

    // Only add the filters once
    add_filter('get_the_generator_html', 'ifb_change_generator', 10, 2);
    add_filter('get_the_generator_xhtml', 'ifb_change_generator', 10, 2);
    add_filter('get_the_generator_atom', 'ifb_change_generator', 10, 2);
    add_filter('get_the_generator_rss2', 'ifb_change_generator', 10, 2);
    add_filter('get_the_generator_rdf', 'ifb_change_generator', 10, 2);
    add_filter('get_the_generator_comment', 'ifb_change_generator', 10, 2);
    add_filter('get_the_generator_export', 'ifb_change_generator', 10, 2);
endif;

/* That’s all, stop editing! Happy publishing */
?>

Like the original get_the_generator function, it is required to create the tag depending on the requested output type. So, I copied the switch statement, and only changed the values accordingly.

Of course, the filters are added too, so for every known output type the customized tag is generated.

And that’s already it. Quite simple, as soon as you know which filter to address 😉

Links

HTML generator meta tag
wpbeginner’s “What, Why, and How-To’s of Creating a Site-Specific WordPress Plugin