Мне интересно, есть ли способ поместить мою ссылку на вложение Wordpress внутри функции javascript. Проще говоря, это будет работать так:
$attach_url = "<?php echo wp_get_attachment_url(); ?>";
function () {
return $attach_url;
}
В частности, я ищу способ реализовать это с помощью кнопок общего доступа Photoswipe (полный исходный код здесь а>):
$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. Однако отображается URL-адрес <?php echo get_attachment_link(); ?>
, а не фактическая ссылка (например: home.com/attachment-page), которая отображается правильно, когда я использую этот php-код в цикле.
Как я могу получить URL-адрес для вывода ссылки, а не фактический 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 (с некоторыми дополнительными исправлениями из моего исходного сообщения):
(this, function () {
'use strict';
var PhotoSwipeUI_Default =
function(pswp, framework) {
shareButtons: [
{url:'https://www.facebook.com/sharer/sharer.php?u={{attach_url}}'},
],
getAttachmentURLForShare: function ( /*shareButtonData */) {
return pswp_custom_share;
},
parseShareButtonOut: function(shareButtonData, shareButtonOut) {
return shareButtonOut;
},
_updateShareURLs = function() {
var shareButtonOut = '',
shareButtonData,
shareURL,
attachment_url;
for(var i = 0; i < _options.shareButtons.length; i++) {
shareButtonData = _options.shareButtons[i];
attachment_url = _options.getAttachmentURLForShare(shareButtonData);
shareURL = shareButtonData.url.replace('{{attach_url}}', encodeURIComponent(attachment_url) );
ИЗМЕНИТЬ
Я сделал ошибку, используя кавычки в своем сценарии постановки в очередь. С приведенным ниже кодом форматирование теперь правильное. Единственная проблема заключается в том, что выходной URL-адрес «home.com» вместо «home.com/attachment-page».
Как я могу определить динамически сгенерированный URL-адрес страницы вложения вне цикла? Нужно ли повторять это?
$attach_url = get_attachment_link($attachment->ID);
ИЗМЕНИТЬ — РЕШЕНО!
Мне нужно было использовать очередь JavaScript, чтобы получить базовый URL-адрес вложения в цикле. (На основе ответа здесь и с помощью дарккодера).
В 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;
},