Working with WordPress on a daily basis I found or created many snippets that make maintenance easier, user experience better both for authors and visitors, or the website more flexible and secure. In the previous collection I shared six useful hacks and snippets for inserting ads, social sharing and security.
Today I’d like to share a few more of the simple tricks that will improve the security and user experience (both for visitors and staff) of your WordPress site.
Prevent WP from adding links to images
Add this piece of code to your theme’s functions.php file to disable the default linking when adding images. Note, this will not remove the existing links, just change the behavior when you add images.
1 2 3 4 5 6 7 8 |
function wpb_imagelink_setup() { $image_set = get_option( 'image_default_link_type' ); if ($image_set !== 'none') { update_option('image_default_link_type', 'none'); } } add_action('admin_init', 'wpb_imagelink_setup', 10); |
Via WP Beginner
Disable comments for media (images)
Spammers just love to exploit these and when neglected you can get a huge amount of junk in your database. Unfortunately, from the media menu you can’t edit settings for all items comments at once, so you’re left with the frustrating job of changing one by one.
Luckily, there’s also this piece of code that disables all media comments.
1 2 3 4 5 6 7 8 |
function filter_media_comment_status( $open, $post_id ) { $post = get_post( $post_id ); if( $post->post_type == 'attachment' ) { return false; } return $open; } add_filter( 'comments_open', 'filter_media_comment_status', 10 , 2 ); |
Via WpBeginner
Replace WP logo with your own on WP-login
This one is not that useful, but it’s cool, and if you build websites for clients, they’ll love it!
1 2 3 4 5 6 7 8 |
function change_loginlogo() { echo '<style type="text/css"> h1 a { background-image: url(' . get_template_directory_uri() . '/images/logo.png) !important; } </style>'; } add_action('login_head', 'change_loginlogo'); |
Via ElegantThemes
See also: Customizing the Login Form, How to Create a Stunning Custom WordPress Login Page
Add async and defer to javascript load
This little function is useful for enhancing user experience and SEO through reducing page loading time. It tells the javascripts to defer and load asynchronously, without stopping the HTML parsing.
1 2 3 4 5 6 |
function defer_parsing_of_js ( $url ) { if ( FALSE === strpos( $url, '.js' ) ) return $url; if ( strpos( $url, 'jquery.js' ) ) return $url; return "$url' async defer='defer"; } add_filter( 'clean_url', 'defer_parsing_of_js', 11, 1 ); |
Via Emoticode
Remove script and css versions and meta generator
These two functions make your WordPress installation a bit less obvious and, by hiding the version of your scripts and styles, a little harder for hackers to plan the attack.
1 2 3 4 5 6 7 8 9 10 |
function remove_script_version( $src ){ return remove_query_arg( 'ver', $src ); } add_filter( 'script_loader_src', 'remove_script_version', 15, 1 ); add_filter( 'style_loader_src', 'remove_script_version', 15, 1 ); function remove_generator() { return ''; } add_filter('the_generator', 'remove_generator'); |
Thanks for sharing, It work!