Minify CSS and JavaScript in WordPress Using PHP Functions

How to minify css and javascript in wordpress; Minify css js using php wordpress; Best wordpress plugin for minifying css and js; Minify css javascript wordpress php function; Wordpress minify css js tutorial; Minify css js wordpress without plugin; Php script to minify css and js in wordpress; Wordpress minify css js manually; Optimize wordpress by minifying css and js; Minify css and javascript wordpress guide;

Explanation

To make your WordPress site load faster, you can minify your CSS and JavaScript files. Minifying means removing unnecessary spaces, comments, and characters from your code, which reduces file size and speeds up loading times.

Here's how it works:

  • Minify CSS: The function removes comments and extra spaces from your CSS file, making it smaller and quicker to load.
  • Minify JavaScript: Similarly, this function cleans up your JavaScript file by removing unnecessary parts, ensuring it loads faster.

How to Use:

  • The code reads your theme's CSS and JavaScript files.
  • It then applies the minification functions to these files.
  • Finally, it enqueues the minified versions, meaning it tells WordPress to use these optimized files instead of the original ones.

This approach is a manual way to optimize your site without using a plugin. It's a great option if you want more control over your site's performance.

Code

<?php
// Function to minify CSS
function wp_dudecom_minify_css($css) {
    // Remove comments, whitespace, and unnecessary characters
    $css = preg_replace('/\/\*.*?\*\//s', '', $css);
    $css = preg_replace('/\s*([{}|:;,])\s+/', '$1', $css);
    $css = preg_replace('/\s\s+(.*)/', '$1', $css);
    $css = str_replace(';}', '}', $css);
    return trim($css);
}

// Function to minify JavaScript
function wp_dudecom_minify_js($js) {
    // Remove comments, whitespace, and unnecessary characters
    $js = preg_replace('/\/\*.*?\*\//s', '', $js);
    $js = preg_replace('/\s*([{}|:;,])\s+/', '$1', $js);
    $js = preg_replace('/\s\s+(.*)/', '$1', $js);
    return trim($js);
}

// Hook to minify CSS and JS files
function wp_dudecom_enqueue_minified_assets() {
    // Get the theme directory URI
    $theme_uri = get_template_directory_uri();

    // Minify and enqueue CSS
    $css_file = file_get_contents($theme_uri . '/style.css');
    $minified_css = wp_dudecom_minify_css($css_file);
    wp_register_style('wp-dudecom-minified-style', false);
    wp_enqueue_style('wp-dudecom-minified-style');
    wp_add_inline_style('wp-dudecom-minified-style', $minified_css);

    // Minify and enqueue JavaScript
    $js_file = file_get_contents($theme_uri . '/script.js');
    $minified_js = wp_dudecom_minify_js($js_file);
    wp_register_script('wp-dudecom-minified-script', false);
    wp_enqueue_script('wp-dudecom-minified-script');
    wp_add_inline_script('wp-dudecom-minified-script', $minified_js);
}

add_action('wp_enqueue_scripts', 'wp_dudecom_enqueue_minified_assets');
?>

Instructions

File Location: Add the code to your theme's functions.php file or create a custom plugin file.

Prerequisites: Ensure you have access to your WordPress theme files and a basic understanding of how to edit them.

Implementation Steps:

  1. Access Your Theme Files: Use an FTP client or your hosting provider's file manager to navigate to your WordPress installation directory.
  2. Locate functions.php: Go to wp-content/themes/your-theme-name/ and find the functions.php file.
  3. Edit functions.php: Open the functions.php file in a text editor.
  4. Insert the Code: Copy the provided PHP code and paste it at the end of the functions.php file.
  5. Save Changes: Save the functions.php file and upload it back to your server if using an FTP client.
  6. Verify Implementation: Visit your website and check if the CSS and JavaScript files are minified. You can use browser developer tools to inspect the loaded resources.

If you encounter any issues or need further assistance, consider reaching out to wp-dude.com for expert help with implementation or more advanced functionality.