WordPress is full of good functions for us developers to use. We can lift post lists out of skinny air, manipulate roughly all about them, squeeze any user we wish and arrangement their amicable media connectors in a jiffy.
There are however utterly a few functions that seem to be ignored for different reasons. I’ve been coding with WordPress for around 8 years now and spasmodic we still find something new! Let’s take a demeanour during some of my favourite ignored functions and learn how to use them along a way.
Recommended Reading: WordPress Conditional Tags (And Snippets) For Beginners
antispambot()
I customarily lift a few eyebrows with this one, it seems to be one of a many well-hidden functions in a codebase. codeantispambot()/code replaces characters with HTML entities that is one approach to mask email addresses from immorality scrapers.
$email = 'mymail@mail.com';
echo 'You can hit me during ' . antispambot( $email ) . ' any time'.
While this is a useful tidbit, it is also an instance of because some people criticize WordPress – this is a horribly named function. From reading a duty name, we have no suspicion what it does.
human_time_diff()
When we initial schooled about this duty about a year ago we suspicion it contingency have been a new serve that we ignored in a changelog. Not quite…
This duty – that outputs a disproportion between dual timestamps – has been in given chronicle 1.5 (that’s Feb 17, 2005!).
The following good dash we borrowed from a codex shows how prolonged ago a stream post was published. It uses a publish date of a post as a initial argument and a current date as a second.
echo 'This post was published ' . human_time_diff( get_the_time( 'U' ), current_time( 'timestamp' ) ) . ' ago';
get_post_meta()
Bear with me here, we know this is a well-used function, however, how it works is not-so-common knowledge. First of all, by omission a second and third parameters we can pull all metadata for a post.
$all_meta = get_post_meta( 14 );
Even if we usually squeeze a information for a singular pivotal all postmeta is pulled anyway.
The reason is indeed flattering logical. Metadata is used in mixed places. If codeget_post_meta()/code queries a database any time it was used we would finish adult with approach too many queries. Instead, if we lift metadata it caches it all and uses a cached values on all successive metadata retrievals.
wp_upload_bits()
This duty is a straight-up file uploading function. While it doesn’t pierce a record to a uploads folder and supplement it to a WordPress media section, it is intensely accessible and we can always to a rest with a codewp_insert_attachment()/code function.
$upload = wp_upload_bits( $file['name'], null, file_get_contents( $file['tmp_name'] ) );
Some reason is during palm for this: a initial parameter is a record name. The second is unheeded so it should be set to nothing (eyeroll during WordPress consistency). The third parameter is a tangible calm of a file.
get_post_field()
In a past we saw utterly a few examples where someone wrote a loop to get a criticism count for a post, or wrote a dedicated database query for it. You don’t need them, what we need is codeget_post_field()/code . This duty retrieves a value of a singular margin for a singular post in a database. Let’s squeeze a criticism count!
This post has ?php relate get_post_field( 'comment_count', 4124 ) ? comments.
wpautop()
This duty has come out into a spotlight a bit, though it is still comparatively unknown. It is identical to a PHP local codenl2br/code though instead of formulating new lines, it wraps your calm in paragraphs.
This is useful if we have a textbox and we wish to make certain that when users emanate paragraphs with double line breaks, they sojourn manifest in a front-end as well.
h2What Our Users Say/h2
?php relate wpautop( $user_comment ) ?
wp_is_mobile()
This aptly named duty detects when a user is on a mobile device and allows we to arrangement calm accordingly. Since this is a redeeming tab it earnings loyal or fake depending on a scenario.
?php if( wp_is_mobile() ) : ?
Visit a website on your desktop for a richer user experience
?php endif ?
DOING_AJAX
While this is a constant, not a duty (sorry), it is useful nonetheless. You can discern between AJAX calls and unchanging stuff by checking if it is tangible and is true. This can be useful if we are regulating a common duty for a charge that is infrequently triggered by AJAX.
if (defined('DOING_AJAX') DOING_AJAX)
relate 'done';
die()
else
wp_redirect()
The final instance shows another neat function: codewp_redirect()/code. This should be used in place of a PHP local codeheader()/code function. The WordPress redirection duty allows we to set a URL to route to, and also set a standing code, good for doing permanent redirects as well.
// For a URL that is no longer in use
wp_redirect( 'http://website.com/new-url/', 301 );
paginate_links()
I gamble that this duty owes a shade in partial to a recognition of a WP-PageNavi plugin. By default WordPress displays previous/next links during a finish of your post list. WP-PageNavi replaces that with page numbers.
This can indeed be finished with a small work regulating a codepaginate_links()/code functions. It has utterly a few parameters so we suggest holding a look during a documentation.
The following instance from a codex shows how we can supplement it to a default loop though adding it to tradition loops is not most of a stretch.
global $wp_query;
$big = 999999999; // need an doubtful integer
echo paginate_links( array(
'base' = str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' = '?paged=%#%',
'current' = max( 1, get_query_var('paged') ),
'total' = $wp_query-max_num_pages
) );
Conclusion
These are only some functions that seem to be less-known that a rest. we learn a new good duty about each dual months and I’m certain my developer friends out there could warn us even further.
If we have a favorite problematic duty or a duty that would be useful though isn’t available, let us know in a comments!
<!–
Suggestion:
–>
Advertisement
10 Useful WordPress Functions You Might Not Know About
Nenhum comentário:
Postar um comentário