Javascript中的Wordpress PHP链接(用于Photoswipe自定义分享网址)

问题描述:

我想知道是否有方法可以将我的WordPress附件链接放入JavaScript函数中。简单来说,它会发挥作用,像这样:Javascript中的Wordpress PHP链接(用于Photoswipe自定义分享网址)

$attach_url = "<?php echo wp_get_attachment_url(); ?>"; 
function() { 
    return $attach_url; 
} 

更具体地说,我正在寻找一种方式与Photoswipe的共享按钮来实现它(完整的源代码here):

$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); 

任何帮助非常感谢!

编辑

这是我更新的代码,这几乎是工作。 js文件正在解释来自functions.php的入队脚本。但是,显示的网址是<?php echo get_attachment_link(); ?>,而不是实际的链接(例如:home.com/attachment-page),当我在循环中使用此php代码时,它显示正确。

我怎样才能得到的网址输出链接,而不是实际的PHP代码?

在functions.php中:

// Custom Photoswipe Share URL 
    wp_enqueue_script('photoswipe_custom_share_link', get_template_directory_uri() . '/custom_path_here/photoswipe-ui-single-item.js'); 
    $attach_url = "<?php echo get_attachment_link(); ?>"; 
    wp_localize_script('photoswipe_custom_share_link', 'pswp_custom_share', $attach_url); 

在Photoswipe js文件(与我原来的职位一些额外的修正):

编辑

我犯的错在我的入队脚本中使用引号。使用下面的代码,格式化现在是正确的。唯一的问题是url输出是“home.com”而不是“home.com/attachment-page”。

如何在循环外定义动态生成的附件页面url?我需要回应吗?

$attach_url = get_attachment_link($attachment->ID); 

编辑 - 已解决!

我需要使用JavaScript enqueue来获取循环中附件的基URL。 (根据回答here和darkcoder的帮助)。

在functions.php中:

// Custom Photoswipe Share URL 
function pswp_custom_share() 
    { 
     /* Get the ID of the current post */ 
     global $post; 
     $ID = $post->ID; 

     /* register the script */ 
     wp_register_script('photoswipe_custom_share_link', get_template_directory_uri() . 'custom_path_here/photoswipe-ui-single-item.js', array('jquery'), false, true); 
     $attach_url = array(
      'attachment_page' => get_attachment_link($ID) 
     ); 
     wp_enqueue_script('photoswipe_custom_share_link'); 
     wp_localize_script('photoswipe_custom_share_link', 'attach_url', $attach_url); 

    } 

    /* If we're not in the admin section call our function on the wp_enqueue_scripts hook */ 
    if (!is_admin()) add_action("wp_enqueue_scripts", "pswp_custom_share", 10); 

在Photoswipe js文件:

getAttachmentURLForShare: function (/*shareButtonData */) { 
       return attach_url.attachment_page; 
      }, 

可以使用WordPress的排队脚本函数来实现这个

请按照以下链接了解详细信息

https://code.tutsplus.com/tutorials/how-to-pass-php-data-and-strings-to-javascript-in-wordpress--wp-34699

让我知道,如果它解决您的问题

+0

所以我应该使用此代码的functions.php: 'wp_enqueue_script;() 'photoswipe_custom_share_link',get_template_directory_uri( '/custom_path_here/photoswipe-ui-single-item.js'。) $ attach_url =“”; wp_localize_script( 'photoswipe_custom_share_link', 'pswp_custom_share',$ attach_url);' 然后在我的javascript参考如下:'getAttachmentPageURL:功能(shareButtonData){ 回报pswp_custom_share;}'? – BlueHelmet

+0

是的,它应该以这种方式工作..请尝试让我知道它是否工作.. – thedarkcoder

+0

谢谢,它工作,虽然它输出PHP代码本身,而不是一个链接。我的完整编辑在上面。 – BlueHelmet

如PHP是在客户端的JavaScript运行之前处理服务器端应该在理论上是可能的。

我注意到你的脚本缺少wp_get_attachment_url函数的附件ID。

从WordPress API文档:

<?php echo wp_get_attachment_url(12); ?> 

的第一步是通过回附件URL页面上测试一个基本水平的功能。一旦你完成了这个,你就知道你正在正确地连接到这个函数。

然后将该值赋给JavaScript变量并尝试运行某种警报或控制台日志来验证您是否可以成功地将URL解析为全局JS变量。

然后看看你是否可以从函数内部访问该变量。如果您可以逐步完成上述每个基本步骤,那么您应该更有可能将这些数据解析为Photoswipe插件。

如果您遇到困难,请告诉我,我会尽力以任何方式提供帮助。

+1

谢谢,你帮我意识到我确实需要使用''。上面包含了我的编辑。 – BlueHelmet

+0

不客气! –