8 Fresh Handy WordPress Code Snippets For Your Blog

Advertisement

WordPress is the most functional CMS(content management system) and it is completely free to use.Wordpress have brought revolution in web design and web development industry.Wordpress has many advantges and provides many reasons to choose as a CMS system like flexibility,functionality,themes,plugins SEO and etc.Although WordPress comes with many features you can easily play with its codes and make it more dynamic and useful for your needs.Changing or adding a piece of code;especially in functions.php file can increase the functionality of your wordpress blog.

In today’s post we have gathered some new WordPress code snippets which may be useful for your blog.Please do not forget to backup your WordPress files especially functions.php file.

Remove Login Shake

If you want to remove screen shake when you enter wrong password add the below code snippet in your functions.php file

<?php
function my_login_head() {
	remove_action('login_head', 'wp_shake_js', 12);
}
add_action('login_head', 'my_login_head');
?>

Source

 

Load jQuery in footer

By default WordPress has registered the jQuery library to load in the header.This snippet changes this behaviour.Add the below code in your functions.php file

<?php
/**
 * Prints jQuery in footer on front-end.
 */
function ds_print_jquery_in_footer( &$scripts) {
	if ( ! is_admin() )
		$scripts->add_data( 'jquery', 'group', 1 );
}
add_action( 'wp_default_scripts', 'ds_print_jquery_in_footer' );

?>

Source

 

Exclude pages from admin edit pages list

Add this snippet in your functions.php file to exclude pages from admin edit pages list.This snippet only hides the page from view

add_action( 'pre_get_posts' ,'exclude_this_page' );
function exclude_this_page( $query ) {
        if( !is_admin() )
                return $query;
        global $pagenow;
        if( 'edit.php' == $pagenow && ( get_query_var('post_type') && 'page' == get_query_var('post_type') ) )
                $query->set( 'post__not_in', array(23,28,30) ); // page id
        return $query;
}

Source

 

Force specific pages to be secure, ssl, https

Using this snippet you can force your specific pages to be secure.Add the following code in your functions.php file specify the specific post or page ID.

function wps_force_ssl( $force_ssl, $post_id = 0, $url = '' ) {
    if ( $post_id == 25 ) {
        return true
    }
    return $force_ssl;
}
add_filter('force_ssl' , 'wps_force_ssl', 10, 3);

Source

 

Add a Copyright Field to the Media Uploader

If you upload something using Media Uploader and want to add a Copyright field for only that uploaded item then add the following code in your functions.php file.

/**
 * Adding a "Copyright" field to the media uploader $form_fields array
 *
 * @param array $form_fields
 * @param object $post
 *
 * @return array
 */
function add_copyright_field_to_media_uploader( $form_fields, $post ) {
	$form_fields['copyright_field'] = array(
		'label' => __('Copyright'),
		'value' => get_post_meta( $post->ID, '_custom_copyright', true ),
		'helps' => 'Set a copyright credit for the attachment'
	);
 
	return $form_fields;
}
add_filter( 'attachment_fields_to_edit', 'add_copyright_field_to_media_uploader', null, 2 );
 
/**
 * Save our new "Copyright" field
 *
 * @param object $post
 * @param object $attachment
 *
 * @return array
 */
function add_copyright_field_to_media_uploader_save( $post, $attachment ) {
	if ( ! empty( $attachment['copyright_field'] ) ) 
		update_post_meta( $post['ID'], '_custom_copyright', $attachment['copyright_field'] );
	else
		delete_post_meta( $post['ID'], '_custom_copyright' );
 
	return $post;
}
add_filter( 'attachment_fields_to_save', 'add_copyright_field_to_media_uploader_save', null, 2 );
 
/**
 * Display our new "Copyright" field
 *
 * @param int $attachment_id
 *
 * @return array
 */
function get_featured_image_copyright( $attachment_id = null ) {
	$attachment_id = ( empty( $attachment_id ) ) ? get_post_thumbnail_id() : (int) $attachment_id;
 
	if ( $attachment_id )
		return get_post_meta( $attachment_id, '_custom_copyright', true );
 
}

Source

 

WordPress JPEG Thumbnail Image Quality Adjustment

If you want to set the image quality of the thumbnails from the “the_post_thumbnail” function/template tag add the following line to your functions.php file to override the default value of 90%:

add_filter('jpeg_quality', function($arg){return 100;});

Source

 

Secure your WordPress blog uploads directory

If you want to secure your uploads and only accept specific file extensions then add the following code in your .htaccess file.If you want to add more file extensions then just add other media types.

# secure uploads directory
<Files ~ ".*\..*">
	Order Allow,Deny
	Deny from all
</Files>
<FilesMatch "\.(jpg|jpeg|jpe|gif|png|tif|tiff)$">
	Order Deny,Allow
	Allow from all
</FilesMatch>

Source

 

Allow SVG through WordPress Media Uploader

If you don’t want your SVg files rejected then add the following piece of code in your functions.php file.

function cc_mime_types( $mimes ){
	$mimes['svg'] = 'image/svg+xml';
	return $mimes;
}
add_filter( 'upload_mimes', 'cc_mime_types' );

Source

2 Comments

    • Arshad Cini