Looking for useful code snippets for WordPress?

You can expand the functionality of your WordPress site by adding custom code to your theme. Code snippets for WordPress are used to do certain actions that might otherwise require a dedicated smaller plugin.

Please note that adding code functions incorrectly to your functions.php file could break your website. If you are not experienced in coding you should use a plugin like Code Snippets.

WordPress

WordPress Snippets

Remove Admin Bar For Subscribers

For those of you who have subscribers on your WordPress website and don’t want them to see the admin bar when they are logged in, add this code to your functions.php file or functionality plugin:

add_action('set_current_user', 'ed_hide_admin_bar');
function ed_hide_admin_bar() {
  if (!current_user_can('edit_posts')) {
    show_admin_bar(false);
  }
}

do_shortcode

To add a shortcode from within a template instead of with the content of a Post/Page, you can add the following in your template:

<?php echo do_shortcode("[add-your-shortcode-here]"); ?>

[Year] Shortcode

Create your own shortcode! If you want to add an automatic year entry into your theme, like you would in your footer for current year, add the following:

function year_shortcode() {
  $year = date('Y');
  return $year;
}
add_shortcode('year', 'year_shortcode');

Turn on WordPress Error Reporting

For local development and testing you can add the following to your wp-config.php file to get more detailed error reporting from your WordPress site.

define('WP_DEBUG', true);
define('WP_DEBUG_LOG', true);
define('WP_DEBUG_DISPLAY', false);
@ini_set('display_errors', 0);

Increase Memory Limit

Is your website taking a long time to load? Do you have lots of plugins or a lot of traffic? For this reason you can increase your memory limit  by adding the following to your wp-config file:

define('WP_MEMORY_LIMIT', '256M');

Force HTTPS

Add the following code to your .htaccess file to force your website to load on https and not http:

RewriteEngine On 
RewriteCond %{HTTPS} off 
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Leverage Browser Caching

This is a great snippet for speeding up your website. Paste it into your .htaccess file:

## EXPIRES CACHING ##
ExpiresActive On ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 2 days"
## EXPIRES CACHING ##

Upload SVG files

There’s no SVG support in WordPress by default so if you need to upload an svg file add this code to your functions.php file or functionality plugin:


function add_file_types_to_uploads($file_types){
$new_filetypes = array();
$new_filetypes[‘svg’] = ‘image/svg+xml’;
$file_types = array_merge($file_types, $new_filetypes );
return $file_types;
}
add_filter(‘upload_mimes’, ‘add_file_types_to_uploads’);

Plugin Snippets

Contact Form 7 missing calendar & spinbox

If your date calendar select and spinbox UI for numbers is missing from your form you can add the jQuery UI-based fallback feature. Add the following code into your theme:

add_filter( 'wpcf7_support_html5_fallback', '__return_true' );

WooCommerce remove “Add to Cart” Button

To remove the “Add to Cart” button on your shop and category pages, you can add this code:

add_action( 'woocommerce_after_shop_loop_item', 'remove_add_to_cart_buttons', 1 );
 function remove_add_to_cart_buttons() {
   if( is_product_category() || is_shop()) {
   remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart' );
   }
 }

WooCommerce change number of products that are displayed per page (shop page)

add_filter( 'loop_shop_per_page', 'new_loop_shop_per_page', 20 );

function new_loop_shop_per_page( $cols ) {
  // $cols contains the current number of products per page based on the value stored on Options –> Reading
  // Return the number of products you wanna show per page.
  $cols = 9;
  return $cols;
}

CSS Snippets

Vertically align text in div

Add the following to your css file.  Remember to replace .yourclass

.yourclass {display: flex; align-items: center;}

Remove the Google reCaptcha v3 badge

.grecaptcha-badge {display: none;}

Prevent Text Selection and Copy/Paste

* {
  -webkit-touch-callout: none; /* iOS Safari */
  -webkit-user-select: none; /* Safari */
  -khtml-user-select: none; /* Konqueror HTML */
  -moz-user-select: none; /* Old versions of Firefox */
  -ms-user-select: none; /* Internet Explorer/Edge */
   user-select: none; /* Non-prefixed version, currently supported by Chrome, Opera and Firefox */
}

Contact me today if you need help customising your WordPress website.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.