Site icon CleanTalk's blog

Little-known functions in WordPress

CleanTalk is a SaaS spam protection service for Web-sites. CleanTalk uses protection methods which are invisible for site visitors. Connecting to the service eliminates needs for CAPTCHA, questions and answers and other methods of protection, complicating the exchange of information on the site.

Has it ever happened that during parsing code-party plugin or theme you found quite useful standard function, which did not know before? At such moments, any developer feels a sense of worthlessness, remembering he reinvented the wheel in previous projects.

In order to reduce the amount of frustration, I decided to describe a few little-known but very useful features:

make_clickable
Find in the text links and make them clickable.
Example:

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

popuplinks
Adds  target='_ blank' rel='external' to all the links in the text.
Example:

$string = "This is a long text that contains some links like <a href='https://www.wordpress.org'>https://www.wordpress.org</a> and <a href='https://www.wordpress.com'>https://www.wordpress.com</a> .";
echo popuplinks( $string ); 

wp_list_pluck
Takes out certain fields from the collection.
Example:

$posts = get_posts();
$ids = wp_list_pluck( $posts, 'ID' ); // [1, 2, 3, ...]

antispambot
Converts email addresses to symbols HTML for protection from spambots.
Example:

$email = 'ex*****@em***.com';
echo '<a href="mailto:' . antispambot( $email ) . '">' . antispambot( $email ) . '</a>';

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

<input type="checkbox" name="remember" value="1" <?php checked( $remember ) ?> />
<select name="options">
    <option value="1" <?php selected( $options, 1 ); ?>>1</option>
    <option value="2" <?php selected( $options, 2 ); ?>>2</option>
    <option value="3" <?php selected( $options, 3 ); ?>>3</option>
</select>

human_time_diff
Represents the difference in time in 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 a 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
Receives 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
Specifies the user’s device.
Example:

if ( wp_is_mobile() ) {
    get_footer( 'mobile' );
}
else {
    get_footer();
}

wp_oembed_get
Converts a link to a media resource in the code of the player.
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
Complements the number with zeros to the specified length.
Example:

$tickets_count = 8;
echo zeroise( $tickets_count, 3 ); // 008

capital_P_dangit
Corrects common errors in brand name WordPress.
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 page.
Example:

<!-- Number of queries: <?php echo get_num_queries(); ?> -->

wp_text_diff
Finds the differences in the text and displays them in a convenient form 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 code for the button.
Example:

<?php submit_button( __( 'Save Changes' ) ); ?>

enjoy 🙂

This text is a translation of article “Малоизвестные функции в WordPress” by Pingbull published on habrahabr.ru.

Forums and blogs without spam

CleanTalk is a SaaS spam protection service for Web-sites. CleanTalk uses protection methods which are invisible for site visitors. Connecting to the service eliminates needs for CAPTCHA, questions and answers and other methods of protection, complicating the exchange of information on the site.

Little-known functions in WordPress
Exit mobile version