Level up your Wordpress CMS
Some handy code snippets for your functions.php
09.11.10
We’ve been making WordPress websites for a long time, and along we way we have picked up some handy tricks that can help streamline your WordPress CMS and get rid of some of the junk you don’t need.
Fix SSL on Attachment URLs
Source: StackExchange
function fix_attachment_url( $url ) {
if (is_ssl()){
$url = str_replace( 'http://', 'https://', $url );
}
return $url;
}
add_filter('wp_get_attachment_url', 'fix_attachment_url');
Disable Pingbacks
Source: Sucuri
add_filter('xmlrpc_methods', function($methods) {
unset($methods['pingback.ping']);
return $methods;
});
Custom Maintenance Mode
function maintenance_redirect(){
if($_SERVER["REMOTE_ADDR"] != 'ipaddress'){
wp_redirect(site_url('maintenance.html'), 302);
exit();
}
}
add_action('init', 'maintenance_redirect');
Hidden Content via Shortcode
function visitor_check_shortcode($atts, $content = null) {
if ((!is_user_logged_in() && !is_null($content)) || is_feed()){
return $content;
}
return '';
}
add_shortcode('visitor', 'visitor_check_shortcode');
Search Posts Only
function post_only_search($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts', 'post_only_search');
Adjust JPEG Quality
Source: Graph Paper Press
function custom_jpeg_quality($arg) {
return (int)100; // change 100 to whatever you prefer, but don't go below 6
}
add_filter('jpeg_quality', 'custom_jpeg_quality');
Add Excerpt to Pages
if (function_exists('add_post_type_support')) {
add_action('init', 'add_page_excerpts');
function add_page_excerpts(){
add_post_type_support('page', 'excerpt' );
}
}
Custom Admin Footer
function custom_admin_footer() {
echo 'By <a href="https://thriveweb.com.au/" title="Web Design & Development by Thrive Digital">Thrive Digital</a>';
}
add_filter('admin_footer_text', 'custom_admin_footer');
Custom Login Logo
function custom_login_logo() {
echo 'h1 a {background-image:url('.get_bloginfo('template_directory').'https://d14pjowmeoxkey.cloudfront.net/images/logo.png) !important;}';
}
add_action('login_head', 'custom_login_logo');
function custom_login_headerurl() {
return home_url('/');
}
add_filter('login_headerurl', 'custom_login_headerurl');
Hide Themes Menu Item
function remove_theme_menus() {
global $submenu;
unset($submenu['themes.php'][5]);
unset($submenu['themes.php'][15]);
}
add_action('admin_init', 'remove_theme_menus');
Add Custom Admin Favicon
function admin_favicon() {
echo '';
}
add_action('admin_head', 'admin_favicon');
Remove Unwanted Menu Items
function remove_menu_items() {
remove_menu_page('tools.php'); // Tools
remove_menu_page('edit-comments.php'); // Comments
remove_menu_page('edit.php'); // Posts
}
add_action('admin_menu', 'remove_menu_items', 100);
Remove Admin Bar
function hide_admin_bar() {
return 0;
}
add_filter('show_admin_bar', 'hide_admin_bar');
Limit Max Upload Size
Source: StackExchange
add_filter('wp_handle_upload_prefilter', function($file) {
$size = $file['size'];
if ($size > 500 * 1024) {
$file['error'] = '"Files larger than X bytes are prevented from uploads.';
}
return $file;
});
Set Default Colour Scheme
function set_user_colour_scheme($color_scheme) {
$color_scheme = 'modern';
return $color_scheme;
}
add_filter('get_user_option_admin_color', 'set_user_colour_scheme', 5);
Add SVG Support
function add_svg_support($mimes){
$mimes['svg'] = 'image/svg+xml';
return $mimes;
}
add_filter('upload_mimes', 'add_svg_support');
Did you find any of these snippets useful or have some tips of your own? We would love to hear from you!
Join the conversation
Leave a Reply
You must be logged in to post a comment.
Keep Reading
We think you may like these
Free SVG QR Code Generator
No need to need to sign up, give away your email, or even pay just to download your QR code in a usable format like SVG.
Thanks for the great read, Glad to see all of these culminated on one post. great reference, Will definitely utilize on my client sites.
Thanks mate, even when that’s not difficult I really forget how to put the codes very often and I make silly mistakes many times so thank god you helped me with this article!