WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How do I load the contents of a text file - or any other file, like .php - and use the test for the login_message hook that prints a message above the #login box in wp-login.php?

(BTW, this is in a child theme, if it makes a difference.)

function custom_login_message() {

$message = get_bloginfo('stylesheet_directory')."/tos.txt";
return $message;
add_filter('login_message', 'custom_login_message');

Right now, the function above prints out the URL of the file, not the text in the file, i.e.:

enter image description here

share|improve this question
up vote 1 down vote accepted

Use locate_template() if you want to use a file from your theme. include() or require() works too.

Sample code, tested:

add_filter( 'login_message', 'wpse_73619_include_login_message' );

function wpse_73619_include_login_message()
    print '<pre>';
    locate_template( 'style.css', TRUE );
    print '</pre>';
share|improve this answer
Works great and is the simplest way to pull that file in; thanks. – markratledge Nov 22 '12 at 18:45

Your function just constructs the URL. You'd need to include the file to get at its contents. You may be able to do this with get_template_part. I am not sure about the hook context. However, your file would need to generate a PHP variable or it will echo immediately rather than return.

If you are using a text file, you could look into one of PHP's file functions, like get_file_contents for more complicated manipulation of the file contents before your echo/return it. Or use output buffering.

share|improve this answer
Thanks, that should work. get_template_part actually uses locate_template(), so toscho's idea works more directly. – markratledge Nov 22 '12 at 18:47
locate_template will fail in places that get_template_part does not. If you look at the source for them both you will see that get_template_part does more work to find the file. This thread is an example. – s_ha_dum Nov 22 '12 at 18:51
You're right, good point. This is for use in a child theme. But I can see the difference now. – markratledge Nov 22 '12 at 18:58

Your Answer


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.