In ACF I added a custom editor field but when I try to save it's text into a variable and output the value with js to the console it gives me an empty string.

Here's my code:

<?php $test = the_field('text'); ?>

And the javascript:

     var v = '<?php echo $test; ?>';

I need to get the text value because I need to check if there are list items in it. What am I doing wrong? Or is there a special method for that? If i try to output it directly to the front end...

<?php the_field('text'); ?>

...it works.

1 Answer 1


the_field() is a echo statement use get_field() instead.

You can't save echoed value into a variable, for that you may need to use output buffer ob_start.In your case get_field function should work.


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.