$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - Where to write custom logs in WordPress|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugin development - Where to write custom logs in WordPress

matteradmin9PV0评论

I am creating a plugin in WordPress, the plugin creates logs for some events, my question is where should I write custom logs, so that no explicit file read/write permission required. I am looking to put logs at one of below two locations-
1. wp-content/plugins/pluginName/logs
2. wp-content/logs

I am creating a plugin in WordPress, the plugin creates logs for some events, my question is where should I write custom logs, so that no explicit file read/write permission required. I am looking to put logs at one of below two locations-
1. wp-content/plugins/pluginName/logs
2. wp-content/logs

Share Improve this question edited Dec 6, 2018 at 15:33 Nathan Johnson 6,5386 gold badges30 silver badges49 bronze badges asked Dec 6, 2018 at 15:13 Surjan RaghuwanshiSurjan Raghuwanshi 1631 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Any files you want to create, such as logs, should be created in the wp-content/uploads/ directory. Probably in your own subdirectory. This is because this is directory will be the most reliably writable, since it needs to be writable for everyday usage (for uploading media).

You can use wp_upload_dir() to get the path to the uploads directory, and mkdir() to create a directory there:

$uploads  = wp_upload_dir( null, false );
$logs_dir = $uploads['basedir'] . '/pluginName-logs';

if ( ! is_dir( $logs_dir ) ) {
    mkdir( $logs_dir, 0755, true );
}

$file = fopen( $logs_dir . '/' . 'log.log', 'w' );

wp-content would be the next option, but I don't see any reason to prefer it over the uploads directory. Definitely don't put it inside your plugin directory. If you did you would lose any logs whenever the plugin is updated.

Post a comment

comment list (0)

  1. No comments so far