Popular custom WordPress functions
Exactly one month ago, was publicized article about most popular WooCommerce functions. This article it’s peculiar continue with the difference but here will be only WordPress functions.
0. Display custom ACF link
<?php
function the_custom_acf_link( $title, $url, $target, $class = '' ) {
if ( ! $url ) {
return;
}
$target = ( $target ) ? '_blank' : '_self';
$class = ( $class ) ? 'class="' . preg_replace("/[^ \w]+/", "", $class) . '" ' : '';
$link = '<a href="' . esc_url( $url ) . '" ' . $class . 'target="' . $target . '">' . esc_html( $title ) . '</a>';
echo $link;
}
1. Display custom ACF image
<?php
function the_custom_acf_image( $image, $class = '' ) {
if ( ! $image ) {
return;
}
$class = ( $class ) ? 'class="' . preg_replace("/[^ \w]+/", "", $class) . '" ' : '';
$img = '<img ' . $class . 'src="' . esc_url( $image['url'] ) . '" alt="' . esc_attr( $image['alt'] ) . '">';
echo $img;
}
2. Check category has children
<?php
function category_has_children( $term_id = 0, $taxonomy = 'category' ) {
$children = get_categories( [
'child_of' => $term_id,
'taxonomy' => $taxonomy,
'hide_empty' => false,
'fields' => 'all',
] );
return ( $children );
}
3. Disable plugin update
<?php
add_filter( 'site_transient_update_plugins', 'disable_plugin_updates' );
function disable_plugin_updates( $value ) {
if ( isset($value) && is_object($value) ) {
if ( isset( $value->response['plugin-folder/plugin.php'] ) ) {
unset( $value->response['plugin-folder/plugin.php'] );
}
}
return $value;
}
4. Enable upload JSON files WordPress
<?php
add_filter('upload_mimes', 'sf_myme_types', 1, 1);
function sf_myme_types($mime_types) {
$mime_types['json'] = 'application/json';
return $mime_types;
}
5. Render page builder sections
<?php
function render_sections($field = 'sections') {
// Exit early if ACF is not activated
if( !function_exists('have_rows') ) return;
// For each row of sections
while (have_rows($field)) {
// Render the row by including a PHP file
the_row();
$layout = get_row_layout();
@include(THEME_DIR . "/$field/$layout.php");
// NOTE: Please don't edit this. Views and blocks can be easily achieved by creating view.php or block.php in the sections folder.
}
}
6. Popular Posts Counter
<?php
function set_post_views( $postID ) {
$count_key = 'koropets_post_views_count';
$count = get_post_meta( $postID, $count_key, true );
if ( $count == '' ) {
$count = 1;
delete_post_meta( $postID, $count_key );
add_post_meta( $postID, $count_key, $count );
} else {
$count++;
update_post_meta( $postID, $count_key, $count );
}
}
// To keep the count accurate, lets get rid of prefetching
remove_action( 'wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0 );
add_action( 'wp_head', 'track_post_views' );
function track_post_views( $post_id ) {
if ( ! is_singular( [ 'news', 'post', 'event' ] ) ) return;
if ( empty ( $post_id ) ) {
global $post;
$post_id = $post->ID;
}
set_post_views( $post_id );
}
7. Get User ID By Display Name
<?php
function get_user_id_by_display_name( $display_name ) {
global $wpdb;
if ( ! $user = $wpdb->get_row( $wpdb->prepare(
"SELECT `ID` FROM $wpdb->users WHERE `display_name` = %s", $display_name
) ) )
return false;
return $user->ID;
}
Sources:
This article is really a nice one it assists new web viewers,
who are wishing in favor of blogging.
Thanks! If you have any question ping me 🙂
What’s Taking place i am new to this, I stumbled upon this I have found It positively helpful and it has helped me out loads.
I hope to give a contribution & assist different customers like its helped me.
Great job.
Just desire to say your article is as astonishing.
The clarity in your post is just excellent and i can assume you’re an expert on this subject.
Fine with your permission allow me to grab your RSS feed to keep updated with forthcoming
post. Thanks a million and please carry on the gratifying work.
Hey there! Do you use Twitter? I’d like to follow you if that would be
okay. I’m undoubtedly enjoying your blog and look
forward to new posts.
Info about socials you can found at the top or bottom side of the site.
At this time I am ready to do my breakfast, after
having my breakfast coming over again to read additional news.
Amazing! This blog looks just like my old
one! It’s on a totally different topic but it has pretty much the same page layout and design. Superb choice
of colors!
Aw, this was a really good post. Spending some time and actual effort to generate a
superb article… but what can I say… I hesitate a lot and don’t manage to get nearly anything done.
Hello, I read your blog like every week. Your story-telling style is awesome, keep it up!
Thanks!
I pay a quick visit daily a few web sites and information sites to read
posts, but this website presents quality based articles.
Hmm it appears like your blog ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog.
I as well am an aspiring blog blogger but I’m still new to the whole thing.
Do you have any tips for first-time blog writers? I’d really appreciate it.
Heya i’m for the first time here. I found this board and I find It really useful &
it helped me out a lot. I hope to give something back and help
others like you helped me.
My brother suggested I might like this blog. He was entirely
right. This put up actually made my day. You can not imagine just how much time I had
spent for this information! Thank you!
You are welcome)
This is a very good tip especially to those fresh to the
blogosphere. Short but very precise info… Thanks for sharing this one.
A must read post!
I constantly emailed this webpage post page to all
my contacts, for the reason that if like to read it afterward my friends will too.
Hello, I enjoy reading all of your article post.
I like to write a little comment to support you.
Thank you!
Hi I am so delighted I found your blog, I really found you by mistake, while I was researching
on google for something else, Regardless I am here now and would just like to say thank
you for a incredible post and a all round enjoyable blog (I also love the theme/design), I don’t
have time to browse it all at the moment but I have book-marked it and also included your RSS feeds,
so when I have time I will be back to read a lot more, Please do keep up the superb jo.
What’s Taking place i’m new to this, I stumbled upon this I’ve discovered It positively useful and it has aided me out loads.
I am hoping to give a contribution & aid other users like its aided me.
Good job.
Thanks!
That is really fascinating, You are an overly professional blogger.
I have joined your feed and look forward to in the hunt for extra of your magnificent post.
Additionally, I have shared your site in my social networks
Thanks!
What’s up to all, how is the whole thing,
I think every one is getting more from this web page, and your views are nice designed for new people.
Spot on with this write-up, I really believe this amazing site needs a great deal more attention. I’ll probably be back again to see more, thanks for
the information!
Thanks to my father who stated to me on the topic of this weblog, this blog is in fact remarkable.
Pretty nice post. I just stumbled upon your weblog and wished to say that I have
really enjoyed surfing around your blog posts. After all I will be subscribing to your rss feed and I hope you
write again soon!
Nice post. I learn something totally new and challenging on websites I stumbleupon on a daily basis.
It’s always useful to read content from other writers and use something
from other sites.
What’s up, I read your blog like every week. Your story-telling style is awesome, keep up the good
work!
Thanks!
Heya this is kinda of off topic but I was wondering if blogs use WYSIWYG editors or if you have to manually code with HTML.
I’m starting a blog soon but have no coding experience so I wanted to get guidance from someone with experience.
Any help would be greatly appreciated!
All blogs use, as rule, WYSIWYG editors.
Hello There. I discovered your blog the usage of msn. This is a really well written article.
I’ll make sure to bookmark it and return to learn extra of your useful info.
Thanks for the post. I’ll definitely return.
You are welcome)
Woah! I’m really enjoying the template/theme of this blog.
It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appearance.
I must say that you’ve done a awesome job with this.
In addition, the blog loads very quick for me on Chrome. Superb Blog!
Thank you!
Appreciating the dedication you put into your site and detailed information you provide.
It’s nice to come across a blog every once in a while that isn’t the same unwanted rehashed material.
Wonderful read! I’ve saved your site and I’m including your RSS
feeds to my Google account.
You are welcome!
I really like what you guys are usually up too.
This type of clever work and coverage! Keep up the great works guys I’ve added you guys to blogroll.
Awesome! Its genuinely amazing post, I have got
much clear idea on the topic of from this piece of writing.
Welcome!
Hi there, just wanted to say, I loved this blog post. It
was helpful. Keep on posting!
Thank you!
whoah this blog is magnificent i really like reading your articles.
Keep up the good work! You realize, lots of persons
are searching round for this info, you could help them greatly.
Thank you!
My brother suggested I might like this web site.
He was entirely right. This post truly made my day.
You can not imagine simply how much time I had spent
for this information! Thanks!
Welcome!
It’s very simple to find out any topic on web as compared to textbooks, as I
found this paragraph at this website.
It’s hard to find well-informed people for this topic, however, you sound like
you know what you’re talking about! Thanks
Welcome!
Excellent goods from you, man. I have understand your stuff previous to and you’re just too magnificent.
I really like what you have acquired here, certainly like
what you are saying and the way in which you say it.
You make it entertaining and you still care for to keep it smart.
I can’t wait to read much more from you. This is
actually a tremendous site.
Thank you very much!
No matter if some one searches for his necessary thing,
so he/she wishes to be available that in detail, thus that thing is maintained over here.