Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Typically in my theme function file I'll require other files to keep things neat.

require_once("foo.php");

Now working in a child theme I'd like to do that same. I'm adding custom admin options and it seems impossible to include code. I've echoed out the path to make sure I'm calling the right file and it is calling the proper location but nothing inside that file seems to run. The code runs fine if placed inside the child theme functions file.

share|improve this question
    
Can you please post some more of your code to show what you are doing, exactly? –  MikeSchinkel Oct 26 '10 at 6:45
add comment

7 Answers

up vote 7 down vote accepted

Child themes reference parent themes by directory name, and in a normal install all your themes live in wp-content/themes/, so I'd say it's fine to reference those themes by their relative path:

include '../parent-theme/some-file.php';

If that makes you uncomfortable, I observe the following constants in WordPress 3.0.1 with a twentyten child theme called tt-child:

TEMPLATEPATH     /home/adam/public_html/wp3/wp-content/themes/twentyten
STYLESHEETPATH   /home/adam/public_html/wp3/wp-content/themes/tt-child

So you can do the following in your child theme to reference the parent theme directory:

include TEMPLATEPATH . '/some-file.php';
share|improve this answer
    
The constant STYLESHEETPATH is what I was looking for. Includes work in a child theme if you use that. –  curtismchale Oct 26 '10 at 14:21
    
I'm really surprised that you could even consider using a hard coded path... a theme is not supposed to work only in one environment. I could understand if get_template_directory() didn't exist... but it does exist. I just realised that it is a question from 2010 lol –  Christian Aug 23 '12 at 19:44
add comment

Hi @curtismchale:

Don't know if this is it or not, but you need to include quotes around foo.php, like so:

require_once('foo.php');

Does that solve your problem?

share|improve this answer
    
Ha good note. I missed it here but I had it in the child theme. –  curtismchale Oct 26 '10 at 14:15
add comment

Use <?php get_template_part( 'name-the-page-here' ); ?>. Without the extension .php

share|improve this answer
add comment

In a child theme the proper way is

require_once( get_stylesheet_directory() . 'foo.php');

While in the parent theme you can still use

require_once ( get_template_directory() . 'foo.php' );

get_template_directory() still works in the child theme, sadly target the parent theme directory. In your case it's useful

share|improve this answer
add comment

You definitely do not want to hard code the URL. The proper way of doing so is

require_once( get_stylesheet_directory(). '/my_included_file.php' );

See more info at Wordpress Codex

Now, if your e.g. modifying header.php which has an include you would reference it as follows:

require_once( get_stylesheet_directory() . '/../parenthteme/my_included_file.php' );
share|improve this answer
add comment

It is completely possible and normal to do includes in functions.php.

I do it like this in my child theme (php is subdirectory for code):

include 'php/core.php';

If you have issues without apparent reason try enabling debug mode in wp-config.php:

define('WP_DEBUG', true);

There might be relevant errors happening, but not displayed.

share|improve this answer
add comment

I think this is the best way to get your child theme path

<?php $get_my_path = dirname( get_bloginfo('stylesheet_url') ); ?>
<?php require_once( $get_my_path. '/my_included_file.php' ); ?>

Get idea from http://dynamicweblab.com/2013/02/get-template-path-in-wordpress-child-themes/

share|improve this answer
    
require_once needs a path, not an URL. –  toscho Mar 19 '13 at 21:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.