0

I'm having a hard time understanding how to import a specific variable for use with jQuery.

Some links on a Wordpress theme are using

<?php  if( get_post_meta($post->ID, "portfolio_link", true) ): ?> 
            <h1 class="portfolio-title">
                <a target="_blank" href="<?php the_field('portfolio_link'); ?>">
                    <?php the_field('portfolio_title'); ?> <span class="sosa-icon">p</span>
                </a>
            </h1>
            <!--get PDF if not empty-->
        <?php else: ?>
            <h1 class="portfolio-title"><?php the_field('portfolio_title'); ?></h1>
<?php endif; ?>

As you can see the href is set as

href="<?php the_field('portfolio_link'); ?>"

Now I have a jQuery script as follows

    <script>
  <?php  if( get_post_meta($post->ID, "portfolio_link", true) ): ?> 
  <?php

$phpVar = 'http://www.google.com';
  echo "var phpVariable = '{$phpVar}';";

?>
  jQuery(".box").click(function() {

    window.open(phpVariable);
});

  <?php endif; ?>
</script>

This script currently works. It opens google in a new tab as a placeholder until I know how to make it open the same result as the href.

Now what I can't understand is how to set '$phpVar' to have the same effect as the 'href' I showed before instead of 'http://www.google.com';

5
  • window.location.href = phpVariable; Commented Nov 25, 2016 at 17:06
  • the window.open(phpVariable); is fully functional. My issue is that I don't know how to set that variable to be the same as href="<?php the_field('portfolio_link'); ?>" Commented Nov 25, 2016 at 17:08
  • $('.box').attr('href', phpVariable); Commented Nov 25, 2016 at 17:10
  • or $phpVar = the_field('portfolio_link'); Commented Nov 25, 2016 at 17:11
  • do you want track by php which link was clicked ? i dont understand what exactly you want Commented Nov 25, 2016 at 17:15

1 Answer 1

1

Not sure just how the WordPress handling of this is, but given that the_field('portfolio_title'); returns a valid URL, you can simply assign the JavaScript variable the output of this variable.

var phpVariable = "<?php the_field('portfolio_link'); ?>";
Sign up to request clarification or add additional context in comments.

4 Comments

Your comment just helped me get it! Thank you. I added your line of code into the click function and it worked exactly as I need it.
The final function looks like this for anyone who may need it: jQuery(".box").click(function() { var phpVariable = "<?php the_field('portfolio_link'); ?>"; window.open(phpVariable); });
I'm not sure if I should open a new question or ask here. This does work but I noticed if I have adblock disabled, it will open every link on the page at the same time.
@MartynJH That is not related to the question here. To be able to solve that you'd need to provide information about the loop (if there is loop) as well as where .box is in the markup. It is not solvable given the information provided here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.