-3

I recently installed and configured my ACF fields. However, I found out that when I include PHP (ex. <?php the_field('field_name');?>) in a custom field on the admin panel, the PHP only shows up as raw text on the front end page.

To clarify, I want to be able to add PHP in the text areas of ACF on the admin panel and I want the PHP to work as such on the webpage. For example, this is the content of an ACF text area of the field name water_blog_post:

 <div class="<?php the_field('water_type'); ?>">
      <?php the_field('water_title'); ?>
      <?php the_field('water_description'); ?>
 </div>

In this case, the PHP in the front end shows up as raw text, not PHP, but the HTML works fine. I see there is an option to enable HTML, but not PHP, and what I want is to enable PHP so I can use PHP in an ACF field.

Sorry if I sound confusing, it's hard to explain :P Let me know if I need to elaborate.


Goal: (was told PHP in text area was bad idea)

I would like to be able to add content as I go while being able to use custom fields in the new content. For example, in ACF, there is a function where you can add / delete content as you go called a 'flexible field' but it is a premium feature. My work around solution was to just add / delete content via the text areas; however, the problem I encountered was that I needed to be able to include ACF content fields in the text area, so it would not run.

  • 1
    This is really not a great idea. You should not execute PHP in text areas. It is huge security loophole that a hacker will exploid. You will need to rethink your approach here – Pieter Goosen Jun 27 '16 at 5:29
  • @PieterGoosen Hmm, how would I go about doing this then? I would like to easily edit the content through ACF so I can add/delete parts I need/don't need. Is there an alternative plugin that does this? Also, why would it be a security loophole if the textarea is in the admin panel? – DarkTakua Jun 27 '16 at 5:35
  • As far as I read this, you need to use get_field() instead of the_field(). You can use get_field() inside the the_content filter to add your extra info to the content. – Pieter Goosen Jun 27 '16 at 6:05
  • I don't quite know how to approach this method. Do you mind elaborating? I see that get_field() has a different type of formatting than the_field() and I don't really know what to put for the different variables. – DarkTakua Jun 27 '16 at 7:14
  • ACF specific questions are off topic, you will need to go and read up on their functions and how it works, if you have issues with that, contact their support for help. In short, the_field() echo output to screen, while get_field() returns the value from the field. – Pieter Goosen Jun 27 '16 at 7:34
-1

Pay for the ACF Pro version to enable flexible fields (which is basically the function I wanted to have) or use an alternative WP plugin that has the feature for free, such as Carbon Fields or Piklist.

If you want to display PHP in the textboxes, although creating a security vulnerability, you could use Shortcodes. For instance, to display a field in your WYSIWYG editor, use the following code: [acf field="{$field_name}"] and in order to display it, you need to use <?php echo do_shortcode(get_field('field_name')); ?>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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