[tutorial] Chris’s custom optimizations for functions.php, etc.

Posted in: Newsmag
Post count: 9544

HI, folks
I thought I’d put this in ONE place (it’s buried in the pagespeed sticky topic on the newspaper forum, but a lot to shovel through there).

This is some of the custom code I put at bottom of my functions.php file EVERY time the theme is updated, and for ALL our websites, regardless of theme (with some new code for Visual Composer in this iteration).

Basically this code helps with various things – some of which are needlessly often done in plugins and don’t need to be – including things for PageSpeed rating, and general optimizations such as default image compression level for thumbnails, etc.

Use if you wish; but please *do not* go post this in a blog article as if it’s your idea or I’ll come and punch you 🙂

*NOTE:
this also includes code you might not need
a) remove visual composer CSS from post pages
b) remove pingback URL
c) change default WP thumb compression from 90 to 50 (some like 60)
d) remove yoast admin bar (annoying)
e) add go away message for people trying to login guessing passwords; great to use with limit login attempts
f) delete enclosure in RSS feed
g) disallow some things in comments for security

AND MY MAGIC BUNNY CODE:

/*CHRISTOPHER's CUSTOM MODS - 1.10.15 rev15A - (c) 2010-2015 Chris Simmons */

 remove_action( 'wp_head', 'wp_generator' ) ;
 remove_action( 'wp_head', 'wlwmanifest_link' ) ;
 remove_action( 'wp_head', 'rsd_link' ) ;
 remove_action( 'wp_head', 'feed_links', 2 );
 remove_action( 'wp_head', 'feed_links_extra', 3 );

 add_filter( 'pre_comment_content', 'wp_specialchars' );

 function no_errors_please(){
   return 'You appear to be up to no good. Please stop now!';
 }
 add_filter( 'login_errors', 'no_errors_please' );

 /* Disable YOAST SEO Admin Bar. */
 function mytheme_admin_bar_render() {
 	global $wp_admin_bar;
 	$wp_admin_bar->remove_menu('wpseo-menu');
 }
 // and we hook our function via
 add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );

 function delete_enclosure(){
  return '';
  }
  add_filter( 'get_enclosed', 'delete_enclosure' );
  add_filter( 'rss_enclosure', 'delete_enclosure' );
  add_filter( 'atom_enclosure', 'delete_enclosure' );

 function remove_cssjs_ver( $src ) {
     if( strpos( $src, '?ver=' ) )
         $src = remove_query_arg( 'ver', $src );
     return $src;
 }
 add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
 add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );

 add_filter( 'jpeg_quality', create_function( '', 'return 50;' ) );

 function remove_pingback_url( $output, $show ) {
     if ( $show == 'pingback_url' ) $output = '';
     return $output;
 }
 add_filter( 'bloginfo_url', 'remove_pingback_url', 10, 2 );

 add_action('init', 'myoverride', 100);
 function myoverride() {
     remove_action('wp_head', array(visual_composer(), 'addMetaData'));
}

function dequeue_visual_composer_css() {
if (is_single()) {
    wp_dequeue_style('js_composer_front');
}
}
add_action('wp_enqueue_scripts', 'dequeue_visual_composer_css', 1003);

Note:
last item removes Visual Composer CSS from “post” pages (single). You would not use that if you use VC for anything on posts, obviously.

  • This topic was modified 11 years by simchris.
  • This topic was modified 10 years by Radu.
Post count: 9544

For those using mod_pagespeed on Apache or other server type, here are my custom “works best with WordPress” settings which I add to my sites in the .htaccess file, at top, above the WordPress rewrite module stuff.

You generally do NOT want to use the javacript modification stuff with WordPress.
This is compatible with both Newspaper and Newsmag, and with TagDiv Speedbooster plugin.

<IfModule pagespeed_module>
ModPagespeed on
ModPagespeedEnableFilters collapse_whitespace
ModPagespeedEnableFilters remove_comments
ModPagespeedEnableFilters rewrite_images
ModPagespeedEnableFilters insert_dns_prefetch
</IfModule>
  • This reply was modified 11 years by simchris.
Post count: 9544

Additional .htaccess optimizations for Apache servers.


# END WordPress

AddType text/javascript .js
AddType application/vnd.ms-fontobject .eot
AddType application/x-font-ttf .ttf
AddType application/x-font-opentype .otf
AddType application/x-font-woff .woff

