admin管理员组文章数量:1130349
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
1 Answer
Reset to default 0Any 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.
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
1 Answer
Reset to default 0Any 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.
本文标签: plugin developmentWhere to write custom logs in WordPress
版权声明:本文标题:plugin development - Where to write custom logs in WordPress 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749121389a2319067.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论