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.

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
add comment

2 Answers

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. –  songdogtech Nov 22 '12 at 18:45
add comment

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. –  songdogtech 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. –  songdogtech Nov 22 '12 at 18:58
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.