Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Save/Share URL not generating optimally

  • 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

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

  • Oh — apologies, and thanks for everything so far.

  • No problem, Did it work ?

  • Yes, works magnificently!

  • Awesome. Glad we could be of help!

Viewing 9 posts - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.