Day 15 – Share a WordPress tip or trick

Prompt 15/20

Blog: Share a WordPress tip or trick that has made your life easier. Post your response on a WordPress website and link it in the comments.

There are probably a couple for me, the first being ‘Plucks a certain field out of each object or array in an array.

$categories = get_the_terms( get_the_ID(), 'category' );
wp_list_pluck( $categories, 'term_id' );

Just feels like a really efficient way of just getting the info we need, in the above instance we just want and array of Category IDs that we could do something with. But this could be post IDs or child pages via query args.

Another is ‘Displays translated text that has been escaped for safe use in HTML output‘ This one is best if we might be using HTML in the string

<?php esc_html_e( 'Skip to main content', 'dogwonder' ); ?>

We could also use the following depending on the context in which the text is being used.

esc_html__( 'text to translate', 'text-domain' ) // To store a translated string in a variable, can be echoed later
__( 'text to translate', 'text-domain' ) // Needs echoing, no escaping
_e( 'text to translate', 'text-domain' ) // No escaping echos directly

In general it’s probably good practice to use either esc_html_e or esc_html__

It also has the benefit of preventing cross-site scripting (XSS) attacks.

Now you may never wish to translate your theme or plugin, but it’s not a bad idea to do the above if you ever think it might happen. It will save a huge amount of time later. You can then use a tool such as Poedit so scan your theme or plugin to create an empty translation file.

View All Posts