It’s time to drop old PHP Versions

Dear fellow developers,
I think every year Matt receives a question at WordCamp US – “When will WordPress stop supporting PHP 5.2.17”, and every year the answer is the same – “we’re still thinking about it”.

I think the main reason for not forcing a PHP Upgrade is obvious – outdated hosting providers and outdated servers are holding us back. I think it’s time to stop that. More

Adding header video in custom pages

Twenty Seventeen WordPress theme has video support for headers. By default, the video header is only allowed on the front page, but there is an easy way to customize that using hooks by tapping into twentyseventeen_custom_header_args More

Picking values from an Array in WordPress

_.pick is great

Underscore.js has a lot of great stuff packed in it. One of the functions I love is pick, which lets you input some keys and get a new array from an Array.

Can’t pick in PHP

So today I wanted to find an alternative to Underscore’s _.pick for PHP, and I didn’t. It’s not a function that would be difficult to write, but that’s where WordPress comes in.

Can pick in WordPress

People at WordPress thought that it would be a nice helper for them too, so in WordPress 3.1 they added wp_array_slice_assoc.

The naming is a bit long, but the function is awesome anyway and the naming explains the purpose alright. Slice an array associativley ( that’s a big word ).

Here is how it works.

Imagine we have a some array like this one.

In this case the array is a mess, your array should never be a mess, but it might happen. In this case, I know I want only John and Jane in my array, so I can do this:

Tara. Now I have a new array “$persons” which has only john and jane.

How is this useful ?

Well, there are times where you would write something like this:

Which I think of an overkill, especially if you need like 10 variables. Of course you could extract from all of them

But what if the array has like 20 items. What happens when they clash? What if someone else modifies your code? I just don’t feel that blindly using extract is the best development pattern.

Along comes wp_array_slice_assoc()

Or a slightly more readable (based on preference) variant

That’s it.

Final Note

Please, please be careful extracting variables. And when you do, please document them, even if the documentation comes in the form of $keys_to_extract. Recently I had a very difficult time figuring out what is what because the author of a plugin pulled some weird variables out of nowhere and I had to run around var_dump’ing all over the place until I figured out what is what.

Snippet: Get the correct page in WordPress

Here is another really quick snippet.
If you have a page where you’re using Query Posts you’re going to have trouble with Pagination.
Wordpress for some reason likes to use page and paged randomly.

If you assign your page as the “front page”, you’re going to have to get_query_var(‘page’), and if you’re going to assign it elsewhere, it’s going to be paged. This is all fine and dandy, at least when you’re in control, but as soon as someone else starts using your theme, you need something more trustworthy, and that’s what this is.
More

Get Attachment ID from media_sideload_image()

Problem

In this scenario,

The $image variable now returns the full HTML, like this (with single quotes for some reason)

Which isn’t so bad, unless we want to get Just the image URL, or maybe the Thumbnail URL, or just the attachment ID.
Here is how to do it.
More

Get and Display Custom Taxonomies function

The new WordPress Custom Post types are awesome, and if you do want to categorize them differently, you can even add your own Taxonomies for them (like categories, tags, and even introduce your own names, like “colors”, “style”, etc. etc.). All seem unlimited, but I found myself struggling finding a way to reasonably display them afterwards.
Hope this function helps, if you ever find yourself in my position

WordPress archive title display switch

While creating my Blankwork I was looking for different examples of archives pages, since I never used them much. In the end, I was pretty disapointed with all of them.

For example, an else if statement list with opening PHP tags every time, repeating same text and adding comments in between. In my code editor it looked like one big rainbow. An example (which is short and a bit more readable):

I like when everything is clear and easy readable even in code. of course you can dig-through anything, but I made this little snippet just for those, who want a little more clean archives code.

So, I made my own little snippet, which is a bit more Designer friendly, here you go:

BlankWork – The WordPress Blank Framework

When I first started theming WordPress, most difficult thing for me to do was to find something that I could base my work on. I didn’t exactly start everything from scratch. Well – in the end I had to, because I couldn’t find any blank frameworks for WordPress.
While I did find about 10 stripped down templates – most of them didn’t suit my needs. I wanted a simple tiny framework to base my own code on, not tons of code that I have to dig through and then try to fit my work in.
So – I am working on The BlankWork, which is going to offer a basic (tiny tiny) framework to start your theme on. Currently I’m thinking of making the BlankWork in some ways like Initializr, so when downloading you would get some options (whether you want reset css or not, or maybe you want some predefined css styles, or maybe you want threaded comments, etc. ). This is definitely going to be open-source and I hope that with time I will succeed finding some contributors to the development and updating the theme as well.
I am so sure of this, that I already bought the domain – blankwork.net, I can’t wait, I can’t wait.

How to display sub-categories depending on what category you’re in.

Arhh, it took me a while, but I finally figured this out. Decided to share 🙂