Categories
Development WordPress

Advertising Gutenberg use on your website

The brand-new, all-singing, all-dancing, editor for WordPress is due imminently and I’ve been using the beta version for some time for creating posts on this site. Recently you may have noticed a little advertisement of this fact has appeared at the top of (some of) my posts – I’ve added this to help promote Gutenberg.

Let me share the code I’ve used to do this…

<?php
function add_gutenberg_promo( $content ) {
global $post;
if ( function_exists( ‘gutenberg_post_has_blocks’ ) && gutenberg_post_has_blocks( $post->ID ) && is_single() ) {
$content = ‘<div style=”background:#fff8c4; border:1px solid #f2c779; border-radius:10px; padding:10px; margin-bottom:10px;”>This post was created using Gutenberg, the new editor, coming soon, for WordPress. You can <a href=”https://wordpress.org/gutenberg/”>learn more about it here</a>, <a href=”https://wordpress.org/plugins/gutenberg/”>try the Beta plugin</a> or simply <a href=”https://testgutenberg.com”>try it out for yourself</a>, no site required.</div>’ . $content;
}
return $content;
}
add_filter( ‘the_content’, ‘add_gutenberg_promo’ );
view raw functions.php hosted with ❤ by GitHub

This uses a Gutenberg function to see if any blocks exist in the current post. If so, and this is a single post, it will then add a simple text box to the top of the content. Obviously, please style the message box to taste.

Talk to me!

This site uses Akismet to reduce spam. Learn how your comment data is processed.