-
registriran
Hi,
Some time ago you helped me set up a Pinterest “Save” button on images on my website for the GP Premium theme. (old thread: https://generate.support/topic/tyring-to-figure-out-if-this-is-custom-code-or-a-plugin/ )
Now, some time later and after testing, I discovered that when clicked the button does not always produce the desired result.
Long story short, I found another website the button of which works every time as desired.
I noticed that the difference lies in how the buttons generate the URLs to Pinterest. (example site in Private field)
Could you help me adjust my script hook so that the URLs in the buttons generate conceptually the same way as with the example site?
And possibly inserting the post’s title as a description because right now it’s not set to insert anything?
Thanks
-
registriran
And for your convenience, here’s what the hook script looks like right now:
<script> document.addEventListener("DOMContentLoaded", function() { var images = document.querySelectorAll(".single-post .wp-block-image img, .single-post .gb-block-image img, .single-post p[data-slot-rendered-content='true'] img, .single-post p img"); images.forEach(function(img) { var a = document.createElement('a'); a.className = 'pin-it-button'; a.href = 'https://pinterest.com/pin/create/button/?url=' + window.location.href + '&media=' + img.src + '&is_video=false&description='; a.rel = 'noopener'; a.target = '_blank'; a.setAttribute('data-pin-do', 'buttonPin'); img.parentNode.insertBefore(a, img); }); }); </script>
-
David
Hi there,
try this script instead:
<script> document.addEventListener("DOMContentLoaded", function() { var images = document.querySelectorAll(".single-post .wp-block-image img, .single-post .gb-block-image img, .single-post p[data-slot-rendered-content='true'] img, .single-post p img"); images.forEach(function(img) { var a = document.createElement('a'); a.className = 'pin-it-button'; var url = encodeURIComponent(window.location.href); var media = encodeURIComponent(img.src); a.href = 'https://pinterest.com/pin/create/bookmarklet/?url=' + url + '&media=' + media + '&is_video=false&description='; a.rel = 'noopener'; a.target = '_blank'; a.setAttribute('data-pin-do', 'buttonPin'); img.parentNode.insertBefore(a, img); }); }); </script>
-
registriran
Looks great, thanks!
Can we make so that the post’s title is filled in as a description also? And I’m marking this as resolved.
Thanks
-
This is as far as I can take it, as its really custom development which is out of scope:
<script> document.addEventListener("DOMContentLoaded", function() { var images = document.querySelectorAll(".single-post .wp-block-image img, .single-post .gb-block-image img, .single-post p[data-slot-rendered-content='true'] img, .single-post p img"); var postTitle = document.querySelector(".single-post .entry-title").innerText || document.title; // Adjust the selector if needed to match your theme images.forEach(function(img) { var a = document.createElement('a'); a.className = 'pin-it-button'; var url = encodeURIComponent(window.location.href); var media = encodeURIComponent(img.src); var description = encodeURIComponent(postTitle); a.href = 'https://pinterest.com/pin/create/bookmarklet/?url=' + url + '&media=' + media + '&is_video=false&description=' + description; a.rel = 'noopener'; a.target = '_blank'; a.setAttribute('data-pin-do', 'buttonPin'); img.parentNode.insertBefore(a, img); }); }); </script>
this looks for the themes entry-title on the post to create the description.
-
registriran
Oh — apologies, and thanks for everything so far.
-
David
No problem, Did it work ?
-
registriran
Yes, works magnificently!
-
David
Awesome. Glad we could be of help!
- You must be logged in to reply to this topic.