0

I have a Wordpress based website, and some of the content is loaded through javascript. For example:

jQuery(".portfolio-fs-slides").css({"display":"none"}).prepend('<div class="portfolio-fs-          slide current-slide portfolio-ppreview"><div class="project-coverslide"></div><div id="content" class="content-projectc contenttextwhite"></div></div>');

What I want to do is append this shortcode: <?php echo do_shortcode('[daisy]'); ?>

But as far as I know is not really possible to append php code in javascript. Is there any workaround to accomplish this ? Thanks!

5
  • 4
    The PHP needs to be executed serverside. However you can echo js code to be interpreted by the client. Commented Aug 22, 2012 at 16:35
  • so, you're saying to append the echoed shortcode in that function ? Commented Aug 22, 2012 at 16:49
  • It should work, yes. What's the result of that PHP snippet? Commented Aug 22, 2012 at 16:53
  • a class="clickable">CLICK HERE</a> Commented Aug 22, 2012 at 16:55
  • I've tried to add a class="clickable">CLICK HERE</a> inside that function and it's not working, my website crash Commented Aug 22, 2012 at 17:43

1 Answer 1

1

As @Bergi mentioned, the PHP will run serverside, and the JS will run client side. You can output JS (or parts of your JS) via PHP and have it run client side, e.g.

<script>
    // extra code here
    jQuery(".portfolio-fs-slides").css({"display":"none"}).prepend('<div class="portfolio-fs-          slide current-slide portfolio-ppreview"><div class="project-coverslide"></div><div id="content" class="content-projectc contenttextwhite"></div></div>'); 
    <?php echo 'we can output valid js here' ?>
    // more code here
</script>

One way to think of this is that since PHP runs server side, it will always run before the JS is parsed.

Put another way, you could have a javascript line like this:

console.log(<?php echo $someVariable ?>);
Sign up to request clarification or add additional context in comments.

1 Comment

I'm thinking to add it like this: ('.portfolio-fs-viewport').append('<a class="clickable">CLICK HERE</a>'); but I receive this in console : yntaxError: Unexpected token <

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.