Categories
WordPress

How to Check Database’s Column Charset

I recently stumbled upon encoding issue in WordPress where user’s site post_content column of wp_posts table still uses utf8 charset instead of utf8mb4 which makes saved content comparison against $_POST‘s post content check fails when there is an emoji used inside the post_content given.

Thus, here’s how to check what charset is used in given column in WordPress:

global $wpdb;

// Let's assume you want to check `post_content` column of `wp_posts`
// $charset value here is either `utf8` or `utf8mb4`
$charset = $wpdb->get_col_charset( $wpdb->posts, 'post_content' );

As in why charset matters, here’s a quote of utf8 charset compared to utf8mb4 charsets:

The difference between utf8 and utf8mb4 is that the former can only store 3 byte characters, while the latter can store 4 byte characters. In Unicode terms, utf8 can only store characters in the Basic Multilingual Plane, while utf8mb4 can store any Unicode character. This greatly expands the language usability of WordPress, especially in countries that use Han character sets. Unicode isn’t without its problems, but it’s the best option available.

Source: https://make.wordpress.org/core/2015/04/02/the-utf8mb4-upgrade/

There’s also larger story behind why this charset update matters: The Trojan Emoji.

Photo by Fikret tozak on Unsplash

Categories
WordPress

How To Wrap WordPress’ Loop Posts Without Changing Theme Template

So here’s the task at hand. Let’s say you want to wrap your posts on the main loop:

<div class="wrapper">
  <!-- START WRAP HERE -->
  <article class="post-1">
  </article>
  <article class="post-2">
  </article>
  <article class="post-3">
  </article>
  <!-- END WRAP HERE -->
</div>

which is generated by this:

<div class="wrapper">
  <?php while ( have_posts() ) : ?>
    <?php the_post(); ?>
    <?php get_template_part( 
      'content', 
      get_post_format() 
    ); ?>
  <?php endwhile; ?>
</div>

But you can’t modify the theme template because it won’t be feasible; What are your option? loop_start and loop_end.

loop_start

You can check out about loop_start at documentation here. I personally found it out while roaming around core code at https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-query.php#L3278.

loop_end

Similar to loop_start, read about loop_end documentation here. Again, i found it much easier to understand the context by seeing it on core code: https://github.com/WordPress/WordPress/blob/master/wp-includes/class-wp-query.php#L3305

Example / Putting it together

To use it, you can do something like this at the theme’s functions.php:

function fr_wrap_main_loop_start() {
  echo '<div class="custom-posts-wrap">';
}
add_action( 'loop_start', 'fr_wrap_main_loop_start' );

function fr_wrap_main_loop_end() {
  echo '</div><!--- .custom-posts-wrap -->';
}
add_action( 'loop_end', 'fr_wrap_main_loop_end' );

You’ll get something like:

<div class="wrapper">
  <div class="custom-posts-wrap">
    <article class="post-1">
    </article>
    <article class="post-2">
    </article>
    <article class="post-3">
    </article>
  </div><!--- .custom-posts-wrap -->
</div>

Pretty neat 👌

***

Photo by DiEtte Henderson on Unsplash