Press "Enter" to skip to content

10 Pre-WCNYC Developer Tips

Are you pretty familiar with WordPress by now? Feel like you know all WordPress has to offer yet? Well after many years of WordPress development, I’m proud to say that I am still discovering new functions I’ve never used before or new capabilities behind functions I thought I knew. Since WordCamp is here in New York this weekend, clearly this is my rushed attempt to get something out there before then. Here are 10 pre-WCNYC developer tips that I hope level up your WordPress knowledge in some way. If you run into me this weekend, I’d love to hear if any of these helped you.

A non-WordPress related pause first…

Before I get into this post however, I want to pause for a moment of honesty. Writing this blog post after not having update this blog in literally a year, feels like throwing myself into daylight after having been trapped in a cave all this time.

The sunlight hurts my eyes sloth gif

I’m still on the fence how personal I should get on my blog that mainly attracts people looking for WordPress knowledge, and yet, here we are, and, this is my blog.

See – this cave I’m in is called grief.  I am currently grieving the loss of my mother half a year ago, and that, combined with some unhealthy work culture shifts have definitely stifled, if not, completed erased any passion I have for WordPress, writing and many of my other hobbies.

It wasn’t until I realized Wordcamp NYC was closer than I realized, that I had to make a new decision, one of the many I make on a daily basis during my “grief journey”. The “me” before all of this would have definitely went and looked forward to going. The “me” now, however, is feeling pretty meh about it. It wasn’t until I vented to a good friend of mine and she said, “Okay, that’s it. We’re both going. We need to motivate each other.”

And here I am. Bought my ticket and writing my first post in a year so that new RachieVee explorers don’t get discouraged by old content.

This is a small victory and I just want to take the time to remind everyone that life happens – you never know what people are going through.

So if someone has an open source project they’ve stopped contributing to, or an event they didn’t show up to this year, or a blog they haven’t kept up with, be kind. My friend was, and now I’m going to WordCamp NYC.

Thank you for taking the time to listen (err…read I mean). If you did, I hope that this could both be my confession and first brave step back into the WordPress community. I hope this can also serve as a reminder to anyone else out there, that they’re not alone if they’re in the same boat. Grief sucks. One day at a time. Having friends to remind you of your passions help.

Pre-WCNYC Developer Tips

These tips are geared towards experienced WordPress developers. Let me know if you have some of your own to add in the comments!

WordPress has a Trimming Function

Have you ever wanted to trim content and WordPress’ built in template functions get_the_excerpt or the_excerpt weren’t quite what you were looking for? And so you spent time on Stack Overflow and googled how to craft your own custom trimming function? Well, turns out WordPress already has a trim function that might fit your needs.

It’s wp_trim_words() and it’s used to chop off content by word count. You can pass in 3 parameters: content, character count, and what you want appended at the end of the trimmed content, which of course, is an excellent place to add your accessible read more links.

echo wp_trim_words( get_the_content(), 40, '...' );

Query Vars don’t work on CPTs

I learned about using query vars within the last year. They’re great for creating custom urls. For example, if I want to have “print” appended to the single post permalink, for a version of the same content, but for printing purposes, query vars are the perfect solution. The bad thing I discovered about query vars, however, is that they will not work on custom post types. In that case, you have to resort to rewrite rules.

Use pre_get_posts to alter the main loops

For a long time, to alter main loop content, I wiped the main loop from a WordPress template that was in use via the WordPress template hierarchy and created custom loops instead with WP Query or Get Posts. Within the last year, I learned about using pre_get_posts() instead. As an example, I had a custom post type archive with a main loop of posts that “expired” based on post meta. I wanted to avoid showing the expired posts on the front-end and without having to create a custom loop. Instead, I left the default loop in the archive template alone, and used pre_get_posts to alter the main query for that template. This allowed me to specify removing posts I didn’t want based on post meta before the main loop ran, without having to alter the loop within the template. In addition to a cleaner WordPress template, it’s also a best practice to use when altering the main queries.

You can order posts with “order_by” via a manual order

