0

How is it possible to use php inside an entry-field? When I write e.g. in the summary-field

test <?php echo 'bla'; ?> test

I get exactly the same string in the frontend:

test <?php echo 'bla'; ?> test

How can I manage that the php-command is executed? The template where I am trying to use this allows php to be parsed; changing parsing-stage from output to input changes nothing.

UPDATE 1: I am working with EE 5.3.2

UPDATE 2: The reason I want to do this is the following: I have a snippet which contains the number of mailinglist-subscribers; it just shows the pure number. In (only) one/some of my entries I want to show this number, like "The mailinglist contains 32673 subscribers". If I could use the snippet (like I could in a template) I just would write "The mailinglist contains { snip_number } subscribers." As I assume that the allow-ee-code-plugin might be deprecated for EE5 and is not futuresafe I tried to realize it with php like

The mailinglist contains <?php echo(file_get_contents('/path/getnumbers.php'));?>

UPDATE 3: It seems that the allow-eecode-plugin still is working with EE 5.3.x (as JCOGS points out in his comment). For me with the snippet/partial it still does not work, as

{exp:allow_eecode query="y"}{summary}{/exp:allow_eecode}

allows to parse an exp:query-tag which I enter in the summary-field, but not a snippet/partial.

5
  • 2
    Why would a string output be magically parsed by PHP? PHP is server side, not client side, so a properly executed PHP statement such as <?php echo "<?php echo 'bla'; ?>"; ?> would output that string, just like EE outputting the content from a data field. Also, please rethink your problem if this if your chosen solution (PHP in an entry field EVALed).
    – jrothafer
    Commented Jun 3, 2020 at 0:13
  • @jrothafer For sure you are right. My real problem is that I want to use a snippet inside an entry, but the allow-ee-code-plugin is no longer available (UPDATE: but see answer and discussion from JCOGS below). So I had the idea to use php inside the entry field. You don't have any idea how to solve this, do you?
    – BNetz
    Commented Jun 3, 2020 at 7:51
  • as JCOGS said, I think we need a clearer explanation of what you are trying to accomplish. Can you explain why you'd want to use a snippet inside of a content field in an entry and to what extent? For example, you could just use a field to pass info to the template, and then the template can call the snippet: {if my_snippet_field == "snippet_1"}{snippet_1}{/if}. It can definitely be done better than that, just an example. If you can explain what you are trying to do, I can open an answer and attack the problem.
    – jrothafer
    Commented Jun 3, 2020 at 8:03
  • I updated the initial question. Please note that I don't want to operate on template-basis, as I already have a lot of templates and I just want to display the snippet-content inside an entry.
    – BNetz
    Commented Jun 3, 2020 at 8:40
  • I would do it using layout variables I think. You can pull the number of subscribers and put it into a layout variable, and then use some conditionals to build up whatever appropriate bit of text you want to use for the current use case and put that either into the same or a second layout variable, and then drop the layout variable into your template. Commented Jun 3, 2020 at 13:19

2 Answers 2

2

So if you only want a snippet to display on certain entries, you can apply that via a field and small code addition to your template(s). I understand you have a lot of templates and don't want to add more; there is no reason to add more. You can just edit the templates you currently use for these entries, and they'll work either way. In this example, just add a toggle field with a short-name of show_subscriber_numbers to your channel.

https://docs.expressionengine.com/latest/fieldtypes/toggle.html

Here is an example using a toggle field in EE 5+:

{if show_subscriber_numbers}
    <p>
        The mailinglist contains {snip_number} subscribers.
    </p>
{/if}

Then you can just toggle the snippet on and off in the channel entry. No need for new templates or switching templates, just add the code to the templates you already use for this channel. It's not clear yet if you have further requirements, but based on what you've updated, that should work just fine without any add-on's or ill-advised work-arounds.

EDIT

The only thing I can think is if for some particular reason you need to output this subscriber count in the middle of some text/RTE/Wygwam/whatever content field that this solution wouldn't satifisy. If that is the case, a self-authored plugin could also solve this problem.

3
  • Yes, I have to output it in the middle of the text. Uunfortunately I am not good at writing plugins for this myself :-)
    – BNetz
    Commented Jun 6, 2020 at 14:53
  • Is there a reason you can't reconfigure your field layout to have a content_above and content_below fields, and stick a show_subscriber_numbers field in between? It's more messy (in my opinion) than getting a developer to write a plugin that wraps around one text output, but it would work. This could be done 10 other ways, but I gather you lack on developer resources?
    – jrothafer
    Commented Jun 6, 2020 at 15:46
  • As a workaround I solved it with writing the number into the template directly, so this is near the way you suggested in your original answer. But still this is not what I needed, as I just wanted to show the subscriber-number somewhere inside a e.g. summary-field. But thanks a lot for your help and efforts, I really appreciate this!
    – BNetz
    Commented Jun 7, 2020 at 17:04
1

It is possible with a bit of messing around you could achieve what you are trying to do by installing the Allow EE Code add-on. This add-on makes it possible to evalute EE template code placed within entries in EE channels - to use you simply bracket the template output you want to have evaluated between an {exp:allow_eecode} tag pair. So it might be that if you put your php code within a pair of EE Code tags you'll be able to evaluate it.

Possibly a better solution would be to have a really good think about why you might ever want or need to do this: Allow EE Code is helpful in a handful of edge-cases, but in almost every circumstance there is a solution that works well without resorting to this kind of dodge.

If you explain a bit more about why you want to do this, maybe someone can help you find that better solution.

NB. To get the best assistance in this forum, it helps hugely if you give some information about the EE setup you are using - different versions of EE have different quirks / characteristics, so knowing if you are on EE2 or EE5 makes a difference... etc.

5
  • Are you sure that the Allow EE Code plugin still does work with EE5 and possibly EE6 to come? Indeed this would solve my problem completely.
    – BNetz
    Commented Jun 3, 2020 at 7:55
  • It certainly works with EE5 (I have it running on a site currently on EE5.3.2) Commented Jun 3, 2020 at 13:14
  • However my partial (that's what I meant with the deprecated "snippet") is not parsed - the summary-field is inside an an allow_eecode-tag, all of that is inside a exp:channel:entries-loop. Do you have any idea?!
    – BNetz
    Commented Jun 6, 2020 at 14:43
  • I have no idea why your configuration is not working - but I'm with jrothafer you would be better off finding a better way to do this than what you are trying to do. You might want to consider doing something using layout variables - which would give you the space to build up the string elsewhere and just drop it into your template 'complete'. More here docs.expressionengine.com/latest/templates/… HTH Commented Jun 6, 2020 at 16:32
  • I will check this out, but it seems to me that the approach with layout-variables might be too complicated for the moment. I hoped I could get it to work with allow_eecode like you said, but it seems this is not parsing snippets. Perhaps I will investigate this approach and open a new question dedicated to this problem Anyway, thanks a lot for your effort and your help!!
    – BNetz
    Commented Jun 7, 2020 at 17:02

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.