# BEGIN Expire headers
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 5 seconds"
ExpiresByType image/x-icon "access plus 2500000 seconds"
ExpiresByType image/jpeg "access plus 2500000 seconds"
ExpiresByType image/png "access plus 2500000 seconds"
ExpiresByType image/gif "access plus 2500000 seconds"
ExpiresByType application/x-shockwave-flash "access plus 2500000 seconds"
ExpiresByType text/css "access plus 700000 seconds"
ExpiresByType text/javascript "access plus 700000 seconds"
ExpiresByType application/javascript "access plus 700000 seconds"
ExpiresByType application/x-javascript "access plus 700000 seconds"
ExpiresByType text/html "access plus 600 seconds"
ExpiresByType application/xhtml+xml "access plus 600 seconds"
ExpiresByType font/truetype "access plus 2500000 seconds"
ExpiresByType font/opentype "access plus 2500000 seconds"
ExpiresByType application/x-font-woff "access plus 2500000 seconds"
ExpiresByType image/svg+xml "access plus 2500000 seconds"
ExpiresByType application/vnd.ms-fontobject "access plus 2500000 seconds"
</IfModule>
# END Expire headers

# BEGIN Cache-Control Headers
<IfModule mod_headers.c>
<filesMatch "\.(ico|jpe?g|png|gif|swf)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch "\.(css)$">
Header set Cache-Control "public"
</filesMatch>
<filesMatch "\.(js)$">
Header set Cache-Control "private"
</filesMatch>
<filesMatch "\.(x?html?|php)$">
Header set Cache-Control "private, must-revalidate"
</filesMatch>
</IfModule>
# END Cache-Control Headers

<IfModule mod_headers.c>
    Header unset ETag
</IfModule>
<IfModule mod_alias.c>
RedirectMatch 403 /xmlrpc.php
</IfModule>

All of this “obviously” goes AFTER the WordPress elements generated when WP is setup.

  • This reply was modified 11 years by simchris.
Post count: 9544

Customization/minification to wp-config.php file to optimize for fewer dbase calls.

a) remove all the excess comments

e.g., stuff like this does NOT need to be in file; which are “comments”


 *
 * This file has the following configurations: MySQL settings, Table Prefix,
 * Secret Keys, WordPress Language, and ABSPATH. You can find more information by
 * visiting {@link http://codex.wordpress.org/Editing_wp-config.php Editing
 * wp-config.php} Codex page. You can get the MySQL settings from your web host.
 *
 * This file is used by the wp-config.php creation script during the
 * installation. You don't have to use the web site, you can just copy this file
 * to "wp-config.php" and fill in the values.
 *

// ** MySQL settings - You can get this info from your web host ** //
/** The name of the database for WordPress */
/** MySQL database password */
/** The Database Collate type. Dont change this if in doubt. */

/**
 * WordPress Database Table prefix.
 *
 * You can have multiple installations in one database if you give each a unique
 * prefix. Only numbers, letters, and underscores please!
 */

/* Thats all, stop editing! Happy blogging. */

Add the following at bottom and put in your actual site URL

define('WP_HOME', 'http://site.com');
define('WP_SITEURL', 'http://site.com');

if your site runs as https, be sure to use https!

** ADVANCED USERS ONLY ON FINAL SITE DEPLOY **
For additional optimization, you can add the following:
(final line changes default folder from ‘uploads’ to something else; such as images; in my case ‘meta’)

define('TEMPLATEPATH', '/var/www/vhosts/site.com/httpdocs/guru/wp-content/themes/Newsmag');
define('STYLESHEETPATH', '/var/www/vhosts/site.com/httpdocs/guru/wp-content/themes/Newsmag');
define(UPLOADS, 'meta');

  • This reply was modified 11 years by simchris.
  • This reply was modified 11 years by simchris.
Post count: 9544

*PLEASE TO NOT POST QUESTIONS HERE, AS I WON’T ANSWER*
*ADMIN: make a sticky and lock from posts if you wish*

All of the above is from a forthcoming ebook I’m writing on how to optimize WordPress for speed, SEO, and raise visibility online.

DO NOT REPOST THE ABOVE, as I’ll be very unhappy with you.
The ‘left shark’ will hunt you down (kidding) 😉

