TransWikia.com

Use php inside entry-field instead of snippet

ExpressionEngine® Answers Asked by BNetz on October 3, 2021

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.

2 Answers

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.

Answered by jrothafer on October 3, 2021

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.

Answered by JCOGS Design on October 3, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP