Add custom code to WordPress head


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?


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?

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.

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
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?

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…

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.

1 Like

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?

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.

1 Like

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

Thank you so much for your help and patience!

Best regards,