- Joined
- Feb 2, 2015
- Messages
- 155
- Likes
- 142
- Degree
- 1
This is mostly directed at beginners with WP and not so great with PHP, but you might find some that you need anyways to copy and paste.
If you're not utilizing your functions.php you're probably editing a lot of theme files that makes it more a bitch for theme updates if you're using a child theme (hopefully you are).
Security
Some security additions to help hide the version of WP you're using and help lean it out more too for speed.
Tracking/Analytics
Add whatever tracking codes or additional scripts you need to the header, this will add it to the bottom of the file (so you probably want to just add JS, always put CSS before this stuff for page speed).
You can also hook it to the footer, but most of these tracking scripts are asynchronous and won't affect anything else being loaded, but you can place whatever you want here.
Speed
Note about these. Plugins you're adding to the site or in your purchased theme's layout you might be loading CSS/JSS that you don't need to use on every page or don't need at all. If you need to load these scripts on specific pages you can refer to this.
Replace WP Jquery with Google's CDN version.
Sets a limit for your post revisions to help stop so much database bloat.
Provided by @Ryuzaki here to remove added Emoji styles. These are asynchronous so they shouldn't affect speed, but it's just another footprint + added stuff we don't need so why not remove it.
This resizes images if they're too big for the Large media setting. The idea here is for client sites or your own you won't end up with these massive image files that can eat away at your memory.
Remove the default fields for user's that WP has: AIM, Jabber, YIM. These add database bloat.
This a random side one that I like, it just makes it so all the HTML tags are closed before the posts ends. If your clients are poking in the page or VA's this might save you some grief if pages are getting broken from editing in visual editor and breaking anything.
I'll try and post some more too.
If you're not utilizing your functions.php you're probably editing a lot of theme files that makes it more a bitch for theme updates if you're using a child theme (hopefully you are).
Security
Some security additions to help hide the version of WP you're using and help lean it out more too for speed.
Code:
//remove WP version string from header [security] [footprint]
remove_action('wp_head', 'wp_generator');
//hide login error messages (ex. "Wrong Password") [security] [footprint]
add_filter('login_errors',create_function('$a', "return null;"));
//remove admin name in comments class [security] [footprint]
function remove_comment_author_class( $classes ) {
foreach( $classes as $key => $class ) {
if(strstr($class, "comment-author-")) {
unset( $classes[$key] );
}
}
return $classes;
}
add_filter( 'comment_class' , 'remove_comment_author_class' );
//remove WP version param from any enqueued scripts [security] [footprint] [speed]
function remove_wpversion_cssjs( $src ) {
if ( strpos( $src, 'ver=' ) )
$src = remove_query_arg( 'ver', $src );
return $src;
}
add_filter( 'style_loader_src', 'remove_wpversion_cssjs', 999 );
add_filter( 'script_loader_src', 'remove_wpversion_cssjs', 999 );
//change URL string from Author to Writer [footprint]
function new_author_base() {
global $wp_rewrite;
$author_slug ='sellers';
$wp_rewrite->author_base = $author_slug;
}
add_action('init','new_author_base');
Tracking/Analytics
Add whatever tracking codes or additional scripts you need to the header, this will add it to the bottom of the file (so you probably want to just add JS, always put CSS before this stuff for page speed).
Code:
function add_trackingheader() {
?>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-YOURNUMBERSHERE-1', 'auto');
ga('send', 'pageview');
</script>
<meta name="msvalidate.01" content="YOURNUMBERSHERE" />
<?php
}
add_action( 'wp_head', 'add_trackingheader', 999 );
You can also hook it to the footer, but most of these tracking scripts are asynchronous and won't affect anything else being loaded, but you can place whatever you want here.
Code:
//add JS to footer
function add_js_to_footer() {
?>
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-YOURNUMBERSHERE-1', 'auto');
ga('send', 'pageview');
</script>
<meta name="msvalidate.01" content="YOURNUMBERSHERE" />
<?php
}
add_action( 'wp_footer', 'add_js_to_footer', 999 );
Speed
Note about these. Plugins you're adding to the site or in your purchased theme's layout you might be loading CSS/JSS that you don't need to use on every page or don't need at all. If you need to load these scripts on specific pages you can refer to this.
Code:
//remove CSS from header
add_action( 'wp_print_styles', 'deregister_my_styles', 100 );
function deregister_my_styles() {
wp_deregister_style( 'jetpack_css' );
}
Replace WP Jquery with Google's CDN version.
Code:
// even more smart jquery inclusion
add_action('init','jquery_register');
// register from google and for footerfunction jquery_register() {
if(!is_admin()) {
wp_deregister_script('jquery');
wp_register_script('jquery',('https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js'),false,null,true);
wp_enqueue_script('jquery');
}
}
Sets a limit for your post revisions to help stop so much database bloat.
Code:
//set limit for post revisions to 10
if(!defined('WP_POST_REVISIONS')) define('WP_POST_REVISIONS',10);
Provided by @Ryuzaki here to remove added Emoji styles. These are asynchronous so they shouldn't affect speed, but it's just another footprint + added stuff we don't need so why not remove it.
Code:
//remove emoji [speed] [footprint]
remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
remove_action( 'wp_print_styles', 'print_emoji_styles' );
This resizes images if they're too big for the Large media setting. The idea here is for client sites or your own you won't end up with these massive image files that can eat away at your memory.
Code:
//resize images that are too big to the Large media size setting [speed]
function replace_uploaded_image($image_data){// if there is no large image : returnif(!isset($image_data['sizes']['large']))return $image_data;
$upload_dir = wp_upload_dir();
$uploaded_image_location = $upload_dir['basedir'].'/'.$image_data['file'];
$large_image_location = $upload_dir['path'].'/'.$image_data['sizes']['large']['file'];
unlink($uploaded_image_location);
rename($large_image_location,$uploaded_image_location);
$image_data['width']= $image_data['sizes']['large']['width'];
$image_data['height']= $image_data['sizes']['large']['height'];
unset($image_data['sizes']['large']);
return $image_data;
}
add_filter('wp_generate_attachment_metadata','replace_uploaded_image');
Remove the default fields for user's that WP has: AIM, Jabber, YIM. These add database bloat.
Code:
function remove_default_userfields( $contactmethods ) {
unset($contactmethods['aim']);
unset($contactmethods['jabber']);
unset($contactmethods['yim']);
return $contactmethods;
}
add_filter('user_contactmethods','remove_default_userfields',10,1);
This a random side one that I like, it just makes it so all the HTML tags are closed before the posts ends. If your clients are poking in the page or VA's this might save you some grief if pages are getting broken from editing in visual editor and breaking anything.
Code:
//automatically clean up html wysiwyg editor by closing missing tags (ex. <p>, <a>, etc) [wp-admin]
function clean_bad_content($bPrint = false) {
global $post;
$szPostContent = $post->post_content;
$szRemoveFilter = array("~<p[^>]*>\s?</p>~", "~<a[^>]*>\s?</a>~", "~<font[^>]*>~", "~<\/font>~", "~style\=\"[^\"]*\"~", "~<span[^>]*>\s?</span>~");
$szPostContent = preg_replace($szRemoveFilter, '', $szPostContent);
$szPostContent = apply_filters('the_content', $szPostContent);
if ($bPrint == false) return $szPostContent;
else echo $szPostContent;
}