Taken out of context:
<?php
$options=file_get_contents('options.txt');
?>
This isn't working. 'options.txt' is in the same directory as the plugin .php. What am I doing wrong? I thought that since the .php is being included from somewhere else, the path would be wrong, so I tried '/wp-content/plugins/开发者_Python百科myPlugin/options.txt'. But nothing...
You should really use the appropriate WordPress Plugin and Content Directories function to obtain directory paths to ensure that your plugin will work on future versions of WordPress, etc.
Incidentally, you do realise that this file will technically be publically viewable via a URL? (Just mentioning it in case it contains anything even vaguely sensitive.)
Use the path relative to the Wordpress root dir without a preceding slash:
wp-content/plugins/myPlugin/options.txt
I would strongly recommend against using the native file_get_contents()
method. Not every server setup will allow this, so if you ever migrate your site or give the code to someone else, you might run into problems.
Instead, use the built-in HTTP API that comes with WordPress. Your specific example would become:
$options = wp_remote_retrieve_body( wp_remote_get( plugins_url() . '/my_plugin/options.txt' ) );
This will use the built-in API to GET the text file and then retrieve the body of the GET request (store the text file's contents in the $options
variable.
精彩评论