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

$defaults = [
    'type' => 'post',
    'posts_per_page' => 10
];

$args = wp_parse_args(['posts_per_page' => 5], $defaults);

print_r($args);

// Output:
Array
(
    [type] => post
    [posts_per_page] => 5
)

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:

$defaults = [
    'type' => 'post',
    'posts_per_page' => 10
];

$args = wp_parse_args('type=page&posts_per_page=15', $defaults);

print_r($args);

// Output:
Array
(
    [type] => page
    [posts_per_page] => 15
)

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

$data = [
    'name' => 'john doe',
    'sex' => 'male',
    'spouse' => [
        'name' => 'jane doe',
        'sex' => 'female'
    ]
];

$result = map_deep($data, function($item) {
    return ucwords($item);
});

print_r($result);

// Output:

Array
(
    [name] => John Doe
    [sex] => Male
    [spouse] => Array
        (
            [name] => Jane Doe
            [sex] => Female
        )
)

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:

$name = 'john doe';
        
$name = map_deep($name, function($item) {
    return ucwords($item);
});

echo $name; // John Doe

3. make_clickable

echo make_clickable('Visit https://heera.it for more.');

// Output (The URI will be rendered as a clickable link):

Visit https://heera.it for more.

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:

echo make_clickable('Email Address: mail@example.com');

// Output (The email address will be rendered as a clickable mailto link):

Email Address: mail@example.com

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

4. links_add_target

$text = 'Visit https://heera.it for more.';

$text .= 'Also check https://stackoverflow.com/users/741747/the-alpha.';

// Using the make_clickable function to generate
// clickable links from URIs available in the text
$html = make_clickable($text);

echo links_add_target($html);

// Output (formatted for clarity):

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

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

$html = "Hidden Gems Of WordPress - Part 1";

echo links_add_base_url($html, 'https://heera.it');

// Output (formatted for clarity):


Hidden Gems Of WordPress - Part 1

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.

echo links_add_base_url($content, 'https://heera.it', ['href']);

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
Php

PHP – 8.0 Match Expression

In PHP 8.0 there is a new feature or I should say a new language construct (keyword) going to be introduced,  which has been implemented depending […]