Using PHP attachment page link in Javascript

I’m wondering if there is a way to put my WordPress attachment link inside of a javascript function. In simple terms, it would function like so:

var attach_url = "<?php echo wp_get_attachment_url(); ?>";

(function (){
  return attach_url;
});

More specifically, I’m looking for a way to implement it with Photoswipe’s share buttons (full source code here):

var attach_url = "<?php echo wp_get_attachment_url(); ?>";

(this, function () {
    'use strict';
var PhotoSwipeUI_Default =
 function(pswp, framework) {
shareButtons: [
            {url:'https://www.facebook.com/sharer/sharer.php?u={{attachment_pg_url}}'},
        ],  
        getAttachmentPageURL: function ( shareButtonData ) {
            return attach_url;},
        parseShareButtonOut: function(shareButtonData, shareButtonOut) {
            return shareButtonOut;
        },
    _updateShareURLs = function() {
    var shareButtonOut = '',
                    shareButtonData,
                    shareURL,
                    attachment_pg_url;
    for(var i = 0; i < _options.shareButtons.length; i++) {
                    shareButtonData = _options.shareButtons[i];

                    attachment_pg_url = _options.getAttachmentPageURL(shareButtonData);

                    shareURL = shareButtonData.url.replace('{{attachment_pg_url}}', attachment_pg_url );

Any help is much appreciated!

Read more here: Using PHP attachment page link in Javascript

Leave a Reply

Your email address will not be published. Required fields are marked *