0

I am trying to figure out how to insert a php tag in between the opening and closing div tags below. Can anyone help me understand how to insert <?php the_field('200_200_1', 'option'); ?> in there?

jQuery(document).ready(function($){
 $('<div class="video_gallery_ad"></div>').appendTo('#avs_gallery');
});

Edit: This is a JS file.

Edit2: Since this won't work with a JS file, I moved it into my functions.php file (using WordPress). Below is the snippet I have in my functions.php file. Is there anything wrong with this code?

jQuery(document).ready(function($){
 $('<div class="video_gallery_ad"><?php the_field('200_200_1', 'option'); ?></div>').appendTo('#avs_gallery');
}); 
SamB
9,2796 gold badges51 silver badges57 bronze badges
asked Jan 10, 2014 at 21:04
8
  • 2
    Like this: $('<div class="video_gallery_ad"><?php the_field('200_200_1', 'option'); ?></div>').appendTo('#avs_gallery');? If this is in a .php file, that should work (assuming the_field() echos something). Commented Jan 10, 2014 at 21:05
  • As this looks like Wordpress, you should generally speaking put your script in a file, add it with dependencies using enque_script, and use localize_script to pass the PHP value. That is the proper way to do this. Commented Jan 10, 2014 at 21:07
  • @Rocket It's a js file. Sorry, should have mentioned. Commented Jan 10, 2014 at 21:09
  • 1
    .js files aren't parsed by PHP unless you explicitly set PHP to parse .js files. Commented Jan 10, 2014 at 21:10
  • @Desi then it isn't possible without modifications to your webserver. Commented Jan 10, 2014 at 21:10

1 Answer 1

1

You can try with:

$('<div class="video_gallery_ad"><?php the_field('200_200_1', 'option'); ?></div>').appendTo('#avs_gallery');

But it is VERY risky. Isn't it better to render render output of this function in some hidden div and after that load its content into #avs_gallery ?

And JS:

$('#hiddenAdv').show().appendTo('#avs_gallery');
answered Jan 10, 2014 at 21:05
Sign up to request clarification or add additional context in comments.

1 Comment

@RohitAgrawal Easy, configure your webserver to send .js files to the php parser. (note, that generally is a bad idea)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.