<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>seo Archives - Alexandros Georgiou</title>
	<atom:link href="https://www.alexgeorgiou.gr/tag/seo/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.alexgeorgiou.gr/tag/seo/</link>
	<description>Balancing brackets for a living</description>
	<lastBuildDate>Mon, 30 May 2016 06:26:12 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://www.alexgeorgiou.gr/wp-content/uploads/2021/07/cropped-alexgeorgiou-icon-32x32.png</url>
	<title>seo Archives - Alexandros Georgiou</title>
	<link>https://www.alexgeorgiou.gr/tag/seo/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Using the WordPress bloginfo filter to add HTML markup</title>
		<link>https://www.alexgeorgiou.gr/use-wordpress-bloginfo-filter-add-html-markup/</link>
					<comments>https://www.alexgeorgiou.gr/use-wordpress-bloginfo-filter-add-html-markup/#comments</comments>
		
		<dc:creator><![CDATA[alexg]]></dc:creator>
		<pubDate>Tue, 17 May 2016 16:50:04 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[bloginfo]]></category>
		<category><![CDATA[filter]]></category>
		<category><![CDATA[metadata]]></category>
		<category><![CDATA[seo]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">http://www.alexgeorgiou.gr/?p=72</guid>

					<description><![CDATA[<p>In this article I will show you how to use a bloginfo filter to add markup to your WordPress blog's title and other metadata, without hurting your SEO.</p>
<p>The post <a href="https://www.alexgeorgiou.gr/use-wordpress-bloginfo-filter-add-html-markup/">Using the WordPress bloginfo filter to add HTML markup</a> appeared first on <a href="https://www.alexgeorgiou.gr">Alexandros Georgiou</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In this article I will show you how to use a <a href="https://codex.wordpress.org/Plugin_API/Filter_Reference/bloginfo"><code>bloginfo</code></a> filter to add markup to your WordPress blog&#8217;s title and other metadata, without hurting your SEO.</p>
<h2>The problem with the bloginfo filter: context!</h2>
<p>The <a href="https://developer.wordpress.org/reference/functions/bloginfo/">bloginfo()</a> function can, unsurprisingly, be used to retrieve info about your blog! Say you need to show the name of the site in your theme. If you&#8217;re anywhere under the <code>body</code> tag, you might do something like the following:</p>
<pre>&lt;h1&gt;&lt;?php bloginfo( 'name' ); ?&gt;&lt;/h1&gt;</pre>
<p>Now say you want to use that same title in the HTML headers. Being an avid HTML markup expert, you might be tempted to do something like:</p>
<pre>&lt;head&gt;
    &lt;title&gt;&lt;?php bloginfo( 'name' ); ?&gt;&lt;/title&gt;</pre>
<p>What&#8217;s the problem with the above? Often nothing.</p>
<p>Until you start using the <code>bloginfo</code> filter. If you use it to modify the <em>title text</em> of the blog, you might want your modification to be reflected <em>everywhere</em>:</p>
<pre>public function filter_bloginfo( $name, $show = null ) {
    if ( 'name' == $show ) {
        <strong>return "The $name blog";</strong>
    } else {
        return $name;
    }
}
add_filter( 'bloginfo', 'filter_bloginfo', 10, 2 );</pre>
<p>In which case you are fine.</p>
<p>But suppose you are using it to add <em>markup;</em> perhaps you&#8217;re writing a plugin that makes the title text bold (and you hate using CSS!). OK, this is a bad example, I know, but there are legitimate cases where you might want to add markup; perhaps you want to add a link onto the site title from your plugin.</p>
<p>In any case, for the sake of simplicity, let&#8217;s stick with our &#8220;Site title boldifier&#8221; plugin! Consider the following code:</p>
<pre>public function filter_bloginfo( $name, $show = null ) {
    if ( 'name' == $show ) {
        <strong>return "&lt;strong&gt;$name&lt;/strong&gt;";</strong>
    } else {
        return $name;
    }
}
add_filter( 'bloginfo', 'filter_bloginfo', 10, 2 );</pre>
<p>In this case you would want the <code>strong</code> tag to show anywhere under the <code>body</code> tag, but not in the HTML head&#8217;s <code>title</code> or <code>meta</code> tags. Otherwise you might get something horribly wrong, such as:</p>
<pre>&lt;head&gt;
    &lt;title&gt;&lt;strong&gt;The blog's title&lt;/strong&gt;&lt;/title&gt;</pre>
<p>Congrats! All your SEO efforts have gone out the window! The <code>strong</code> tag will now show in the browser window title, in search engine results, in any place where <a href="http://ogp.me/">OpenGraph</a> matters (such as Facebook), in any <a href="https://en.wikipedia.org/wiki/Microdata_(HTML)">Microdata</a> you might be using, in your <a href="http://www.sitemaps.org/">sitemap.xml</a>, and generally, in all the wrong places. Not a very pro move!</p>
<p>The solution? Easy. Simply don&#8217;t use <code>bloginfo()</code> in your theme&#8217;s meta tags; use <a href="https://developer.wordpress.org/reference/functions/get_bloginfo/"><code>get_bloginfo()</code></a> instead! Set the second parameter, <code>filter = false</code>, and the string remains unfiltered.</p>
<pre>&lt;head&gt;
    &lt;title&gt;&lt;?php echo get_bloginfo( 'name', false ); ?&gt;&lt;/title&gt;</pre>
<p>This is a good solution,<em> if you, the theme author is also the site owner, and you know that this is what you want</em>. But what if you&#8217;re not?</p>
<p>(Incidentally, this is <em>not</em> how the experts at <a href="https://profiles.wordpress.org/automattic/">Automattic</a> are doing it. In at least the latest few /^twenty\w+teen$/ themes, the blog name is <em>always</em> filtered, <em>wherever</em> it is used. The <em>Yoast SEO</em> plugin also uses the <em>filtered</em> metadata for OpenGraph and other tags. They are correctly making the assumption that you are using the filter to modify the <em>text content</em> of your meta, not the <em>tags</em>. After all, HTML tags belong in theme templates, right?)</p>
<blockquote><p>But what if you&#8217;re a <em>plugin developer</em>, and you have a legitimate reason for adding markup to the title from your plugin code, and your plugin has to work with any and all themes out there?</p></blockquote>
<p>And as a <em>theme developer</em>, how do you know whether the modifications that the filter is doing to the blog&#8217;s title —or indeed to <em>any</em> bloginfo metadata— is intended to be shown in the site&#8217;s metadata or not?</p>
<p>You really don&#8217;t!</p>
<p>A possible compromise would be to run the filter in your theme, but then filter out any tags:</p>
<pre>&lt;head&gt;
    &lt;title&gt;&lt;?php echo strip_tags( get_bloginfo( 'name' ) ); ?&gt;&lt;/title&gt;</pre>
<p>This would ensure that any <em>text</em> modifications are reflected in the site&#8217;s title and meta tags, while any tags will not show in the browser&#8217;s title for instance. At least that&#8217;s true for the metadata that you control as a theme developer.</p>
<p>But suppose if you&#8217;re filtering the title in your plugin to do something using HTML tags, like our <code>&lt;strong&gt;</code> example above. You have taken extra care so that your theme shows the tags only where it&#8217;s supposed to. But the site owner has installed something like <a href="https://wordpress.org/plugins/wordpress-seo/">Yoast SEO</a> which writes its own metadata in your HTML. You can&#8217;t control <em>those</em> <code>meta</code> tags. And even if you can, you can&#8217;t do this for all the plugins out there! Again, you&#8217;re in trouble.</p>
<h2>Don&#8217;t just filter all the things with the bloginfo filter!</h2>
<p>If you think about it, what you are lacking is the information of <em>where the result of your filter is to be used</em>. Let&#8217;s rectify that:</p>
<pre>private $_in_body = false;

public function action_wp_head_finished() {
    $this-&gt;_in_body = true;
}
add_action( 'wp_head', array( &amp;$this, 'action_wp_head_finished' ), PHP_INT_MAX );

public function action_wp_footer_started() {
    $this-&gt;_in_body = false;
}
add_action( 'wp_footer', array( &amp;$this, 'action_wp_footer_started' ), 0 );</pre>
<p>The above OO code maintains a boolean value that can now tell you when you&#8217;re in the <code>body</code> tag of your markup and when you&#8217;re not. Notice how by setting our actions&#8217; priorities to <a href="http://php.net/manual/en/reserved.constants.php#constant.php-int-max"><code>PHP_INT_MAX</code></a> and <code>0</code>, we can ensure that our boolean becomes true only <em>after</em> other actions bound to <code>wp_head</code> have already run, and becomes false <em>before</em> other actions bound to <code>wp_footer</code> have run. (<code>PHP_INT_MAX</code> requires PHP 5.0.5, which shouldn&#8217;t be a problem. If it is, use a large integer literal instead.)</p>
<p>Now you, as a <em>plugin developer</em>, know when you&#8217;re in the <code>body</code> tag and when you&#8217;re not. So you can reasonably decide whether to filter or not, without any knowledge of how the installed theme works:</p>
<pre>public function filter_bloginfo( $name, $show = null ) {
    if ( 'name' == $show <strong>&amp;&amp; $this-&gt;_in_body</strong> ) {
        return "&lt;strong&gt;$name&lt;/strong&gt;";
    } else {
        return $name;
    }
}
add_filter( 'bloginfo', array( &amp;$this, 'filter_bloginfo' ), 10, 2 );</pre>
<p>Good job! Now you know that your plugin is only adding HTML markup in places where they are valid and can be shown.</p>
<p>Now go filter some metadata!</p>
<p>The post <a href="https://www.alexgeorgiou.gr/use-wordpress-bloginfo-filter-add-html-markup/">Using the WordPress bloginfo filter to add HTML markup</a> appeared first on <a href="https://www.alexgeorgiou.gr">Alexandros Georgiou</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.alexgeorgiou.gr/use-wordpress-bloginfo-filter-add-html-markup/feed/</wfw:commentRss>
			<slash:comments>11</slash:comments>
		
		
			</item>
	</channel>
</rss>
