I am including a partial file both in header.php and in footer.php with get_template_part('content-form');

Is there an if clause I can use to check from where the file is being called? If it is called from within footer.php, then I would like to add a class name.

<div class="default-class <?php if (called_from_footer) echo 'footer-class'; ?>">

I could do this and style accordingly if there isn't a better solution, I am just curious:

<div class=footer-container">
    <?php get_template_part('content-form') ;?>

4 Answers 4


This is not a true solution to your problem (checking which template loaded another), but it will work to test if the footer has been loaded or not, and thus if it's loading your partial:

if ( did_action( 'get_footer' ) ) echo 'footer-class';
  • 2
    Might not be perfect but under such circumstances I probably would just do it and not bother with anything elaborate. :D
    – Rarst
    May 30, 2016 at 17:42

There are many good solutions for doing this, you should follow the link that cjbj has provided in the comments.

I am suggesting to use PHP's debug_backtrace() function:

function wpse_228223_verify_caller_file( $file_name, $files = array(), $dir = '' ) {

    if( empty( $files ) ) {
        $files = debug_backtrace();

    if( ! $dir ) {
        $dir = get_stylesheet_directory() . '/';

    $dir = str_replace( "/", "\\", $dir );
    $caller_theme_file = array();

    foreach( $files as $file ) {
        if( false !== mb_strpos($file['file'], $dir) ) {
            $caller_theme_file[] = $file['file'];

    if( $file_name ) {
        return in_array( $dir . $file_name, $caller_theme_file );




In your content-form template, pass the file name in the first param:

echo var_dump( wpse_228223_verify_caller_file( 'header.php' ) ); // called from header
echo var_dump( wpse_228223_verify_caller_file( 'footer.php' ) ); // called from footer

and there in your template you can add the appropriate class names..

Please give it few tests first. The way I tested it it worked fine. Since you are creating your own custom template which won't be called by default unless your call it, it should work fine.


Honestly, I think that best solution for your specific issue is the one form @TheDeadMedic.

It might be a little "fragile" because do_action('get_footer') can be done in any file... but what's not fragile in WordPress?

An alternative solution, just for "academic purpose" could be make use of PHP get_included_files() checking that footer.php was required:

function themeFileRequired($file) {

    $paths = array(

    $included = array_map('wp_normalize_path', get_included_files());

    $intersect = array_intersect($paths, $included);

    return ! empty($intersect);

And then:

<div class="default-class <?= if (themeFileRequired('footer') echo 'footer-class'; ?>">

If you really want it quick 'n dirty, just use a global variable (hey, WP is doing it all the time, so why can't you?). Set it before the call and read it afterwards. Like this:

In functions.php: global $contentform_origin = '';

In header.php: $contentform_origin = 'header'; get_template_part('content-form');

In footer.php: $contentform_origin = 'footer'; get_template_part('content-form');

In content-form.php: <div class="default-class <?php echo $contentform_origin ?>-class"> </div>

Don't forget to declare $contentform_origin at the beginning op each php file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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