3

I have created a content type in Drupal 8 and I added a field named "body". But I'm unable to use PHP code in this field.

How can I enable PHP in my "body" field?

  • 2
    Please don't ?!? – AyeshK Dec 28 '16 at 20:27
  • 1
    I would encourage you to find an alternate method, this is a very bad idea. If you expand your question as to what exactly you need to do, most likely there is a way to accomplish what you need to do without having to use PHP that way. – Danny Englander Dec 28 '16 at 20:54
9

Using PHP in your body field is considered bad practice. Read: What are the downsides of using PHP Filter code in blocks, nodes, views-args, etc?

Even in the module page it clearly says:

Warning

Enabling this module can cause security and performance issues as it allows users to execute PHP code on your site. There are better alternatives out there that do not expose such vulnerabilities on your site.

The proper way to add your PHP:

  • Use a preprocess function in theme_name.theme located in your theme's folder.
  • Create a custom module that uses hooks or preprocess functions.

Furthermore, Twig has several filters and functions that you can make use of. Which in some cases, you don't even need to use PHP at all.

4

Install and configure PHP module.

The PHP Filter module adds a PHP filter to your site, for use with text formats.

Note: The PHP filter core module has been removed from core starting with version 8.x. Please check PHP filter overview

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.