CodeHQ

Posts Tagged ‘Functions’

« Older Entries |

Responsive WordPress images using SRCSET and ACF

Tuesday, June 26th, 2018

Make sure your image field is set to an array in ACF and then use the below.

‘150px’ is the maximum width your image will ever display.

Click the link to view the code

Tags: , , , ,
Posted in PHP, WordPress | No Comments »

Grab first image from post

Monday, April 16th, 2018

Function for grabbing image from post and using it instead of a featured image for example.

Add this to functions.php

// Get URL of first image in a post
function catch_that_image() {
	global $post, $posts;
		$first_img = '';
		ob_start();
		ob_end_clean();
		$matches[1][0] = '';
		if(preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches)){
		  	$first_img = $matches[1][0];
		}

	// no image found display default image instead
	return $first_img;
}

And call in the theme with

<img src="<?php echo catch_that_image() ?>" onerror="this.style.display = 'none';"/> 

Tags: , , , , , ,
Posted in PHP, WordPress | No Comments »

Preparing for Gutenberg

Thursday, February 8th, 2018

If you want to pre-disable Gutenberg for when it launches, install the following plugin, activate and untick the checkbox in the settings: https://wordpress.org/plugins/classic-editor/

If you also want to avoid the 5.0 update, you can always hide the update banner:

//Add to functions.php file to remove WordPress update notice 

function hide_update_notice() {
remove_action( 'admin_notices', 'update_nag', 3 );
}
add_action( 'admin_notices', 'hide_update_notice', 1 );

Tags: , , ,
Posted in WordPress | No Comments »

Limit WordPress Revisions

Tuesday, January 9th, 2018

Add this to you wp-config.php file and change the number for how many revisions you want to limit your pages to:

define('WP_POST_REVISIONS', 10);

Or use the below to globally turn off post revisions:

define('WP_POST_REVISIONS', false);

Tags: , , , , , , , , ,
Posted in PHP, WordPress | No Comments »

jQuery in the footer

Thursday, December 1st, 2016

Add this to your functions.php

function my_init()   
{  
    if (!is_admin())   
    {  
        wp_deregister_script('jquery');  
  
        // Load a copy of jQuery from the Google API CDN  
        // The last parameter set to TRUE states that it should be loaded  
        // in the footer.  
        wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js', FALSE, '1.11.0', TRUE);  
  
        wp_enqueue_script('jquery');  
    }  
}  
add_action('init', 'my_init');  

Tags: , , , , ,
Posted in jQuery, PHP, WordPress | No Comments »

Remove script and style version numbers | WordPress

Wednesday, November 30th, 2016

Add this to functions.php

// Pick out the version number from scripts and styles
function remove_version_from_style_js( $src ) {
    if ( strpos( $src, 'ver=' . get_bloginfo( 'version' ) ) )
        $src = remove_query_arg( 'ver', $src );
    return $src;
}
add_filter( 'style_loader_src', 'remove_version_from_style_js');
add_filter( 'script_loader_src', 'remove_version_from_style_js');

Tags: , , , , , , ,
Posted in PHP, WordPress | No Comments »

Remove emoji code in header | WordPress

Thursday, October 6th, 2016

Add to functions.php

// REMOVE WP EMOJI
remove_action('wp_head', 'print_emoji_detection_script', 7);
remove_action('wp_print_styles', 'print_emoji_styles');

remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
remove_action( 'admin_print_styles', 'print_emoji_styles' );

Tags: , , , ,
Posted in PHP, WordPress | No Comments »

Change ellipses on excerpt | WordPress

Friday, September 30th, 2016

Add to functions.php file.

function ld_new_excerpt_more($more) {
    global $post;
    return '...';
}
add_filter('excerpt_more', 'ld_new_excerpt_more');

Tags: , , ,
Posted in PHP, WordPress | No Comments »

Add thumbnail theme support | WordPress

Friday, September 30th, 2016

Add this to the functions.php file.

add_theme_support( 'post-thumbnails' ); 

Tags: , , , ,
Posted in PHP, WordPress | No Comments »

Display taxonomy term name | WordPress

Thursday, August 18th, 2016

Tags: , , , , , , ,
Posted in PHP, WordPress | No Comments »

Next Page »