<?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>schema.org Archives - Alexandros Georgiou</title>
	<atom:link href="https://www.alexgeorgiou.gr/tag/schema-org/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.alexgeorgiou.gr/tag/schema-org/</link>
	<description>Balancing brackets for a living</description>
	<lastBuildDate>Wed, 20 Dec 2023 10:36:17 +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>schema.org Archives - Alexandros Georgiou</title>
	<link>https://www.alexgeorgiou.gr/tag/schema-org/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>💩 Please, WordPress theme and plugin developers, learn to write proper HTML already&#8230; smh</title>
		<link>https://www.alexgeorgiou.gr/how-to-html-in-wordpress/</link>
					<comments>https://www.alexgeorgiou.gr/how-to-html-in-wordpress/#respond</comments>
		
		<dc:creator><![CDATA[alexg]]></dc:creator>
		<pubDate>Fri, 07 Feb 2020 17:28:38 +0000</pubDate>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[ARIA]]></category>
		<category><![CDATA[code quality]]></category>
		<category><![CDATA[code style]]></category>
		<category><![CDATA[coding standards]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[markup]]></category>
		<category><![CDATA[microformats]]></category>
		<category><![CDATA[open graph]]></category>
		<category><![CDATA[output buffering]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[schema.org]]></category>
		<category><![CDATA[string interpolation]]></category>
		<category><![CDATA[templating]]></category>
		<category><![CDATA[wordpress]]></category>
		<guid isPermaLink="false">https://www.alexgeorgiou.gr/?p=303</guid>

					<description><![CDATA[<p>Use PHP output buffering in WordPress to interpolate HTML code and dynamic values in a way that's more readable.</p>
<p>The post <a href="https://www.alexgeorgiou.gr/how-to-html-in-wordpress/">💩 Please, WordPress theme and plugin developers, learn to write proper HTML already&#8230; smh</a> appeared first on <a href="https://www.alexgeorgiou.gr">Alexandros Georgiou</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>I know, I know, I&#8217;ve <a href="https://www.alexgeorgiou.gr/wordpress-php-code-quality/">ranted before about code quality</a>, but today I want to focus more on how we, as WordPress theme and plugin developers, write HTML.</p>



<h2 class="wp-block-heading">Don&#8217;t print out too early</h2>



<p>We&#8217;ve all been there: You&#8217;ve happily opened your PHP tag, <code>&lt;?php</code>, at the beginning of the file, at character #1. Your file is going to contain only code, no markup. Perhaps the file is included before any HTML output has even begun.</p>



<p>Being the savvy PHP developer that you are, you know all too well, that if your code were to output even one extra white-space, that would screw up the page&#8217;s markup. You&#8217;re careful not to use any closing <code>?&gt;</code> tag at the end of the file, to avoid any mishaps. Even a stray linefeed character could trigger output buffering early, and then out goes any chance for other code to modify the HTTP response headers, among other things.</p>



<p>So now you&#8217;re happily defining your functions, classes and other code-y stuff, then all of a sudden, you need to build some markup! Perhaps you&#8217;re printing out a UI element, maybe it&#8217;s a widget, maybe it&#8217;s a shortcode, maybe it&#8217;s even an RSS feed or other XML, but in any case, it&#8217;s too small for you to define an entire <a rel="noreferrer noopener" aria-label="template-part (opens in a new tab)" href="https://developer.wordpress.org/reference/functions/get_template_part/" target="_blank">template-part</a>. Your string is going to be some concatenation of hard-coded markup and dynamic data that&#8217;s currently sitting in variables.</p>



<h2 class="wp-block-heading">How To Meet Ladies</h2>



<p>Everyone says they know HTML. But HTML is easy to get started with, and hard to master.</p>



<p>When writing HTML markup for a theme or plugin in WordPress, your professional-grade code must fulfill a number of simultaneous requirements. Your main concerns should be to:</p>



<ul class="wp-block-list"><li>Write correct HTML5 markup that is SEO friendly, using semantic tags where possible (e.g. <code>&lt;nav&gt;</code>, <code>&lt;article&gt;</code>, <code>&lt;aside&gt;</code>, etc).</li><li>Escape any values properly</li><li><a href="https://www.alexgeorgiou.gr/make-pot-wordpress/">Internationalize strings so they can be translated</a></li><li>Give unique IDs to your elements, and name your classes properly, to assist any CSS and JS assets that go with your markup. Remember, naming things is one of the two hard things in computer science!</li><li>Conform to the <a href="https://make.wordpress.org/core/handbook/best-practices/coding-standards/html/" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">WordPress HTML Coding Standards</a>. This is a good standard to follow, because it forces you to be consistent about how to open and close your PHP tags, among other things.</li><li>Depending on use case, optionally add semantic metadata, such as <a rel="noreferrer noopener" href="http://microformats.org/" target="_blank">microformats.org</a>, <a rel="noreferrer noopener" href="https://schema.org/" target="_blank">schema.org</a>, <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://ogp.me/" target="_blank">Open Graph</a>, <a rel="noreferrer noopener" href="https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA" target="_blank">ARIA</a>, etc</li></ul>



<h2 class="wp-block-heading">A concrete example</h2>



<p>So let&#8217;s say you want to output some HTML form when a shortcode is used. We&#8217;ll give the form a unique ID and we&#8217;ll also embed some default values, coming from the shortcode&#8217;s attributes.</p>



<h3 class="wp-block-heading">Don&#8217;t do this:</h3>



<p>You might be tempted to do the following:</p>



<pre class="wp-block-preformatted"><code>$id = uniqid( 'form' );</code>
<code>$html  = '&lt;form id="' . esc_attr( $id ) . '"&gt;';</code>
<code>$html .= '&lt;input type="text" name="name" placeholder="' . esc_attr( 'Enter your name', 'langdomain' ) . '" value="' . esc_attr( $atts['name'] ) . '" /&gt;';</code>
<code>$html .= '&lt;input type="number" name="age" placeholder="' . esc_attr( 'Enter your age', 'langdomain' ) . '" value="' . absint( $atts['age'] ) . '" /&gt;';</code>
<code>$html .= '&lt;button type="submit"&gt;' . esc_html( 'Submit', 'langdomain' ) . '&lt;/button&gt;';</code>
<code>$html .= '&lt;/form&gt;';</code></pre>



<p>Sadly, a lot of people would say this is fine! I can&#8217;t blame them too much. We&#8217;re all way too used to opening the PHP tag and then never closing it again: we tend to use PHP as if it was Java. And with so many PHP templating libraries out there, it&#8217;s hard to remember that PHP <em>is</em> primarily a templating tool.</p>



<h3 class="wp-block-heading">Do this instead:</h3>



<p>We can combine the awesome templating power of the language with some cleverly nested <a rel="noreferrer noopener" aria-label="output bufferin (opens in a new tab)" href="https://www.php.net/manual/en/book.outcontrol.php" target="_blank">output buffering</a>, and now the above can be re-written as follows:</p>



<pre class="wp-block-preformatted"><code>$id = uniqid( 'form' );</code>

<code>ob_start();</code>
<code>?&gt;</code>
<code>&lt;form id="&lt;?php esc_attr_e( $id ); ?&gt;"&gt;</code>

    <code>&lt;input type="text" name="name" placeholder="&lt;?php esc_attr_e( 'Enter your name', 'langdomain' ); ?&gt;" value="&lt;?php esc_attr_e( $atts['name'] ); ?&gt;" /&gt;</code>
    <code>&lt;input type="number" name="age" placeholder="&lt;?php esc_attr_e( 'Enter your age', 'langdomain' ); ?&gt;" value="&lt;?php esc_attr_e( $atts['age'] ); ?&gt;" /&gt;</code>

    <code>&lt;button type="submit"&gt;&lt;?php esc_html_e( 'Submit', 'langdomain' ); ?&gt;&lt;/button&gt;</code>
<code>&lt;/form&gt;</code>
<code>&lt;?php</code>
<code>return ob_get_clean();</code></pre>



<p>To see a somewhat more complete example, here&#8217;s the above as a gist:</p>



<script src="https://gist.github.com/alex-georgiou/2299b24680e4fe5258ac0285044b8e56.js"></script>



<p>Notice how we start a new output buffer with <code><a rel="noreferrer noopener" aria-label="ob_start() (opens in a new tab)" href="https://www.php.net/manual/en/function.ob-start.php" target="_blank">ob_start()</a></code> and how we then get the resulting string with <code><a rel="noreferrer noopener" aria-label="ob_get_clean() (opens in a new tab)" href="https://www.php.net/manual/en/function.ob-get-clean.php" target="_blank">ob_get_clean()</a></code>. Notice also how we&#8217;ve swapped <code><a rel="noreferrer noopener" aria-label="esc_html() (opens in a new tab)" href="https://developer.wordpress.org/reference/functions/esc_html/" target="_blank">esc_html()</a></code> with <code><a rel="noreferrer noopener" aria-label="esc_html_e() (opens in a new tab)" href="https://developer.wordpress.org/reference/functions/esc_html_e/" target="_blank">esc_html_e()</a></code> and <code><a rel="noreferrer noopener" aria-label="esc_attr() (opens in a new tab)" href="https://developer.wordpress.org/reference/functions/esc_attr/" target="_blank">esc_attr()</a></code> with <code><a rel="noreferrer noopener" aria-label="esc_attr_e() (opens in a new tab)" href="https://developer.wordpress.org/reference/functions/esc_attr_e/" target="_blank">esc_attr_e()</a></code>. Similarly, we would swap <code><a rel="noreferrer noopener" aria-label="sprintf() (opens in a new tab)" href="https://www.php.net/manual/en/function.sprintf.php" target="_blank">sprintf()</a></code> with <code><a rel="noreferrer noopener" aria-label="printf (opens in a new tab)" href="https://www.php.net/manual/en/function.printf.php" target="_blank">printf</a>()</code>, <code><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://developer.wordpress.org/reference/functions/checked/" target="_blank">checked( $value, true, false )</a></code> with <code>checked( $value, true )</code>, and so on.</p>



<p><strong>PRO TIP:</strong> If your code throws an exception, you can use <code><a rel="noreferrer noopener" aria-label="ob_end_clean() (opens in a new tab)" href="https://www.php.net/manual/en/function.ob-end-clean.php" target="_blank">ob_end_clean()</a></code> in your <code>catch</code> clause to cancel your buffer.</p>



<h2 class="wp-block-heading">&#8220;Why does it even matter&#8221;, you ask?</h2>



<p>Well, there&#8217;s a few advantages with the second form of our, err&#8230;, form!</p>



<ul class="wp-block-list"><li><strong>Readability.</strong> I find the second piece of code to be much more readable.</li><li><strong>Syntax coloring.</strong> Yes, some editors do perform HTML parsing and show visual queues related to the syntax of HTML inside strings. But not every editor does this, and the ones that do, cannot do this perfectly.</li><li><strong>No quote hell.</strong> No need to count opening and closing <code>'</code> characters, interpolated with <code>"</code> characters while printing HTML attributes. This business can quickly get confusing, and is a common source of errors.</li><li><strong>Show off</strong> your mad PHP output buffering skillz! Your average front-end developer will be astonished at your professional-looking code!</li></ul>



<p>Now, if only we were allowed to use shorthand PHP tags, our code could be even nicer. Sadly, <a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#no-shorthand-php-tags" target="_blank">we can&#8217;t</a>.</p>



<p><code>?&gt;</code></p>
<p>The post <a href="https://www.alexgeorgiou.gr/how-to-html-in-wordpress/">💩 Please, WordPress theme and plugin developers, learn to write proper HTML already&#8230; smh</a> appeared first on <a href="https://www.alexgeorgiou.gr">Alexandros Georgiou</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.alexgeorgiou.gr/how-to-html-in-wordpress/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
