Wordpress - Check if partial file is called from within header.php or footer.php

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';

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 );
    }

    return;

}

Usage:

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(
        wp_normalize_path(get_stylesheet_directory().'/'.$file.'.php'),
        wp_normalize_path(get_template_directory().'/'.$file.'.php'),
    );

    $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'; ?>">
</div>