Little-Known Features in WordPress

Has it happened to you that while parsing the code of a third-party plugin or theme, you found a rather useful standard function that you did not know about before? At such moments, any developer feels a sense of self-worthlessness, remembering which bicycles he fanned in previous projects.

In order to reduce the number of disorders, I decided to describe several little-known, but very useful functions: Finds links in the text and makes them clickable. Example:

make_clickable


$string = "This is a long text that contains some links like http://www.wordpress.org and http://www.wordpress.com .";
echo make_clickable( $string ); 


popuplinks
Adds to target='_blank' rel='external'all links in the text.
Example:
$string = "This is a long text that contains some links like http://www.wordpress.org and http://www.wordpress.com .";
echo popuplinks( $string ); 


wp_list_pluck
Pulls out certain fields from the collection.
Example:
$posts = get_posts();
$ids = wp_list_pluck( $posts, 'ID' ); // [1, 2, 3, ...]


antispambot
Converts email addresses to HTML characters to protect against spam bots.
Example:
$email = 'example@email.com';
echo '' . antispambot( $email ) . '';


checked / selected
Adds the checked (selected) attribute if the first parameter is equal to the second.
Example:
 />


human_time_diff
Represents the time difference in a human-readable form.
Example:
$published = get_the_time( 'U' );
echo human_time_diff( $published ); // 2 days


wp_send_json_success / wp_send_json_error
Displays data in JSON format for Ajax requests.
Example:
if( $success ) {
    $result = array(
        'message'	=> 'Saved',
        'ID'		=> 1
    );
    wp_send_json_success( $result ); // { "success": true, "data": { "message": "Saved", "ID": 1 } }
}
else {
    wp_send_json_error(); // { "success": false }
}


wp_remote_get / wp_remote_post
Gets data from a third-party web resource.
Example:
$response = wp_remote_get( "https://api.twitter.com/1.1/search/tweets.json?q=%23WordPress", array( 'timeout' => 10 ) );
$tweets = wp_remote_retrieve_body( $response );


wp_is_mobile
Defines a user device.
Example:
if ( wp_is_mobile() ) {
    get_footer( 'mobile' );
}
else {
    get_footer();
}


wp_oembed_get
Converts a link to a media resource into a player code.
Example:
$youtube_url = 'https://www.youtube.com/watch?v=Lcvh0DgytH8';
$embed_code = wp_oembed_get( $youtube_url, array( 'width' => 800 ) );


wp_tempnam
Creates a temporary file with a unique name.
Example:
wp_tempnam( 'cache.log', get_temp_dir() );


zeroise
Pad the number with zeros to a specific length.
Example:
$tickets_count = 8;
echo zeroise( $tickets_count, 3 ); // 008


capital_P_dangit
Fixes a common mistake in a WordPress brand name.
Example:
$string = "I Love Wordpress";
echo capital_P_dangit( $string ); // I Love WordPress


get_num_queries
Shows the total number of SQL queries to the database on the page.
Example:
 -->


wp_text_diff
Finds differences in the text and displays them in a convenient way for comparison.
Example:
$left_string = 'This is the original string';
$right_string = 'This is the revised string';
echo wp_text_diff( $left_string, $right_string );


submit_button
Generates a button code.
Example:


enjoy :)

Also popular now: