Support

Account

Home Forums General Issues ACF Textarea: Show PHP?

Solved

ACF Textarea: Show PHP?

  • 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 😛 Let me know if I need to elaborate.


    Update: (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, I’d like a system similar to the flexible field feature, but it’s premium. 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.

    Somebody on SO said this: “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” however asking this was off topic so I was wondering if you all had some insight on how to do this?

    Thanks for the help!

  • Hi @darktakua

    Could you please explain it in more details? Could you please share some screenshots of it with notes on how you want it to be?

    If you need to execute a code in the content editor, I suggest you create a shortcode instead. This page should give you more idea about it: https://codex.wordpress.org/Shortcode_API.

    Thanks 🙂

  • Hello!

    So, I created a field named “rep_card_content” and to display it on the template, I put <?php the_field('rep_card_content');?>. In this case, I want to be able to add ACF custom values and display them in the ACF area to display the content, as shown here:

    http://image.prntscr.com/image/3caee2eb014c43adb02e24648f6f1050.png (ACF Field)

    On the front end, however, the PHP shows up as raw text, and not the content that I input in the page’s custom field:

    http://image.prntscr.com/image/432ea67f68d84e42ae6d8e4890d46faf.png (Front end PHP)

    I want the fields to show up as the content I set it to in the admin panel, similar to how you guys have the option to convert HTML to HTML.

  • Hi @darktakua

    Just like what you have heard, adding PHP code in a text area is not a good thing to do. Could you please tell me why you can’t add that code in your template file instead? It will keep your server secure and the page layout will be consistent.

    If you need to add it in the text area, ACF has a shortcode to do it. Unfortunately, it will only work for simple field types. This page should give you more idea about it (under Does ACF have a Shortcode?): https://www.advancedcustomfields.com/resources/displaying-custom-field-values-in-your-theme/.

    After adding the shortcode, you need to execute it by using the do_shortcode() function like this:

    echo do_shortcode(get_field('text_area_field_name'));

    You can also create your own shortcode if you need to.

    I hope this makes sense 🙂

  • Hello!

    I want to be able to add and delete content easily as not all pages will have a set number of rep card contents, rather, there will be any where from 1-3.

    So basically I need to replace the PHP inside the textareas to for example the first one, [acf field="{rep_last_name}"] ?

    Then for the echo code, where would I put that?

  • Hi @darktakua

    In that case, you can check if the rep_last_name field has a value or not. If it does, then show the template like this:

    <?php
    if( get_field('rep_last_name') ){
    ?>
    
        <div="<?php the_field('rep_last_name'); ?>" class=
        ...
        ...
        ...
        </span>
    
    <?php } ?>

    Regarding the shortcode, you need to use it like this:

    [acf field="rep_last_name"]

    You need to put the echo code in your template. So instead of

    the_field('rep_card_content');

    You need to use this:

    echo do_shortcode(get_field(‘rep_card_content’));

    I hope this helps 🙂

  • Hello!

    Sorry, I don’t really understand what I should to do to display the PHP from the textarea with the code you have given.

    So for the first part, I should use that code to only display content if there is content in the custom field, or is it to show the PHP? Also, I don’t quite understand why you did <div="<?php the_field('rep_last_name'); ?>" and ended it with a </span>?

    Also, I’m a bit confused on what to a shortcode’s use is. :S Is it to show the PHP in the textarea?

    Thanks for helping!

  • Hi @darktakua

    Please learn how a theme works here: https://codex.wordpress.org/Theme_Development.

    The code is used to check if the custom field (in this case “rep_last_name”) has a value or not. If it has a value, it should be safe to assume that the rep card has a content, right? Then if the rep card has a content, show the rep card layout and the other custom fields’ value.

    The code is ended by a span tag because I followed the code in your screenshot. But I didn’t notice that the code is not ended there. I’m sorry for the confusion.

    A shortcode is a code that you can use in the WYSIWYG content that allows you to execute the PHP code associated with it. This page should give you more idea about it: https://codex.wordpress.org/Shortcode.

    If you don’t have time to learn how WordPress template works, I suggest you hire a developer to help you out with it, and I’d recommend looking for one on https://studio.envato.com/ or https://www.upwork.com/.

    I hope this helps 🙂

  • Hello!

    OK, so I understand the the conditional code that you have provided, as well as how to make a template. But my problem is that I need to have the PHP work inside those fields, as I want to be able to change the content inside the rep_card_content. Is there a work around to this problem without having to have PHP in the text area?

    For example: http://image.prntscr.com/image/3caee2eb014c43adb02e24648f6f1050.png – I want to be able to edit the content through the custom fields in the rep_card_content while being able to add more rep_card_contents.

  • Hi @darktakua

    Like I said before, if you really need to use PHP code in the text area, I suggest you use a shortcode instead. Please check the links I gave you before regarding shortcode. After that, you can execute the shortcode by using the do_shortcode() function like this:

    echo do_shortcode(get_field('rep_card_content'));

    Instead of like this:

    the_field('rep_card_content');

    If you don’t mind about the security of your site, you can always use eval() function to execute the string. Please keep in mind that I have warned you that it is a really bad idea to execute a string.

    Thanks 🙂

  • Thanks for the help 🙂

Viewing 11 posts - 1 through 11 (of 11 total)

You must be logged in to reply to this topic.