lesterchan / wp-polls

Adds an AJAX poll system to your WordPress blog. You can also easily add a poll into your WordPress's blog post/page.
https://wordpress.org/plugins/wp-polls/
93 stars 78 forks source link

Page speed improvements - JS/CSS only load if poll is used #71

Closed foliovision closed 6 years ago

foliovision commented 8 years ago

45 We created this fix.

lesterchan commented 8 years ago

This is not a good solution. What if the user insert the PHP code of the poll manually onto a page or someone is using the sidebar widget for the poll? All of this doesn't use the shortcode

foliovision commented 8 years ago

Hello Lester,

thank you for the reply. You are right about these cases, but why affect page speed for rest of the users who user the plugin in a standard way?

The global $poll_shortcode_used could be moved to the PHP function directly of course.

The CSS could be loaded in footer (not recommended, but better than loading it all the time) using the same logic as JS.

Or you could just use what we created and add a "Load everywhere" option for the plugin which would be intended for these users who play with PHP code and can't rely on this dependency check which we crated.

Thanks, Martin

lesterchan commented 8 years ago

Hmm define standard? The standard way is to use it on the sidebar not in the post. The plugin was designed to be that and I have more users putting it in the sidebar then in the post based on the forum support sites that I visited.

I would prefer you to deregister the JS/CSS and copy the generated CSS and JS and put it inside your own theme CSS/JS. That is what I did to my own site.

foliovision commented 8 years ago

So it could be reworked to work with the PHP function easily and also preferably CSS in the footer.

Moving the CSS and JS to the theme doesn't shound like a fix to me - since if there is an update in the CSS/JS I would have to be copying over your new files.

Wouldn't you prefer if the plugins worked the way we outlined here? You attached some article about why it should work this way in the Issue yourself: https://github.com/lesterchan/wp-polls/issues/45

lesterchan commented 8 years ago

That was two years ago. I think there is a better solution like register the CSS/JS then when the get_poll function is called, call the enqueue script.

Something along the line.

foliovision commented 8 years ago

These practices are still the best, even though it's 2 years old.

I had a look at get_poll(), noticed it uses global $polls_loaded and re-coded the CSS and JS enqueueing based on that.

Now it only loads the JS and CSS if there is some poll, so it all goes into footer. If the poll is in a post it works, if it's in a widget, it works as well. Yet our homepage stays clean with reduced amount of requests.

Thanks, Martin

lesterchan commented 8 years ago

I am ok putting

if( empty( $polls_loaded ) ) {
    return;
}

in function poll_scripts()

But I don't like the idea of putting CSS at the footer, the specs for HTML5 states that tag is suppose to be in the header. Unless you can proof me wrong https://developer.mozilla.org/en/docs/Web/HTML/Element/link

foliovision commented 8 years ago

Hello Lester,

you are right about the CSS. So it could be checking global $posts as well as all the active widgets for any WP Polls instance and load the CSS in such case.

Thanks, Martin