Add custom elements on content output#18
Conversation
By using `the_content` filter instead of `content_save_pre`, the custom element `<pwp-lazy-image>` is added on the fly before the content output. This approach avoid to get stuck to PWP Lazy Image plugin in the future: you will be able to switch between themes, lazy load plugins, or even between publishing platforms without the need to rewrite every post.
|
Awesome. One question tho: This means that the compression work will be done every time someone requests a page. Is there a way to compress the resized image somehow? (That was one of the reasons why I did it on |
|
Do both? In my limited experience, WordPress often relies on caching plugins / CDNs rather than worrying too much about compressing resources on the server. I suspect this is because WordPress is expected to run (well) on machines with limited resources (and lacking more advanced PHP image manipulation extensions). Have I grasped the essence of the question? |
|
Okay, you might already know this, but let me explain what this plugin does: Whenever I use an image in a post, this plugin takes that images, resizes it to 3x3 pixels and turns it into a base64 URL. This URL is then sent instead of the image and the full-size image is only loaded once the user scrolls it into view. With your PR, the part where an image has to be downsamples would happen every time the blog post is requested. Obviously that is something that should be cached, but it’s not something that can be cached by a classical CDN or cache plugin. I think the plugin needs to solve this. But happy to have you prove me wrong :D |
|
It is not good to store in database the modified content that depends on your plugin, it is not good to resize the images on every request either. But I think they are different problems that can be solved separately. You could register a custom image size, so the 3x3 image version is automatically created and saved to disk for later use every time you upload a image to WordPress, this way you can avoid the image resizing on every post request and just need to convert the downsamples to base64 URL, which is something we can live with, I think. But this could be further improved, maybe by storing the base64 URL in a meta field for the attachement associated with the image? (every media file in WordPress is associated with a post type called "attachemnt"). But in any case you still want to use As it is, you can not deactivate your plugin or switch to other theme at any time without breaking the content. Which is a big issue. |
That sounds like the best solution so far!
Yeah, I am aware that my theme and the plugin are currently tightly coupled. As I said, I am not very experienced with WP so sometimes took shortcuts just to make it work. I mostly wanted to prove that a PWA is possible with WP :) |
Use
the_contentfilter instead ofcontent_save_pre. Fix issue #17