WordPress random code snippets

This is a collection of general WordPress snippets to use. I’m not sure where most of this code originally came from. Most of it has just been copied from other ‘internet’ sources. I’ll attribute what I know.

Remove “Category:” from archive pages

This just removes the category: text, and just shows the name of the category instead.

function prefix_category_title( $title ) {
    if ( is_category() ) {
        $title = single_cat_title( '', false );
    }
    return $title;
}
add_filter( 'get_the_archive_title', 'prefix_category_title' );

Change JPEG compression amount

Just a simple snippet to change the default jpeg compression amount. Set the int to whatever percentage you deem fit.

function jpeg_quality_callback($arg) { return (int)70; } add_filter('jpeg_quality', 'jpeg_quality_callback');

Stop wordpress update emails

Turns off the default behavior of sending an email every time wordpress updates.

add_filter( 'auto_core_update_send_email', 'wpb_stop_auto_update_emails', 10, 4 ); function wpb_stop_update_emails( $send, $type, $core_update, $result ) {if ( ! empty( $type ) && $type == 'success' ) {return false;}return true;}
add_filter( 'auto_theme_update_send_email', '__return_false' );
add_filter( 'auto_plugin_update_send_email', '__return_false' );

Allow SVG files

Allows you to upload SVG files to your wordpress site. This is normally forbidden since SVG files can contain malicious code. If you control all of your uploads though it’s not much of an issue.

// Allow SVG
add_filter( 'wp_check_filetype_and_ext', function($data, $file, $filename, $mimes) {

  global $wp_version;
  if ( $wp_version !== '4.7.1' ) {
     return $data;
  }

  $filetype = wp_check_filetype( $filename, $mimes );

  return [
      'ext'             => $filetype['ext'],
      'type'            => $filetype['type'],
      'proper_filename' => $data['proper_filename']
  ];

}, 10, 4 );

function cc_mime_types( $mimes ){
  $mimes['svg'] = 'image/svg+xml';
  return $mimes;
}
add_filter( 'upload_mimes', 'cc_mime_types' );

function fix_svg() {
  echo '<style type="text/css">
        .attachment-266x266, .thumbnail img {
             width: 100% !important;
             height: auto !important;
        }
        </style>';
}
add_action( 'admin_head', 'fix_svg' );

Increase WordPress memory size

Increase the WordPress memory limit (distinct from PHP limit) by placing the code below, at the very top of the wp-config.php file, in the section that says to place all custom code between these two points towards the bottom…

/** Sets up WordPress memory limit */
define('WP_MEMORY_LIMIT', '64M');

Subscribe

SUBSCRIBE to get updates!

News on seasonal items, special markets and recipes. (Infrequent)

Related Posts