9 hacks and snippets to extend WordPress efficiency

It has been a really very long time that I’ve not printed a list of latest and large useful snippets and hacks to spice up your WordPress arrange and performace. So right now I’m utterly blissful to point you this nifty report of coding tips to make your WordPress weblog far more extremely efficient.

Shortcode to indicate exterior info

In case you would possibly wish to present exterior info with out posts or pages, proper right here’s a useful shortcode to help. The code beneath goes into your theme’s capabilities.php:

function show_file_func( $atts ) 
add_shortcode( 'show_file', 'show_file_func' );

And proper right here’s how you use it:

[show_file file=""]

Provide: Specky Boy

Take away featured image when deleting a submit

The code beneath will routinely delete the featured image associated to a submit, when the submit in question is deleted. Merely insert it into capabilities.php.

add_action( 'before_delete_post', 'wps_remove_attachment_with_post', 10 );
function wps_remove_attachment_with_post($post_id) 
        if(has_post_thumbnail( $post_id ))

Provide: WP Snipp

Set default fallback thumbnail for featured footage

This convenient code snippet permits you to define a default fallback image for posts the place a featured image isn’t outlined.

This code have to be pasted wherever you want the featured image or its fallback to be displayed, totally on index.php and single.php. Edit line 4 and alter the URL with the URL of your fallback image.

<?php if ( has_post_thumbnail() )  else  ?>

Provide: WP Expertz

Manually switch scripts and CSS info to the footer

To have the ability to optimize your website speed, you sometimes could have to switch scripts and stylesheet on the footer of your web site. This occasion choices how you are able to do it on a WordPress weblog, nevertheless any developer with slightly bit little bit of experience can modify the code to make it match any type of website online.

First, open your capabilities.php file and paste the subsequent code in it:

 * Filter HTML code and go away allowed/disallowed tags solely
 * @param string 	$textual content material 	Enter HTML code.
 * @param string 	$tags 	Filtered tags.
 * @param bool 		$invert Define whether or not or not must go away or take away tags.
 * @return string Filtered tags
function theme_strip_tags_content($textual content material, $tags = '', $invert = false) 

 * Generate script tags from given provide code
 * @param string $provide HTML code.
 * @return string Filtered HTML code with script tags solely
function theme_insert_js($provide) 

As quickly as completed, edit your header.php file. Substitute the wp_head() tag by this:

$themeHead = ob_get_contents();
define( 'HEAD_CONTENT', $themeHead );

$allowedTags = '<style><hyperlink><meta><title>';
print theme_strip_tags_content( HEAD_CONTENT, $allowedTags );

And finally, place the code beneath into your footer.php file, merely sooner than the closing </physique> tag.

<?php theme_insert_js( HEAD_CONTENT ); ?>

Credit score rating: Tomasz Dobrynski.

Solely allow submit creator to reply to a contact upon their submit

If for some goal you solely want to allow the submit creator to reply to suggestions, proper right here’s a working reply. As sometimes, it is best so as to add it to your theme’s capabilities.php file.

add_action( 'pre_comment_on_post', 'wpq_pre_commenting' );

function wpq_pre_commenting( $pid )  

Provide: GitHub

Present remaining modified date of a submit

When you substitute your current posts incessantly, it is sometimes further associated to indicate the date of modification pretty than the publishing date. Merely paste this code the place you want the date to be displayed. The function have to be used contained in the loop.

<p>Ultimate modified: <?php the_modified_date(); ?></p>

Provide: WordPress Codex

Add Theme Editor shortcut to admin bar

For many who sometimes make modifications to your theme by using the built-in editor, it might be helpful in order so as to add a shortcut to WordPress’ admin bar. Merely paste the code beneath in your capabilities.php file to proceed.

function admin_bar_theme_editor_option() 
add_action( 'admin_bar_menu', 'admin_bar_theme_editor_option', 100 );

Provide: WP Snippet

Schedule cron jobs with WordPress

Cron is a time-based job scheduler in Unix-like computer working strategies. It could be used inside WordPress to schedule events, for example emptying your spam comment queue.
Proper right here’s a main code snippet to stay in your capabilities.php file, that may allow you to create a scheduled event.

add_action('my_hourly_event', 'do_this_hourly');

function my_activation() 
add_action('wp', 'my_activation');

function do_this_hourly() 
	// do one factor every hour


Provide: WP Snippets