• This is a great plugin. Especially because the calculator is interactive.

    One thing that might make the plugin even better is improving its performance when there’s no ConvertCalculator shortcode.

    With this change, this plugin’s JavaScript will only load on pages that have a ConvertCalculator shortcode:

    
    diff --git a/convertcalculator.php b/convertcalculator.php
    index 9c76e99..68e5215 100644
    --- a/convertcalculator.php
    +++ b/convertcalculator.php
    @@ -10,23 +10,17 @@
     
       if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
     
    -  function convertcalculator_hook_javascript() {
    -    wp_enqueue_script("convertcalculator-script", "https://app.convertcalculator.co/embed.js", array(), false, false);
    -  }
    -
    -  add_action( 'wp_enqueue_scripts', 'convertcalculator_hook_javascript' );
    -  
       function convertcalculator_script_loader_tag($tag, $handle) {
              if ($handle === 'convertcalculator-script') {
                      if (false === stripos($tag, 'async')) {
                              $tag = str_replace(' src', ' async="async" src', $tag);
                      }
                 
                    if (false === stripos($tag, 'defer')) {
                            $tag = str_replace('<script ', '<script defer ', $tag);
                    }
            }
    
            return $tag;
     }
     
    @@ -41,8 +35,9 @@ add_filter('script_loader_tag', 'convertcalculator_script_loader_tag', 10, 2);
           return '<div>You need to add an "id" to the "convertcalculator" shortcode. You can find the calculator id in the <a href="https://app.convertcalculator.co">editor</a>.';
         }
     
    +    wp_enqueue_script("convertcalculator-script", "https://app.convertcalculator.co/embed.js", array(), false, true);
         return '<div class="calculator" data-calc-id="' . $atts['id'] .'"></div>';
       }
     
       add_shortcode( 'convertcalculator', 'convertcalculator_embed_calculator' );

    Right now, the JavaScript file loads on every URL, even if there’s no ConvertCalculator:
    https://cldup.com/N_BCI6ZJ0Z.png

    This is a great plugin, thanks for your work.

  • The topic ‘Idea to make this plugin even better and more performant’ is closed to new replies.