by
1,988 11
1
5
188k
7
Top 1% !
Pearl of Wisdom
Easy-to-find
Tuned
OpenSource
Popularity: 9009th place
BrowserChrome
BrowserFirefox
BrowserOpera
LanguagePHP
LicenseGNU General Public v2
OSLinux
OSMac OS X
OSUnix
OSWindows
Project TypeWordPress

WordPress safely defining and executing functions

Handy safe way to include functions in your WordPress Themes functions.php file. This is even more useful if you are using a child theme and don't want to conflict with functions already defined by the parent theme. Even better; These functions can be executed within the same if(function_exists()) block that defines them creating a neater to manage functions.php file.I also included some basic stuff you might find handy to have in your own WordPress themes functions.php file.
Copy Embed Code
<iframe id="embedFrame" style="width:600px; height:300px;"
src="https://www.snip2code.com/Embed/1446864/WordPress-safely-defining-and-executing-?startLine=0"></iframe>
Click on the embed code to copy it into your clipboard Width Height
Leave empty to retrieve all the content Start End
<?php // Lets get rid of the versioning on static .js and .css files. // It looks tacky anyway for static files. /** * Strips Version query strings * @param type $src * @return type */ if(!function_exists('remove_css_js_versions')){ // if the function doesnt exist then... function remove_css_js_versions($src){ // define the function return remove_query_arg('ver', $src); // call built-in wordpress function to remove query named "ver" from whatever is passed as $src } add_filter('script_loader_src', 'remove_css_js_versions'); // Pass a hook as $src for js to function listed above named "remove_css_js_versions" add_filter('style_loader_src', 'remove_css_js_versions'); // Pass a hook as $src for css to function listed above named "remove_css_js_versions" } // All scripts and styles will be filtered to have ?ver=1.2.3 stripped from urls. Function defined and executed in same if() statement. /** * Need to debug? Instead of just using print_r($array) you just do preprint_r($array) * the array will be output neatly instead of wrapped and messy. */ if(!function_exists('preprint_r')){ function preprint_r($val){ echo '<pre>' . PHP_EOL; print_r($val) . PHP_EOL; echo '</pre>' . PHP_EOL; } } /** Remove the WP Icon (Menu) from profile and admin pages. */ if(!function_exists('remove_wp_logo')){ function remove_wp_logo($wp_admin_bar){ $wp_admin_bar->remove_node('wp-logo'); } add_action('admin_bar_menu', 'remove_wp_logo', 33); } /** * Wordpress Login Page fixes. By default WordPress shows its logo on your login pages and links to its own site. * A visitor logging into your site don't need this potential roadblock so lets show WordPress that we mean business! */ # Fix Login Page WordPress URL if(!function_exists('fix_wp_login_img_url')){ function fix_wp_login_img_url($url){ return get_bloginfo('url'); } add_filter('login_headerurl', 'fix_wp_login_img_url'); } # Fix Login Page WordPress Title (Text displayed on hover defaults to Powered By WordPress) if(!function_exists('fix_wp_login_img_title')){ function fix_wp_login_img_title($title){ return get_bloginfo('description'); } add_filter('login_headertitle', 'fix_wp_login_img_title'); } /** Change Login Page WordPress Logo, You will need to update path and point to a valid image. WordPress adds this with a CSS background image for your anchor link. It first applys a PNG and then a SVG override if your browser is modern and supports svg In your themes custom stylesheet you can change default Wordpress image by supplying a new background image: .login h1 a { background-image: none,url(images/your_image.png); } Hint: WordPress uses an 64x64 image but you can adapt size to your need. You might want to use SVG like WordPress did because it will scale best at any resolution. */ /** * Simple Actions that use wp_head hook to cleanup WP header Junk/Bloat * that is included with WordPress by default. */ remove_action('wp_head', 'wp_generator'); remove_action('wp_head', 'rsd_link'); remove_action('wp_head', 'wlwmanifest_link'); remove_action('wp_head', 'wp_shortlink_wp_head'); ?>
If you want to be updated about similar snippets, Sign in and follow our Channels

blog comments powered by Disqus