Hope that all helps somebody.

My karmic debt is hereby paid for the month!

Post count: 9544

When running PageSpeed testing if you keep running into the issue with external Gravatars loading from gravatar.com, copy the images from your website when loaded, and then upload to your media folder, or other folder.

Use this plugin:
https://wordpress.org/plugins/simple-local-avatars/

this will allow you to load all the author photos locally and *not* from gravatar.com, and it works with both the author box on posts, author page, and the author widget in sidebar.

Post count: 9544

Manual minification of the theme style.css file

(e.g., /wp-content/themes/Newsmag/style.css)

Open style.css in a text editor, such as TextPad on Windows, BBEdit on Mac, or other pure “text editor” – notepad on windows works (NOT WordPad!!)

Go to http://cssminifier.com/

1) in your text editor, select all in the style.css file (CNTRL+A), and copy (right click copy, or CTRL+C)
2) paste into the left box on above website (right click paste or CTRL+V), click minify
3) copy all from right box
4) paste back into your style.css file on top (over/replace) — it should now look line one long string of code, not all the line breaks. This is what “minification” does; it removes spaces and line breaks (returns).

Optionally, make new text file called style.css and upload over the original (assuming you’ve made a backup). With a good text editor, you can also save with proper line breaks (for Unix servers, for example you should save stuff with “unix line breaks” and not “windows line breaks”) — hence something like TextPad 5 or BBEdit are good choices for this kind of stuff. Other freeware “programmer editor” software works, too.

This is how you manually minify the main style.css file. Must be done after every theme update; but literally takes less than 5 minutes.

You would need to replace the original style.css file either via FTP, or using a file manager in your hosting panel.

Post count: 9544

When using YouTube videos, use the “long URL” vs the shortcode; this is because it’s a “redirect” and so when embedding into a post via OEMBED (how WordPress does it), you actually have a redirect for video to load (!). This now shows up as error/to fix item for PageSpeed.

So, instead of using
http://youtu.be/cjB-2_H7y2Q
you would actually embed the long version in post as
https://www.youtube.com/watch?v=cjB-2_H7y2Q

seems stupid, I know. But, technically the browser has to go fetch that to load on your page, and it *is* a redirect from one place to another, which slows things down.

Post count: 60

Thanks for this, disabled my CDN and implemented all of these, runs a lot faster now

Cheers

Send me a email sometime we should talk about things.

davis@urbanmusichq.com

Post count: 74

Awesome, thanks Chris

Post count: 322

Damn, Chris. You’ve been a busy boy. Nice mods and a great sticky-to-be. Thanks.

Post count: 1

After these modifications should continue using a cache plugin ? But someone had already modified the code to my site in a similar way and said I would not need a plugin cache , then my hosting is full

Post count: 9544

Additional important suggestion for site optimization

CLEAN HOUSE ON PLUGINS
1) REMOVE all un-used plugins. Not using revolution slider? Get rid of it. Not using animated GIF image capture? Ditch it.

CLEAN HOUSE ON THEMES
2) REMOVE all old themes. Moved from another theme, and know you don’t want to go back; save/download old theme folder; remove from server. Remove any other test folders. One site of a friend I looked at had 8 old theme folders going back to 2009, including one with an insecure version of Timthumb (pre fix). Still have the ooooold WP default, and twenty-ten themes ? Ditch em. Keep *one* of the stock themes (e.g., twenty-fourteen) and remove the rest.

All that crap can slow down site, but also potentially compromise it, if an old theme has some vulnerable code in it.

Post count: 9544

Again: don’t ask questions here! Post a new thread for support. I won’t answer any question posted to this thread !!!!

Post count: 2

Hi,

I am facing an issue. I want to create a page named sitemap. As it was already existed in my old theme but now in current version it redirect me to 404 page. How I can fix this issue?

Post count: 9544

As stated don’t post questions in this thread, please.

Post count: 115

Crhis
I have NEWSMAG installed

Is allowed put this code to works fine?

/*CHRISTOPHER’s CUSTOM MODS – 1.10.15 rev15A – (c) 2010-2015 Chris Simmons */

