Want more timely Centmin Mod News Updates?
Become a Member

Wordpress functions.php and speedup your load time

Discussion in 'Blogs & CMS usage' started by EckyBrazzz, May 15, 2019.

  1. EckyBrazzz

    EckyBrazzz Active Member

    576
    123
    43
    Mar 28, 2018
    >>>>Click here<<<< i'm nearby......
    Ratings:
    +226
    Local Time:
    1:46 PM
    1.17.x
    10.3.x
    Here I have a collection of functions that you can add to your functions.php file.
    Use a child-theme to avoid that they get overwritten when there is an update for your theme.

    PHP:
    // Disable Contact Form 7 JS/CSS loading on each page
    add_filter'wpcf7_load_js''__return_false' );
    add_filter'wpcf7_load_css''__return_false' );

    // Disable WooCommerce Widgets
    add_action'widgets_init''remove_woo_widgets' );

    function 
    remove_woo_widgets() {
      
    unregister_widget'WC_Widget_Recent_Products' );
      
    unregister_widget'WC_Widget_Featured_Products' );
      
    unregister_widget'WC_Widget_Product_Categories' );
      
    unregister_widget'WC_Widget_Product_Tag_Cloud' );
      
    unregister_widget'WC_Widget_Cart' );
      
    unregister_widget'WC_Widget_Layered_Nav' );
      
    unregister_widget'WC_Widget_Layered_Nav_Filters' );
      
    unregister_widget'WC_Widget_Price_Filter' );
      
    unregister_widget'WC_Widget_Product_Search' );
      
    unregister_widget'WC_Widget_Top_Rated_Products' );
      
    unregister_widget'WC_Widget_Recent_Reviews' );
      
    unregister_widget'WC_Widget_Recently_Viewed' );
      
    unregister_widget'WC_Widget_Best_Sellers' );
      
    unregister_widget'WC_Widget_Onsale' );
      
    unregister_widget'WC_Widget_Random_Products' );
    }

    // Remove WooCommerce Meta Boxes
    add_action('edit_form_after_title''my_custom_post_edit_form'100);

    /**
    * remove all meta boxes, and display the form
    */
    function my_custom_post_edit_form($post) {

      global 
    $wp_meta_boxes;

      
    // remove all meta boxes
      
    $wp_meta_boxes = array('my_custom_post_type' => array(
        
    'advanced' => array(),
        
    'side' => array(),
        
    'normal' => array(),
      ));

      
    // show my admin form
      
    require dirname(__FILE__) . '/views/my-custom-post-edit-form.php';
    }



    // Disable WooCommerce Style Sheets
    add_filter'woocommerce_enqueue_styles''__return_empty_array' );

    // Disable Cart Fragmentation
    add_action'wp_print_scripts''nuke_cart_fragments'100 );
    function 
    nuke_cart_fragments() {
      
    wp_dequeue_script'wc-cart-fragments' );
      return 
    true;
    }

    add_action'wp_enqueue_scripts''dequeue_woocommerce_cart_fragments'11);

    function 
    dequeue_woocommerce_cart_fragments() {
      if (
    is_front_page()) wp_dequeue_script('wc-cart-fragments');
    }


    // Disable Dashicons
    function wpdocs_dequeue_dashicon() {

    if (
    current_user_can'update_core' )) {
      return;
    }

    wp_deregister_style('dashicons');
    }
    add_action'wp_enqueue_scripts''wpdocs_dequeue_dashicon' );

    // Disable Google Maps
    function disable_google_map_api($load_google_map_api) {

      
    $load_google_map_api false;

      return 
    $load_google_map_api;

    }

    $plugins get_option('active_plugins');
    $required_plugin 'auto-location-pro/auto-location.php';

    if ( 
    in_array$required_plugin $plugins ) ) {
      
    add_filter('avf_load_google_map_api''disable_google_map_api'101);
    }

    // Disable Self Pingbacks
    function no_self_ping( &$links ) {

    $home get_option'home' );

    foreach ( 
    $links as $l => $link )

      if ( 
    === strpos$link$home ) )
     
        unset(
    $links[$l]);
    }

    add_action'pre_ping''no_self_ping' );

    // Remove REST API Links
    add_action('after_setup_theme', function(){
        
    remove_action'wp_head''rest_output_link_wp_head'10 );
    });


    // Disable RSS Feeds
    function itsme_disable_feed() {
      
    wp_die__'No feed available, please visit the <a rel="noopener" href="'esc_urlhome_url'/' ) ) .'">homepage</a>!' ) );
    }

    add_action('do_feed''itsme_disable_feed'1);
    add_action('do_feed_rdf''itsme_disable_feed'1);
    add_action('do_feed_rss''itsme_disable_feed'1);
    add_action('do_feed_rss2''itsme_disable_feed'1);
    add_action('do_feed_atom''itsme_disable_feed'1);
    add_action('do_feed_rss2_comments''itsme_disable_feed'1);
    add_action('do_feed_atom_comments''itsme_disable_feed'1);
    remove_action'wp_head''feed_links_extra');
    remove_action'wp_head''feed_links');


    // Remove the Shortlink Tag
    add_filter('after_setup_theme''remove_redundant_shortlink');
    function 
    remove_redundant_shortlink() {
      
    remove_action('wp_head''wp_shortlink_wp_head'10); 
      
    remove_action'template_redirect''wp_shortlink_header'11);
    }

    // Remove RSD Link Tag
    remove_action('wp_head''rsd_link');

    // Remove wlwmanifest Link
    remove_action('wp_head''wlwmanifest_link');


    // Hide WordPress version
    remove_action('wp_head''wp_generator');

    // remove version from rss
    add_filter('the_generator''__return_empty_string');

    // remove version from scripts and styles
    function shapeSpace_remove_version_scripts_styles ($src) {
      if (
    strpos($src'ver=')) {
        
    $src remove_query_arg('ver'$src);
      }
      return 
    $src;
    }
    add_filter('style_loader_src''shapeSpace_remove_version_scripts_styles'9999);
    add_filter('script_loader_src''shapeSpace_remove_version_scripts_styles'9999);

    // remove version from head
    remove_action('wp_head''wp_generator');

    // remove version from rss
    add_filter('the_generator''__return_empty_string');

    // Disable XML-RPC
    add_filter'xmlrpc_enabled''__return_false' );


    // Remove RSD Links
    remove_action'wp_head''rsd_link' ) ;remove_action'wp_head''rsd_link' ) ;


    // Remove Query Strings
    function _remove_script_version $src ){
      
    $parts explode'?'$src );
      return 
    $parts[0];
    }
    add_filter'script_loader_src''_remove_script_version'15);
    add_filter'style_loader_src''_remove_script_version'15);



    // Disable Embeds in WordPress
    function disable_embeds_code_init() {
      
    // Remove the REST API endpoint.
      
    remove_action'rest_api_init''wp_oembed_register_route' );

      
    // Turn off oEmbed auto discovery.
      
    add_filter'embed_oembed_discover''__return_false' );

      
    // Don't filter oEmbed results.
      
    remove_filter'oembed_dataparse''wp_filter_oembed_result'10 );

      
    // Remove oEmbed discovery links.
      
    remove_action'wp_head''wp_oembed_add_discovery_links' );

      
    // Remove oEmbed-specific JavaScript from the front-end and back-end.
      
    remove_action'wp_head''wp_oembed_add_host_js' );

      
    add_filter'tiny_mce_plugins''disable_embeds_tiny_mce_plugin' );

      
    // Remove all embeds rewrite rules.
      
    add_filter'rewrite_rules_array''disable_embeds_rewrites' );

      
    // Remove filter of the oEmbed result before any HTTP requests are made.
      
    remove_filter'pre_oembed_result''wp_filter_pre_oembed_result'10 );
    }

    add_action'init''disable_embeds_code_init'9999 );

    function 
    disable_embeds_tiny_mce_plugin($plugins) {
      return 
    array_diff$plugins, array('wpembed') );
    }

    function 
    disable_embeds_rewrites ($rules) {

    foreach(
    $rules as $rule => $rewrite) {
     
      if(
    false !== strpos($rewrite'embed=true')) {
        unset(
    $rules[$rule]);
      }
     
    }

    return 
    $rules;
    }


    // Disable Emojis
    function disable_emojis() {
    remove_action'wp_head''print_emoji_detection_script');
    remove_action'admin_print_scripts''print_emoji_detection_script' );
    remove_action'wp_print_styles''print_emoji_styles' );
    remove_action'admin_print_styles''print_emoji_styles' );
    remove_filter'the_content_feed''wp_staticize_emoji' );
    remove_filter'comment_text_rss''wp_staticize_emoji' );
    remove_filter'wp_mail''wp_staticize_emoji_for_email' );
    add_filter'tiny_mce_plugins''disable_emojis_tinymce' );
    }
    add_action'init''disable_emojis' );
    /**
    * Filter function used to remove the tinymce emoji plugin.
    * @param    array  $plugins
    * @return   array  Difference between the two arrays
    */
    function disable_emojis_tinymce$plugins ) {
      if ( 
    is_array$plugins ) ) {
        return 
    array_diff$plugins, array( 'wpemoji' ) );
      } else {
        return array();
      }
    }

    // Clear whitespace in JS and CSS
    add_action('wp_loaded''atm_output_buffer_start');
    add_action('shutdown''atm_output_buffer_end');

    function 
    atm_output_buffer_start() {
        
    ob_start("atm_output_callback");
    }

    function 
    atm_output_buffer_end() {
        
    ob_get_clean();
    }

    function 
    atm_output_callback($buffer) {
        if(!
    is_admin() && !(defined('DOING_AJAX') && DOING_AJAX))
        {
            
    // Remove type from javascript and CSS
            
    $buffer preg_replace"%[ ]type=[\'\"]text\/(javascript|css)[\'\"]%"''$buffer );
            
    // Add alt attribute to images where not exists
            
    $buffer preg_replace'%(<img(?!.*?alt=([\'"]).*?\2)[^>]*?)(/?>)%''$1 alt="" $3'$buffer );
            
    $buffer preg_replace'%\s+alt="%'' alt="'$buffer );
            
    // clear HEAD
            
    $buffer preg_replace_callback('/(?=<head(.*?)>)(.*?)(?<=<\/head>)/s',
            function(
    $matches) {
                return 
    preg_replace(array(
                    
    '/<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->/s'// delete HTML comments
                    /* Fix HTML */
                    
    '/\>[^\S ]+/s',  // strip whitespaces after tags, except space
                    
    '/[^\S ]+\</s',  // strip whitespaces before tags, except space
                    
    '/\>\s+\</',    // strip whitespaces between tags
                
    ), array(
                    
    '',
                    
    /* Fix HTML */
                    
    '>',  // strip whitespaces after tags, except space
                    
    '<',  // strip whitespaces before tags, except space
                    
    '><',   // strip whitespaces between tags
                
    ), $matches[2]);
            }, 
    $buffer);
            
    // clear BODY
            
    $buffer preg_replace_callback('/(?=<body(.*?)>)(.*?)(?<=<\/body>)/s',
            function(
    $matches) {
                return 
    preg_replace(array(
                    
    '/<!--(?!\s*(?:\[if [^\]]+]|<!|>))(?:(?!-->).)*-->/s'// delete HTML comments
                    /* Fix HTML */
                    
    '/\>[^\S ]+/s',  // strip whitespaces after tags, except space
                    
    '/[^\S ]+\</s',  // strip whitespaces before tags, except space
                    
    '/\>\s+\</',    // strip whitespaces between tags
                
    ), array(
                    
    ''// delete HTML comments
                    /* Fix HTML */
                    
    '>',  // strip whitespaces after tags, except space
                    
    '<',  // strip whitespaces before tags, except space
                    
    '> <',   // strip whitespaces between tags
                
    ), $matches[2]);
            }, 
    $buffer);
            
    $buffer preg_replace_callback('/(?=<script(.*?)>)(.*?)(?<=<\/script>)/s',
            function(
    $matches) {
                return 
    preg_replace(array(
                    
    '@\/\*(.*?)\*\/@s'// delete JavaScript comments
                    
    '@((^|\t|\s|\r)\/{2,}.+?(\n|$))@s'// delete JavaScript comments
                    
    '@(\}(\n|\s+)else(\n|\s+)\{)@s'// fix "else" statemant
                    
    '@((\)\{)|(\)(\n|\s+)\{))@s'// fix brackets position
                    //'@(\}\)(\t+|\s+|\n+))@s', // fix closed functions
                    
    '@(\}(\n+|\t+|\s+)else\sif(\s+|)\()@s'// fix "else if"
                    
    '@(if|for|while|switch|function)\(@s'// fix "if, for, while, switch, function"
                    
    '@\s+(\={1,3}|\:)\[email protected]'// fix " = and : "
                    
    '@\$\((.*?)\)@s'// fix $(  )
                    
    '@(if|while)\s\((.*?)\)\s\{@s'// fix "if|while ( ) {"
                    
    '@function\s\(\s+\)\s{@s'// fix "function ( ) {"
                    
    '@(\n{2,})@s'// fix multi new lines
                    
    '@([\r\n\s\t]+)(,)@s'// Fix comma
                    
    '@([\r\n\s\t]+)?([;,{}()]+)([\r\n\s\t]+)[email protected]'// Put all inline
                
    ), array(
                    
    "\n"// delete JavaScript comments
                    
    "\n"// delete JavaScript comments
                    
    '}else{'// fix "else" statemant
                    
    '){'// fix brackets position
                    //"});\n", // fix closed functions
                    
    '}else if('// fix "else if"
                    
    "$1(",  // fix "if, for, while, switch, function"
                    
    " $1 "// fix " = and : "
                    
    '$'."($1)"// fix $(  )
                    
    "$1 ($2) {"// fix "if|while ( ) {"
                    
    'function(){'// fix "function ( ) {"
                    
    "\n"// fix multi new lines
                    
    ','// fix comma
                    
    "$2"// Put all inline
                
    ), $matches[2]);
            }, 
    $buffer);
            
    // Clear CSS
            
    $buffer preg_replace_callback('/(?=<style(.*?)>)(.*?)(?<=<\/style>)/s',
            function(
    $matches) {
                return 
    preg_replace(array(
                    
    '/([.#]?)([a-zA-Z0-9,_-]|\)|\])([\s|\t|\n|\r]+)?{([\s|\t|\n|\r]+)(.*?)([\s|\t|\n|\r]+)}([\s|\t|\n|\r]+)/s'// Clear brackets and whitespaces
                    
    '/([0-9a-zA-Z]+)([;,])([\s|\t|\n|\r]+)?/s'// Let's fix ,;
                    
    '@([\r\n\s\t]+)?([;:,{}()]+)([\r\n\s\t]+)[email protected]'// Put all inline
                
    ), array(
                    
    '$1$2{$5} '// Clear brackets and whitespaces
                    
    '$1$2'// Let's fix ,;
                    
    "$2"// Put all inline
                
    ), $matches[2]);
            }, 
    $buffer);

            
    // Clean between HEAD and BODY
            
    $buffer preg_replace"%</head>([\s\t\n\r]+)<body%"'</head><body'$buffer );
            
    // Clean between BODY and HTML
            
    $buffer preg_replace"%</body>([\s\t\n\r]+)</html>%"'</body></html>'$buffer );
            
    // Clean between HTML and HEAD
            
    $buffer preg_replace"%<html(.*?)>([\s\t\n\r]+)<head%"'<html$1><head'$buffer );
        }

        return 
    $buffer;
    }

     
    • Informative Informative x 3
  2. eva2000

    eva2000 Administrator Staff Member

    41,274
    9,260
    113
    May 24, 2014
    Brisbane, Australia
    Ratings:
    +14,207
    Local Time:
    2:46 AM
    Nginx 1.17.x
    MariaDB 5.5/10.x
    thanks for sharing

    seems alot related to Woocommerce plugin ?
     
  3. EckyBrazzz

    EckyBrazzz Active Member

    576
    123
    43
    Mar 28, 2018
    >>>>Click here<<<< i'm nearby......
    Ratings:
    +226
    Local Time:
    1:46 PM
    1.17.x
    10.3.x
    Just came accors the Woocommerce plugin, and many are use it. Sharing it because it doesn' t hurt.
     
  4. fabianski

    fabianski Member

    99
    12
    8
    Feb 20, 2019
    Brazil
    Ratings:
    +31
    Local Time:
    1:46 PM
    great list, I think there was only something missing for wordpress heartbeat
     
  5. EckyBrazzz

    EckyBrazzz Active Member

    576
    123
    43
    Mar 28, 2018
    >>>>Click here<<<< i'm nearby......
    Ratings:
    +226
    Local Time:
    1:46 PM
    1.17.x
    10.3.x
    Heartbeat
    Code (Text):
    add_action( 'init', 'stop_heartbeat', 1 );
    
    function stop_heartbeat() {
    
    wp_deregister_script('heartbeat');
    
    }
    


    Observation : The Heartbeat API is also used for some essential functions like locking the post when an admin is editing it, and disabling the API will disable that as well. That's the reason I did not mention it in the list
     
  6. fabianski

    fabianski Member

    99
    12
    8
    Feb 20, 2019
    Brazil
    Ratings:
    +31
    Local Time:
    1:46 PM
    yes i know
    but there are ways to change the heartbeat frequency or allow it on specific pages, such as on the posts page

    plus: to disable contact form styles and scripts it is necessary to call the function on the page where the contact form is (Loading JavaScript and Stylesheet Only When it is Necessary)
     
  7. EckyBrazzz

    EckyBrazzz Active Member

    576
    123
    43
    Mar 28, 2018
    >>>>Click here<<<< i'm nearby......
    Ratings:
    +226
    Local Time:
    1:46 PM
    1.17.x
    10.3.x
    You can or leave this one out or disable/uninstall the plugin that gets installed when using option 22 in centmin,