Did you know that you can pass “post__in” as a value to the “orderby” parameter in  WP Query? I didn’t. Came in handy for a custom field I built, where the user could choose the order of posts to display on the front-end, and I wanted to retrieve those posts in that exact order within the template from said field.

$args = array(
	'orderby' => 'post__in',
        'post__in' => [ 325, 568, 157 ],

$query = new WP_Query( $args )

A “secret” to getting post meta from a post object

When I wanted to get post meta from a post object ( from running a query ), I used to retrieve the post ID from the object and then use that ID in the get_post_meta function. Turns out, I never had to use that extra step and I can access the post meta directly from the post object with this:

//use this
//instead of this
get_post_meta( $post->ID, 'meta_key', true );

You can enqueue specific stylesheets and scripts for IE

The dreadful Internet Explorer! Unfortunately, there are still companies supporting less than optimal versions of this browser where custom CSS is still necessary. Finding myself in that exact situation, since CSS “hacks” are not as reliable as they used to be when we were still trying to support IE7/8, enqueuing IE specific stylesheets is the way to go.

You can do this with wp_style_add_data() which lets you specify that it’s a conditional stylesheet and for what version of IE you need it for. Here is a full tutorial on WP Scholar: Loading IE Conditional Stylesheets in WordPress to get you started. Keep in mind there is also a version for scripts, called wp_script_add_data().

WordPress Functions to use for Forms

I’ve been heavily working in forms for several months. I needed to show the state of these forms after the values were submitted, and I found a handful of helpful functions to help me do just that.

  • Checked: The checked() function allows me to compare values for checkboxes or radio buttons to display the current saved value.
  • Selected: The selected() function allows me to compare values for select boxes to display the current saved value.
  • Disabled: The disabled() function allows me to compare values to determine whether a field should be disabled.

Definitely saved me from writing my own custom functions.

Arrays in PHP Conditionals

Not a huge, mind blowing tip, but it’s just something I was doing that “hard way” when I didn’t need to. Sometimes I tend to write code how I think, especially when it comes to conditionals. For example, if I want to check whether we’re in several pages, I might separate my PHP conditional with multiple “or” operators. Instead, I can use an array for cleaner code and it means the same thing. See below for an example.

//What I used to do
if ( is_page( 'Cookies' ) || is_page( 'Cakes' ) || is_page( 'Candy' ) ) { 
    echo "These are all sweets!";

//What I do now
if ( is_page( [ 'Cookies', 'Cakes', 'Candy' ] ) ) { 
    echo "These are all sweets!";

Crop position with add_image_size

Using add_image_size() to create custom image sizes for my theme isn’t new to me, but taking advantage of crop position is. I used to blindly use default and hadn’t realized it was an option to specify how you want WordPress to crop your images until I truly understood this function. There might be cases where you want the top or bottom or left or right ( you get the picture, pun intended ) cropped instead and you can, by setting the last parameter in the add_image_size function. Here’s an in depth Guide to WordPress Image Cropping on WP Explorer.

add_image_size( 'custom-size', 220, 220, array( 'left', 'top' ) ); // Hard crop left top

When “get” functions don’t work outside the loop

I’ve written in past WordPress tutorials of mine a lot that the difference between most of WordPress’ “get” functions versus “the” functions, such as get_the_title() versus the_title(), is that “get” retrieves the value without automatically displaying it, and can be used outside the loop so long as you pass a post ID.

Well, turns out that doesn’t work for get_the_content(). Outside the loop, without referring to the Codex, one might assume you can pass in a post ID. Turns out, nope, you can’t. That’s where get_post_field() comes in handy instead.

echo get_post_field( 'post_content', 1234 );

And Good Night!

It’s 1 in the morning and officially WordCamp Sunday! I’m happy that I was able to reach my goal in posting to my blog right before I rolled into WCNYC this year, even if by a hair. And I hope these 10 pre-WCNYC Developer tips will be good conversation starters. If you found this helpful at all, feel free to let me know tomorrow in person. Or should I say today? The fact that I don’t care to spellcheck this at all is my cue to get a few hours of sleep. To small victories, WordPress, and to bed!

Be First to Comment

Leave a Reply

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

%d bloggers like this: