0

I've created a Wordpress Page template called head.php

I then went into /wp-includes/general-template.php and by replicating the default get_header() function that is already there I added this new custom function underneath :

function get_head( $name = null ) 
{

    do_action( 'get_head', $name );

    $templates = array();

    $name = (string) $name;

    if ( '' !== $name )
    {
        $templates[] = "head-{$name}.php";
    }

    $templates[] = 'head.php';

    if ('' == locate_template($templates, true))
    {
        load_template( ABSPATH . WPINC . '/theme-compat/head.php');
    }
}

I then called the above function in one of my Custom Page Template files and it worked fine.

Then Wordpress updated the /wp-includes/general-template.php file and my custom function got overwritten.

So my question is, where should I place the above function so that it never gets overwritten either by Wordpress updates or theme-specific updates?

And do I place the above function as is (in the new file you will suggest) without including anything more ? (or does the /wp-includes/general-template.php file include other stuff as well and that's why my function works form within that file and it will not work in some other file?)

Thank you for your help !!

  • Why are you doing this? Why not create a header-custom.php with your custom template code and then call get_header( 'custom' )? – TheDeadMedic Apr 24 '15 at 18:13
1

You should place this function into your theme's functions.php.

Never mess with the core WordPress files (anything in the wp-includes, wp-admin directory). An error can lead to a non functioning site, and the files get updated.

WordPress never updates your custom themes, so this is the way to go.

Of course, if you have a lot of functions, you can create a library directory in your theme and include all the files containing the functions.

You can also create a Plugin for your custom functions - but in your case, as your theme relies on the function, it should be included in the theme.

  • Thank you for your reply fischi. And like I said in my OP, do I simply paste in the above function into my functions.php in my custom theme ? Don't I need to include anything like @subpackage template etc ? – Sprout Coder Apr 24 '15 at 15:38
  • 1
    No, you just copy and paste it in your functions.php, and you are good to go. Maybe you will have to adjust the paths. – fischi Apr 24 '15 at 15:40
  • OK thanks a lot fischi that actually worked! What I did is I duplicated the default twentyfifteen theme folder, renamed the new folder, changed all the appropriate @...... in all of its files and the went into its functions.php file and pasted my function. So this functions.php file will never get updated/overwiteen because it resides in a custom theme folder whereas if I were using the functions.php file found in the twentyfifteen theme folder this could get updated/overwritten in the future by some twentyfifteen update correct? Just clarifying things in my head as I'm new to WP.. – Sprout Coder Apr 24 '15 at 15:51
  • 1
    Yes, that is correct. You should read into Child Themes and Plugin Development here - this would be a good starting point to show you the direction how you can alter WordPress. – fischi Apr 24 '15 at 15:52

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.