0

I'm writing a plugin that helps show a flag if the site has a noindex tag in the header.

I'm using the wp_head() hook twice like this:

    add_action('wp_head', function() {
             ob_start();
        }, 0);

    add_action('wp_head', function() {
        $siteHeaderOutput = ob_get_clean();

        // just a variable that i'll be using in a transient
        $siteHasNoIndex = "not set";

        if (strpos($siteHeaderOutput, 'noindex') !== false) {
            $siteHasNoIndex = "no index";
            } else {
                    $siteHasNoIndex = "index ready";
            }

            // echo the header
            echo $siteHasNoIndex;

          // then set a transient for the admin panel
set_transient('escode_has_index', $siteHasNoIndex, DAY_IN_SECONDS);
            }, PHP_INT_MAX);

Right now I'm doing using a transient to pick up the header information in the admin panel to display the notice.

I'm doing this because admin_head won't render the actual site header.

I'd try using the get_meta_tags(get_site_url()) but it times out -- I think because there is a redirect caused by WP here.

Any idea if I can render that header while in the admin panel? If not my plugin has to rely on someone visiting the front end of the site in order for the information to become available in the backend.

Thanks

  • Would it make more sense to request the frontend in JS, and check the result? Calling frontend functions from WP Admin won't work as expected, and in some cases might cause problems. Things that would normally show in the frontend might not even run due to the is_admin() checks, making this particular approach unfeasible – Tom J Nowell Jul 16 '19 at 0:05
  • Out of curiosity, have you considered building your plugin around the default WP option, for search engine visibility found under Reading settings (get_option( 'blog_public'))? Other plugins setting "noindex" would naturally be missed when relying only on that option, but overall I think there would be a lot less jumping through hoops. – Antti Koskinen Jul 16 '19 at 7:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.