exists( $htaccess_file ) && $wp_filesystem->is_writable( $htaccess_file ) ) { $htaccess_content = $wp_filesystem->get_contents( $htaccess_file ); if ( strpos( $htaccess_content, 'Options -Indexes' ) === false ) { $wp_filesystem->put_contents( $htaccess_file, $htaccess_code . $htaccess_content, FS_CHMOD_FILE ); } } else { // Log error if .htaccess is missing or not writable. if ( function_exists( 'ns_shield_debug_log' ) ) { ns_shield_debug_log( 'The .htaccess file does not exist or is not writable.' ); } } } else { // If directory indexing is enabled, remove the "Options -Indexes" directive. if ( $wp_filesystem->exists( $htaccess_file ) && $wp_filesystem->is_writable( $htaccess_file ) ) { $htaccess_content = $wp_filesystem->get_contents( $htaccess_file ); $updated_content = str_replace( "Options -Indexes\n", '', $htaccess_content ); $wp_filesystem->put_contents( $htaccess_file, $updated_content, FS_CHMOD_FILE ); } } } add_action( 'init', 'ns_shield_disable_directory_indexing' );