0

Hidden Gems of WordPress – Part 2

This is the second part of my “Hidden Gems of WordPress” series. If you missed the first part then you may check it out but it’s not necessary for this part. In the first part, I’ve discussed about some cool and useful functions that you may already know but why don’t you check it out, who knows, maybe you would find something new. Anyways, let’s begin with the second part today.

1. wp_parse_args

This function merges user defined arguments into the $defaults array. It looks like that, we could have used array_merge instead but it does things a little differently. First of all, this function accepts only two arguments while the array_merge can accept more than two arrays to merge them in one array, please check the PHP manual to learn more about array_merge by clicking the link given above. Anyways, but what else about this function? Well let’s check another example now:

Notice the first argument – ‘type=page&posts_per_page=15’, it’s a query string and in this case, the given string is valid because it accepts a query string and turns it into an array first (using wp_parse_str). Also, we can pass an object as the first parameter of this function. It could be an stdClass or a user defined class instance. In case of any class instance, only the non-static public properties will be pulled out from the object using get_object_vars.

2. map_deep

As name suggests, this function maps a function to all elements of an array. This is similar to array_walk_recursive but it also accepts object and any scalar value as well. In case of an object (stdClass/user defined), it uses get_object_vars to pull out the properties of the object. It returns an array unless you pass any scalar value as the first parameter. For example:

3. make_clickable

You’ll see something like the following on the screen: Visit https://heera.it for more. In this example I’ve used one link but it will convert all the URIs found in the given text.

This function converts plaintext URI to HTML links and the links become clickable. The description (developer.wordpress.org) says:

Converts URI, www and ftp, and email addresses. Finishes by fixing links within links.

So in that case, to render a mailto link, we can use the following:

You’ll see something like the following on the screen: Email Address: mail@example.com.

4. links_add_target

This function adds target=”_blank” attribute in all links found in the given text so basically the output will be rendered as something like the following:

Visit https://heera.it for more. Also check https://stackoverflow.com/users/741747/the-alpha.

In this case, clicking on any link will open a new browser tab because of the target=_blank attribute.

5. links_add_base_url

As name suggests, it adds a base url to relative links in passed content as the first parameter of function where the second parameter will be used as the base url. So, in this example, the href attribute in the a tag has a relative url (without the https://heera.it/). This function call added the base part, made it an absolute url in this case.

By default, it’ll add the base url in all links (href/src) found in the given content. So, if the passed content contains any script tags where the src attributes are relative, then it’ll add the base url given in the second parameter in all src attributes as well. It’s possible to modify this behavior by passing a third parameter as an array, so for example, the following call will only add base url in any tag with href attribute.

So, tags with src attributes will remain untouched but all tags with href attributes will be modified if any of the href has a relative url.

That’s all for today. All the functions I’ve mentioned here, are defined in “wp-includes/functions.php” and “wp-includes/formatting.php” and there are many more.

Latest Blog

0

New Union Type in PHP – 8

A new RFC has been finished voting and accepted for PHP – 8. That is “Union Types”, which is an extension of PHP’s type system. […]