# Add custom code to WordPress head

**URL:** https://forum.pinegrow.com/t/add-custom-code-to-wordpress-head/6358
**Category:** WordPress
**Created:** [March 8, 2022, 9:50am UTC](https://forum.pinegrow.com/t/add-custom-code-to-wordpress-head/6358 "2022-03-08T09:50:28Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Riccarcharias](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/riccarcharias/32/4051_2.png) [@Riccarcharias](https://forum.pinegrow.com/u/Riccarcharias)
#### Post date: [March 8, 2022, 9:50am UTC](https://forum.pinegrow.com/t/add-custom-code-to-wordpress-head/6358/1 "2022-03-08T09:50:28Z")

</div>

Hi,

I want to define some variables I can use in a page template in Wordpress. It works when I add them to the very top of my page template php-code (under the  
“/\*  
Template Name: pagetemplate  
Template Post Type: page  
\*/” -stuff inside the first \<?php...?\> tag.

I tried a lot with the function smart action but I wasn’t able to place the code in the area I described above. I could add the code in the WP theme editor but it would be overwritten each time I update the theme.

Can anyone help me?

Thanks!

---

<div class="post-metadata">

### Author: ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)
#### Post date: [March 8, 2022, 10:38am UTC](https://forum.pinegrow.com/t/add-custom-code-to-wordpress-head/6358/2 "2022-03-08T10:38:48Z")

</div>

Hi @Riccarcharias,  
Where exactly do you want these variables to show up - not sure if you have a master with get\_header, or…? All pages or just one selected template? Where are the variables coming from - hard-coded, passed in with the args?  
Cheers,  
Bob

---

<div class="post-metadata">

### Author: ![Riccarcharias](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/riccarcharias/32/4051_2.png) [@Riccarcharias](https://forum.pinegrow.com/u/Riccarcharias)
#### Post date: [March 8, 2022, 2:47pm UTC](https://forum.pinegrow.com/t/add-custom-code-to-wordpress-head/6358/3 "2022-03-08T14:47:31Z")

</div>

Thanks for your answer @RobM !

I want to show the php-code on only one page template. I have a master page but I want to show the code on a page template with its own header and footer (with get\_header)…

Maybe a picture says more than thousand words…I wrote the “$title = get\_the\_title();” in the WP Theme editor, but it will disappear when I update the theme.

 ![Bildschirmfoto 2022-03-08 um 15.45.48](https://canada1.discourse-cdn.com/flex036/uploads/pinegrow/original/2X/7/7b6388fd80469d5998acee48c6744687e9831bd1.png)

---

<div class="post-metadata">

### Author: ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)
#### Post date: [March 8, 2022, 7:07pm UTC](https://forum.pinegrow.com/t/add-custom-code-to-wordpress-head/6358/4 "2022-03-08T19:07:47Z")

</div>

Hi @Riccarcharias,  
Where do you want to use the variables and where are they coming from?  
Are you using them in the header and is the header a separate PHP file? If you are only using it once, does it have to be separate?  
Not sure if this helps: [get\_header() | Function | WordPress Developer Resources](https://developer.wordpress.org/reference/functions/get_header/)  
If you are wanting to get values from standard WP functions, is there a reason you can’t call the functions where you need them, or is this just a quick example?  
Cheers,  
Bob

---

<div class="post-metadata">

### Author: ![Riccarcharias](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/riccarcharias/32/4051_2.png) [@Riccarcharias](https://forum.pinegrow.com/u/Riccarcharias)
#### Post date: [March 10, 2022, 12:57pm UTC](https://forum.pinegrow.com/t/add-custom-code-to-wordpress-head/6358/5 "2022-03-10T12:57:57Z")

</div>

Hi @RobM

I have theme with a normal master page with header and footer and a page with its own header and footer which I use as a page template. I need the variables only in the page template with its own header and footer. I wand to define them on my own (eg. $title = get\_the\_title() so that the variable $title is different on all pages where I use this page template.

When I create a page with its now header and footer, PG exports separate files for them (header-stadtseite.php and footer-stadtseite.php). I tried to write the custom php code with the smart action “PHP Code” right to the Page Settings of my page template. I used Replace \> Insert before. PG inserts the Custom PHP Code with my variables at the very top of the header-stadtseite.php file. This is great, but I can’t use them in my page template, because I need the variables directly in the stadtseite.php (Page Template).

PG adds just this code “\<?php get\_header( 'stadtseite' ); ?\>” But I also need my variables (like $title…) there as I showed you in the screenshot.

Is there a way to tell PG to add the code in that area? Or do you see another solution?

I could write the code with the theme editor in Wordpress but it would be lost when I update my theme…

---

<div class="post-metadata">

### Author: ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)
#### Post date: [March 10, 2022, 10:02pm UTC](https://forum.pinegrow.com/t/add-custom-code-to-wordpress-head/6358/6 "2022-03-10T22:02:39Z")

</div>

Hi @Riccarcharias,  
This is a slightly complex question. Is there a reason that you need to define these variables before the header? Can you not define them at the top of the template after the call to the header? Is there a real reason to define these variables rather than calling the functions, like `get_the_title()` inside the template? Answering your question is a mix between how Pinegrow works and best practices for WordPress.  
Sorry,  
Bob

---

<div class="post-metadata">

### Author: ![Riccarcharias](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/riccarcharias/32/4051_2.png) [@Riccarcharias](https://forum.pinegrow.com/u/Riccarcharias)
#### Post date: [March 11, 2022, 6:36am UTC](https://forum.pinegrow.com/t/add-custom-code-to-wordpress-head/6358/7 "2022-03-11T06:36:27Z")

</div>

Hi @RobM , Thanks for your answer.

I always thought, that its safer to include php code (especially with a database connection with a passwort) at the very top of a page, above the head. Or doesn’t it matter where I put my php code? I thought that users could see the code with the password of my database in the html code (e.g. with the Crome inspector) when I write it inside the tag?

---

<div class="post-metadata">

### Author: ![RobM](https://avatars.discourse-cdn.com/v4/letter/r/278dde/32.png) [@RobM](https://forum.pinegrow.com/u/RobM)
#### Post date: [March 11, 2022, 10:04am UTC](https://forum.pinegrow.com/t/add-custom-code-to-wordpress-head/6358/8 "2022-03-11T10:04:11Z")

</div>

Hi @Riccarcharias,  
Nope, PHP is a server side language. It is interpreted before it is sent to the browser, so the browser only gets HTML. For JavaScript, which is interpreted by the browser, you have to be careful what you expose.  
Cheers,  
Bob

---

<div class="post-metadata">

### Author: ![Riccarcharias](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.pinegrow.com/riccarcharias/32/4051_2.png) [@Riccarcharias](https://forum.pinegrow.com/u/Riccarcharias)
#### Post date: [March 11, 2022, 10:28am UTC](https://forum.pinegrow.com/t/add-custom-code-to-wordpress-head/6358/9 "2022-03-11T10:28:39Z")

</div>

Ok, Then I can put the code inside the HTML-Tag with the PHP Code smart action 🙂

Thank you so much for your help and patience!

Best regards,  
Ric
