Private Log File Helper Function
Adds a wpide_log() helper that writes timestamped messages and data to a daily log file in a protected folder inside uploads, so custom code can leave a trail without touching debug.log.
Opens WPIDE on
Code
if ( ! function_exists( 'wpide_log' ) ) {
function wpide_log( $message, $context = array(), $level = 'info' ) {
$folder = get_option( 'wpide_log_dir' );
if ( ! $folder ) {
$folder = 'wpide-logs-' . wp_generate_password( 12, false );
update_option( 'wpide_log_dir', $folder, false );
}
$uploads = wp_upload_dir( null, false );
$dir = trailingslashit( $uploads['basedir'] ) . $folder;
if ( ! is_dir( $dir ) ) {
if ( ! wp_mkdir_p( $dir ) ) {
return false;
}
file_put_contents( $dir . '/.htaccess', "Require all denied\nDeny from all\n" );
file_put_contents( $dir . '/index.php', "<?php\n// Silence is golden.\n" );
}
$line = sprintf(
"[%s] %s: %s%s\n",
wp_date( 'Y-m-d H:i:s' ),
strtoupper( sanitize_key( $level ) ),
is_scalar( $message ) ? (string) $message : wp_json_encode( $message ),
$context ? ' ' . wp_json_encode( $context, JSON_PARTIAL_OUTPUT_ON_ERROR ) : ''
);
return false !== file_put_contents( $dir . '/log-' . wp_date( 'Y-m-d' ) . '.log', $line, FILE_APPEND | LOCK_EX );
}
}
Notes
Example wpide_log( 'Order synced', array( 'id' => $order_id ), 'info' ); the folder name contains a random suffix and blocks direct web access on Apache.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.