Skip to content

Add custom elements on content output#18

Open
CybMeta wants to merge 1 commit into
GoogleChromeLabs:masterfrom
CybMeta:patch-1
Open

Add custom elements on content output#18
CybMeta wants to merge 1 commit into
GoogleChromeLabs:masterfrom
CybMeta:patch-1

Conversation

@CybMeta

@CybMeta CybMeta commented Jan 30, 2018

Copy link
Copy Markdown

Use the_content filter instead of content_save_pre. Fix issue #17

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.
@surma

surma commented Jan 31, 2018

Copy link
Copy Markdown
Contributor

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 content_save.

@tedsecretsource

Copy link
Copy Markdown

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?

@surma

surma commented Jan 31, 2018

Copy link
Copy Markdown
Contributor

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

@CybMeta

CybMeta commented Feb 1, 2018

Copy link
Copy Markdown
Author

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 the_content filter to replace images elements with custom elements, I'm pretty sure. I think we talk about problems that can be solved separately.

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.

@surma

surma commented Feb 1, 2018

Copy link
Copy Markdown
Contributor

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

That sounds like the best solution so far!

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.

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants