If I wanted all PHP removed from the front end (meaning not in views, not in the body of blocks), but someplace hidden in the background, is there a 'official' recommendation for doing so?

Drupal's framework is PHP so I figured it would have some sort of standard for this type of thing, but it doesn't appear to be the case.

  • It suppose it depends on what the PHP code is for. If it provides some kind of functionality, you should create a custom module and put your code there. Otherwise, the code is probably for theming and then you should put it in a proper template file of your theme's template.php file. So according to me, you won't find an unambiguous answer if you formulate your question like this.
    – Jeroen
    Commented Mar 31, 2016 at 22:03
  • How is this question getting along? Any updates/thoughts? Commented Apr 7, 2016 at 17:53

2 Answers 2


I would quickly answer no to your question (and explain myself below).

The only thing that comes to my mind as a kind of answer is "place it in a hook".
But as you probably know, there are tons of these.
So, there is not one single "official" place, if I get your point right. Plus, using a hook means placing your PHP Snippets in files. Is that in agreement with your question? Or, is your question better phrased as "Is there a 'official' place in the configuration to put PHP snippets?"

Well, hope this explained the "no" answer some, if I am off, please do elaborate some more.


P.S.1: Sounds interesting, sounds like you have something specific in mind. :-)
P.S.2: It's worth mentioning that there are even some modules like the Computed Field that guide you (through naming conventions) to place your snippets inside specific functions. This is similar to the hook idea, but fantastically presented and suggested in the field's configuration. It's one of the reasons I am so very fond of the Computed Field


If I wanted all PHP removed from the front end (meaning not in views, not in the body of blocks)

Then you would use template files. Create a template file (.tpl.php) for your view or for your block and put the php code there.

The template file goes in /sites/all/themes/YourTheme/templates folder.


If when you edit your block the url is admin/structure/block/manage/block/8/configure which means the 8 is the block ID.

Then template file name is block--block--8.tpl.php

Copy the code in block.tpl.php file located in /sites/all/themes/YourTheme/templates and paste it into your block--block--8.tpl.php file. Then add your custom php code somewhere in there.

Remember to clear/flush the Drupal cache to see changes.

For View's you could create a template (.tpl.php) file OR you could use template.php

It really depends on the use case to select which method is the best way to go. I would need more details about what your view's php code does in order to give you a proper solution.

PS: It is good that you're asking this as using the PHP Filter module or Views PHP is considered bad practice. See What are the downsides of using 'custom' PHP code in blocks, nodes, views-args, etc?

  • Isn't this suggestion breaking the separation of concerns idea? Template files should tackle presentation, not really the place to place php code in general. What do you think? Commented Mar 31, 2016 at 23:10
  • Generally, if you're adding php here, it's probably small snippets such as if statements, if value is x then print y if value is z print a and not 100 lines of code. So it is tackling presentation as in making the presentation dynamic.
    – No Sssweat
    Commented Mar 31, 2016 at 23:24
  • 1
    Ok, we agree then. That is btw a useful point for the original poster to keep in mind when reading your answer. Commented Mar 31, 2016 at 23:28

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.