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


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>';
  • Works great and is the simplest way to pull that file in; thanks. 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.

  • Thanks, that should work. get_template_part actually uses locate_template(), so toscho's idea works more directly. 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. Nov 22 '12 at 18:58

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.