bozdoz / wp-plugin-leaflet-map

Add leaflet maps to Wordpress with shortcodes
https://wordpress.org/plugins/leaflet-map/
GNU General Public License v2.0
141 stars 72 forks source link

JavaScript / CSS don't load on archive page #127

Closed xballoy closed 3 years ago

xballoy commented 3 years ago

First of all I'm not 100% sure if it's an issue with the plugin / my theme or Wordpress.

My issue When I add a leaflet shortcode in my category description the JavaScript and the CSS don't load.

Steps to reproduce:

  1. Add any leaflet shortcode into the description of your category
  2. Go the the category archive page
  3. The html is rendered
    <div class="leaflet-map WPLeafletMap" style="height:250px; width:100%;"></div>

    but the map is not displayed and no JS / CSS is loaded

Screen Shot 2021-03-23 at 00 51 35

However it works when I add it on a post.

bozdoz commented 3 years ago

What's a category description? Is that an excerpt?

xballoy commented 3 years ago

My theme uses https://developer.wordpress.org/reference/functions/category_description/ under the hood to get the description. Does it answer your question?

bozdoz commented 3 years ago

I'll test this out and get back to you. Thanks

bozdoz commented 3 years ago

At first look, it appears I had to add do_shortcode to the category_description function; but I doubt that's your problem if you already have the HTML being rendered. Are there any JS problems in the developer tools?

here's what's working for me:

image
xballoy commented 3 years ago

Basically my theme is doing

<?php if (!empty($content['desc'])): ?>
    <?php echo wpautop( $content['desc'] ); ?>
<?php endif; ?>

I didn't notice yesterday but the script is wrapped in a p tag

Screen Shot 2021-03-23 at 5 01 29 PM

If I change my theme to remplace wpautop by do_shortcode the script is not wrapped anymore but I still do not download Leaflet script. I cannot see any request to get it in the network panel.

xballoy commented 3 years ago

Some more information:

bozdoz commented 3 years ago

That helps. So for some reason the javascript is not being enqueued. I'm guessing there's a problem with the logic I added to the map shortcode class; that, or your theme is doing something funky:

https://github.com/bozdoz/wp-plugin-leaflet-map/blob/master/shortcodes/class.map-shortcode.php#L33-L43

bozdoz commented 3 years ago

I might just change it to use wp_script_is: https://developer.wordpress.org/reference/functions/wp_script_is/

bozdoz commented 3 years ago

if you're able to download v2.23.1 once it's published let me know if that works!

MildConcussion commented 3 years ago

@bozdoz v2.23.0 worked fine here, but v2.23.1 the tiles are all messed up. It's no longer in a specified width container, but it overflows everywhere and there is spacing between them.

Looking at the commit, maybe the wp_leaflet_map is enqueued, but the stylesheet isn't enqueued as it comes after the return?

xballoy commented 3 years ago

🎉

Screen Shot 2021-03-24 at 8 54 08 AM

It's working fine now, thanks a lot!

bozdoz commented 3 years ago

I think I see it; I think did_action is what I want: https://developer.wordpress.org/reference/functions/did_action/

MildConcussion commented 3 years ago

The issue I encountered is also gone in the latest (v2.23.2) version. Cheers!

xballoy commented 3 years ago

Not working anymore with 2.23.2 :( The script don't enqueue anymore

bozdoz commented 3 years ago

v2.23.3 should be a fine compromise. I'm not sure if anyone is even using the leaflet_map_enqueue action. It may have been added just as an option. I thought it should fire only once, but probably won't matter, and there are better custom ways to deal with it if it does. Having each map enqueue the required javascript and css should solve any asynchronous problems that may be happening.

xballoy commented 3 years ago

v2.23.3 works for me 🚀

MildConcussion commented 3 years ago

v2.23.3 also works fine here 👍