10 WordPress dev tips before going into WCNYC

Reading Time: 7 mins

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 here in New York is this weekend, clearly this is my rushed attempt to get something out there before then. Here are 10 WordPress dev tips before going into WCNYC this year. If you run into me this weekend, I’d love to hear if any of these helped you in some way.

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.

WordPress dev tips before going into WCNYC

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.

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. It 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.

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:

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.

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.

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.

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 WordPress dev tips before going into WCNYC 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!

10 Quick WordPress Tips for Front-end Developers

Reading Time: 8 mins

We don’t always realize how much we’re actively learning in our day to day work. We’ve gotten so used to the daily grind of web development, that perhaps finding answers to “gotchas” or successfully troubleshooting a problem, is something we celebrate with a fist bump in the air before we move on to the next thing. What helps me keep this blog going is taking a few seconds to write those discoveries down in a draft. While small, each new bit of knowledge levels me up. I want to share some of those with you today as quick WordPress tips.

The great thing about these 10 quick tips, are that they can be absorbed in this one read. No in-depth tutorials, no lengthy explanations – just small bits of knowledge that hopefully help you grow the same way they’ve enabled me to grow.

Half of these tips are for HTML or CSS, the other half, WordPress functions that I’ve found handy during front-end development. Let’s start from the quickest tips down to ones that require a little more explaining.

Continue reading 10 Quick WordPress Tips for Front-end Developers

Escaping WordPress Template Functions. To do or not to do?

Reading Time: 7 mins

Sanitization, escaping, and validation have become a regular part of my WordPress theme development within the last year. If those words confuse you, don’t worry, I’ve got another draft in my dashboard waiting to be finished. Eventually. 😉

But for those of you that do have a general idea of what these terms mean, perhaps you’ve faced the same nagging questions I had when it came to escaping WordPress template functions. At first, I was just applying escape functions mechanically, while not truly understanding what it was that I was doing, I knew that it was a best practice. Just like WordPress hooks, over time, my understanding became less fuzzy, but until these nagging questions could be answered, I couldn’t feel confident that I was escaping correctly.

Which WordPress template functions should be escaped? Which functions already have this built into core?

By template functions, I mean functions that are regularly used throughout theme development to call content from the dashboard. Like the_title(), the_permalink(), and the_excerpt() to name a few.

This becomes harder to figure out if, like me,  you hadn’t truly dived into WordPress’ mysterious core files for browsing. Or maybe you have, but found it overwhelming to follow the rabbit hole and chain of functions while simultaneously trying to make sense of how WordPress does just one thing. It doesn’t help if your code editor doesn’t make the task any easier. I’m a huge fan of Sublime Text, but have recently been exploring PHP Storm. I won’t lie to you, I still prefer Sublime for my daily development needs, but PHP Storm has a feature that has made learning about WordPress core so much easier.

You can Shift (PC) or Command (Mac) click on any function, WordPress or not, and it will take you to the file and line where that function is written. So for WordPress core, instead of being overwhelmed by all the files in the wp-admin or wp-includes folder, PHP Storm teleports you instantly!

A power ranger teleporting.

And so, via PHP Storm, I narrowed down which core functions actually needed escaping. I threw together a reference until it’s something I commit to memory, and thought it would be useful to share said reference with a blog post. In this post, I’ll also briefly review what escaping does for us, and how we would know if a template function needs escaping. Let’s get to it!

Continue reading Escaping WordPress Template Functions. To do or not to do?

Using ARIA Landmark Roles in WordPress

Reading Time: 6 mins

What’s a girl to write about after so long? Why, things that she loves of course – WordPress and Accessibility. I’ve been getting to know WAI ARIA in recent months, specifically using ARIA landmark roles in WordPress. This seemed like a great place to start as it was a handful of markup to memorize in an otherwise vast amount of ARIA to learn. It’s also familiar to developers if they’ve browsed through WordPress.org themes even if they haven’t realized it yet.

Before we move on, if you’re unfamiliar with what WAI ARIA is ( ARIA for short ), I’ll direct you to W3.org’s reference first.

Reading! So. Much. Reading…

 

Now if you’re like me, and stopped reading by the second line of that massive intimidating wall of jargon, I’d advise reading something easier on the eyes ( and brain ) like the A11y project’s, “Getting Started with ARIA“. I also found this lengthy article recently, “Introduction to WAI ARIA” written by Gez Lemon, and although it’s from 2008, it cleared up a lot for me. I’d recommend giving it a read through.

Now that we have a basic understanding of what ARIA is ( not the pirate Queen of Omega* ), let’s go over what ARIA landmark roles are and how they can make our WordPress themes more accessible.

* A Mass Effect reference that I couldn’t help, but take advantage of.

Continue reading Using ARIA Landmark Roles in WordPress

10 WordPress Things I’ve learned working with 10up

Reading Time: 8 mins

It’s been half a year since I joined the 10up team and I’ve been learning so much about the CMS I love so far. Working with a company where I can continue my WordPress adventures full time has been a rewarding experience. I’ve met new people, dived into open source, and made new discoveries in WordPress. Since it’s long past due for a new post here on RachieVee, I’d love to share 10 WordPress things I’ve learned working with 10up – a mixture of newly discovered tools, techniques and functions!

Spoiler alert, I’ve written more than ten and there are front-end related things too. Confession, I just wanted to use the whole 10 things with 10up title.

Continue reading 10 WordPress Things I’ve learned working with 10up