# Is there a way to add php code to template?

**URL:** https://discourse.gohugo.io/t/is-there-a-way-to-add-php-code-to-template/15218
**Category:** support
**Created:** [November 10, 2018, 5:33am UTC](https://discourse.gohugo.io/t/is-there-a-way-to-add-php-code-to-template/15218 "2018-11-10T05:33:25Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![jesselau](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jesselau/32/7288_2.png) [@jesselau](https://discourse.gohugo.io/u/jesselau)
#### Post date: [November 10, 2018, 5:33am UTC](https://discourse.gohugo.io/t/is-there-a-way-to-add-php-code-to-template/15218/1 "2018-11-10T05:33:25Z")

</div>

Hello,

I want to add php code to my search.html.

I just put `<?php` to my template file. But Hugo generate a `&lt;?php`.

So I need make another program to change this.

Is there a way to add php code to template?

Thanks for reply.

---

<div class="post-metadata">

### Author: ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)
#### Post date: [November 10, 2018, 5:41am UTC](https://discourse.gohugo.io/t/is-there-a-way-to-add-php-code-to-template/15218/2 "2018-11-10T05:41:56Z")

</div>

Try surrounding your PHP code with double quotes and piping to `safeHTML`

```
{{ "<?php" | safeHTML }}
```

---

<div class="post-metadata">

### Author: ![jesselau](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jesselau/32/7288_2.png) [@jesselau](https://discourse.gohugo.io/u/jesselau)
#### Post date: [November 10, 2018, 5:54am UTC](https://discourse.gohugo.io/t/is-there-a-way-to-add-php-code-to-template/15218/3 "2018-11-10T05:54:21Z")

</div>

Thank you very much. It works.

---

<div class="post-metadata">

### Author: ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)
#### Post date: [November 10, 2018, 5:57am UTC](https://discourse.gohugo.io/t/is-there-a-way-to-add-php-code-to-template/15218/4 "2018-11-10T05:57:54Z")

</div>

Am curious about your workflow. So you are building your hugo site then copying the generated files to a PHP web server?

---

<div class="post-metadata">

### Author: ![jesselau](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jesselau/32/7288_2.png) [@jesselau](https://discourse.gohugo.io/u/jesselau)
#### Post date: [November 10, 2018, 6:05am UTC](https://discourse.gohugo.io/t/is-there-a-way-to-add-php-code-to-template/15218/5 "2018-11-10T06:05:35Z")

</div>

Yes. I just migarated my wordpress site to Hugo a few days ago.  
I made git prod master on my nginx web server and update everything smoothly with just one deploy script.  
But only search function need php to connect my manticoresearch database.

---

<div class="post-metadata">

### Author: ![zwbetz](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/zwbetz/32/16088_2.png) [@zwbetz](https://discourse.gohugo.io/u/zwbetz)
#### Post date: [November 10, 2018, 6:10am UTC](https://discourse.gohugo.io/t/is-there-a-way-to-add-php-code-to-template/15218/6 "2018-11-10T06:10:46Z")

</div>

Oh interesting

---

<div class="post-metadata">

### Author: ![jesselau](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jesselau/32/7288_2.png) [@jesselau](https://discourse.gohugo.io/u/jesselau)
#### Post date: [November 10, 2018, 6:38am UTC](https://discourse.gohugo.io/t/is-there-a-way-to-add-php-code-to-template/15218/7 "2018-11-10T06:38:59Z")

</div>

Just found that it was a **security hole** if web server allow html to run php code.

And hugo theme makers can include php code inside the html files.

Please make sure that web server **just allow php file to run php code** if anybody need the same solution as me.

For my case, I will make a html file with php function with powerful **Hugo** but I will also make a bash script to rename the html file to php file before I upload the whole site to my web server.
