• Resolved shofera

    (@shofera)


    Hi,

    First off thanks for the great plugin and the awesome support you provide!
    I have plugin-bundled, Page-builder shortcodes that require some INLINE! CSS and JS to be enqueued whenever a shortcode is called on a page so I followed the WP core requirements to enqueue them accordingly, for example:

    add_action( 'wp_enqueue_scripts', 'my_register_shortcodes_scripts' );
    function my_register_shortcodes_scripts() {
      // Notice I create a handle without a file, only to be used for inline JS
      wp_register_script( 'my_shortcodes_script', false );
      wp_enqueue_script( 'my_shortcodes_script');
      wp_add_inline_script( 'my_shortcodes_script', $js );
    }

    As header is already loaded once a shortcode is met on a page the queueing loads my scripts in the footer (not the most elegant of executions but a compromise that I can live with along with the benefits of those scripts only executing on pages where the specific shortcode exists). Same happens with inline styles btw.

    Once I enable the plugin the inline scripts no longer get loaded. When I disable the JS processing they pop back in.

    Any idea on what I could do to preserve those would be greatly appreciated or possibly if the plugin can accommodate them as being a WP supported functionality…
    Thanks a lot for your time again!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Raul P.

    (@alignak)

    I would suggest either adding the code inside a static css file,
    OR
    actually enqueue a css file with some dummy code ( ie: .fvm1234{display:none} ) so that FVM can merge it together with it’s inlined code,
    OR
    Select the option to not merge inlined code on fvm,
    OR
    do this instead (so it avoids being found by FVM):

    add_action( 'wp_enqueue_scripts', 'my_register_shortcodes_scripts' );
    function my_register_shortcodes_scripts() {
      echo '<script>'. $js . '</script>';
    }

    I’ll work on an update soon to consider this type of situations, but by default, all inlined data must have a valid parent file (currently), hence when FVM doesn’t see a valid url on the parent, it must be skipping it.

    The problem is that it should leave it alone, instead of stripping it… but will look into it sometime later.

    Thanks for reporting.

    Thread Starter shofera

    (@shofera)

    Hi again and thanks for the quick reply!
    I have considered all the alternatives you mentioned above before:
    1 . I would suggest either adding the code inside a static css file
    Not an option – some of them are dynamic (resulting from either theme options or DB)

    2 . actually enqueue a css file with some dummy code ( ie: .fvm1234{display:none} ) so that FVM can merge it together with it’s inlined code
    How would I go about this, can I use wp_add_inline_script/style and hint for the generated code to be linked/merged to an existing file somehow?

    3 . Select the option to not merge inlined code on fvm. Which option is this, experimented quite a bit, my wp_add_inline_script/style gets ignored when not linking to a file all the time.

    do this instead (so it avoids being found by FVM)
    Unfortunately not an option, the theme & plugin we are finalizing is headed for Envato Market and they require complying fully with WP guidelines on properly enqueueing all scripts and styles (namely via wp_add_inline_script/style) so that they can later be dequeued and so on.

    Would love to see this option added to the plugin especially since it’s a core functionality (although not very popular, but sometimes you don’t need whole files for a few lines of dynamically generated JS/CSS).

    Thanks again for your time, looking forward to see this in your awesome plugin!

    PS: Btw, just for reference, tested with a few other plugins:

    WP Better Minify – doesn’t include inline JS/CSS.
    Autoptimize – includes them.

    Plugin Author Raul P.

    (@alignak)

    I see… so if you’re developing the theme…

    How about instead of using wp_add_inline_style to a handle without file, you add it to the theme main style.css?

    And for wp_add_inline_script perhaps adding it to some script that is already enqueued by the theme?

    If it’s a plugin, you could add it to any other css/js file that you are enqueueing.
    Of course that if your plugin doesn’t enqueue anything, then this wouldn’t work.

    What I can tell you is that very soon I’ll look into this.

    Version 3 of the FVM plugin will work relatively different, by reading the tags directly from the html instead of going through the enqueue filters… which in turn, allows for better optimization.
    Users will be able to decide which inline code to merge, or which code to exclude.
    Currently version 3 is under testing… but I am still deciding if I’ll change the name or push this as version 3 (too many differences).

    For version 2 however, I’ll still want this fixed… so I’ll look at it asap.

    Thread Starter shofera

    (@shofera)

    Hi again,

    Brilliant! I would love to be one of your beta-testers (both v2 or v3 whichever you set to accommodate those up first, obviously v3 should capture them automatically by the script tag). 🙂
    Let me know as soon as you have something to push.
    Greetings!

    Plugin Author Raul P.

    (@alignak)

    @shofera FVM 3 will probably be under a different name, but will let you know sometime later.

    I pushed the 2.6.0 update, which has fixed the situation you reported.

    Thanks again,

    Raul

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Problem with Inline enqueued CSS/JS’ is closed to new replies.