I’d like to echo get_attachment_link in an enqueued script on my WordPress functions.php. However, I’m not sure how to format it. Is there a way to write something that would work like this code:

// Enqueued script with localized data.
wp_enqueue_script( 'some_handle' );

// Localize the script with new data
$attachment_page = <?php echo get_attachment_link($attachment->ID);?>;

wp_localize_script( 'some_handle', 'object_name', $attachment_page );

This part doesn’t work:
$attachment_page = ID);?>;

Ideally, when corrected, this would give me the attachment page url of dynamically generated content, to be used in javascript.

Read more here: Echo PHP in Enqueue Script


If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Wordpress related questions and answers: