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.

Most popular 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:

  1. https://gist.github.com/rniswonger/ee1b30e5fd3693bb5f92fbcfabe1654d
  2. https://www.wpbeginner.com/wp-tutorials/how-to-track-popular-posts-by-views-in-wordpress-without-a-plugin/

Leave Comment

55 Replies to “Most popular WordPress functions”

Your email address will not be published. Required fields are marked *