remove_action( ‘wp_head’, ‘wp_generator’ ) ;
remove_action( ‘wp_head’, ‘wlwmanifest_link’ ) ;
remove_action( ‘wp_head’, ‘rsd_link’ ) ;
remove_action( ‘wp_head’, ‘feed_links’, 2 );
remove_action( ‘wp_head’, ‘feed_links_extra’, 3 );

add_filter( ‘pre_comment_content’, ‘wp_specialchars’ );

function no_errors_please(){
return ‘You appear to be up to no good. Please stop now!’;
}
add_filter( ‘login_errors’, ‘no_errors_please’ );

/* Disable YOAST SEO Admin Bar. */
function mytheme_admin_bar_render() {
global $wp_admin_bar;
$wp_admin_bar->remove_menu(‘wpseo-menu’);
}
// and we hook our function via
add_action( ‘wp_before_admin_bar_render’, ‘mytheme_admin_bar_render’ );

function delete_enclosure(){
return ”;
}
add_filter( ‘get_enclosed’, ‘delete_enclosure’ );
add_filter( ‘rss_enclosure’, ‘delete_enclosure’ );
add_filter( ‘atom_enclosure’, ‘delete_enclosure’ );

function remove_cssjs_ver( $src ) {
if( strpos( $src, ‘?ver=’ ) )
$src = remove_query_arg( ‘ver’, $src );
return $src;
}
add_filter( ‘style_loader_src’, ‘remove_cssjs_ver’, 10, 2 );
add_filter( ‘script_loader_src’, ‘remove_cssjs_ver’, 10, 2 );

add_filter( ‘jpeg_quality’, create_function( ”, ‘return 50;’ ) );

function remove_pingback_url( $output, $show ) {
if ( $show == ‘pingback_url’ ) $output = ”;
return $output;
}
add_filter( ‘bloginfo_url’, ‘remove_pingback_url’, 10, 2 );

add_action(‘init’, ‘myoverride’, 100);
function myoverride() {
remove_action(‘wp_head’, array(visual_composer(), ‘addMetaData’));
}

function dequeue_visual_composer_css() {
if (is_single()) {
wp_dequeue_style(‘js_composer_front’);
}
}
add_action(‘wp_enqueue_scripts’, ‘dequeue_visual_composer_css’, 1003);

Post count: 9544

Everyone is welcome to use any or all of that, as fits your needs.
Just don’t post that on the web somewhere as “your invention” is all I’m asking 🙂

Post count: 9544

One more possible mod for the functions.php file.

If you are *not* using the next / previous story options, or/and also want to ensure that is removed from your header meta for some reason, you can use this in functions.php to remove the next/prev from the header data:

remove_action(‘wp_head’, ‘adjacent_posts_rel_link_wp_head’, 10, 0 );

I personally remove those since they don’t serve a purpose for most things. I think Yoast SEO has this as option, but why rely on a plugin to do something you can do yourself 🙂

Post count: 9544

well… and actually that didn’t work in something I was doing …
`
THIS IS CORRECT ONE (sorry):
remove_action('wp_head', 'start_post_rel_link', 10, 0 );
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

  • This reply was modified 11 years by simchris.
Post count: 67

you can add this too 😉

function no_wordpress_errors(){
return ‘GET OFF MY LAWN !! RIGHT NOW !!’;
}

Post count: 9544

I preferred mine, but same idea 🙂

function no_errors_please(){
return ‘You appear to be up to no good. Please stop now!';
}
add_filter( ‘login_errors’, ‘no_errors_please’ );

Some people might not get what “get off my lawn” means 😉

Post count: 1

How To Adjust The Width Of The Entire Theme (NewsPaper Theme)

Post count: 9544

@djless
wrong topic. Please post a new topic.

Post count: 9544

ADVANCED USERS CODE CORRECTION:
for advanced users, you likely already caught the missing single quotes, but this is the correct syntax for changing default uploads directory to root folder, something other than uploads

define('UPLOADS', 'images');

See: http://codex.wordpress.org/Editing_wp-config.php

——————-

Moving uploads folder

Set UPLOADS to :
define( 'UPLOADS', 'blog/wp-content/uploads' );

This path can not be absolute. It is always relative to ABSPATH, therefore does not require a leading slash.

  • This reply was modified 11 years by simchris.
  • This reply was modified 11 years by simchris.
Viewing 25 posts - 1 through 25 (of 182 total)
You must be logged in to reply to this topic.