WordPress Snippets


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} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

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 ##

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' );
   }
 }

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;}

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


One response to “WordPress Snippets”

Leave a Reply

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