How to write Flowmattic output to TXT file?

Tech Articles | February 2, 2025 | Automation, Blog, Coding, FlowMattic, Wordpress

Following a question on the Flowmattic Facebook group about a user who was using Newoaks to transcribe YouTube Videos and Podcasts, I created this little quick tip

What they wanted to do was be able to store the output in text files to pass over, and they asked how they could achieve this, so I decided to create a little PHP function to do this and also to clear up the generated files every 24 hours.

The flow in the video uses fixed data as it wasn’t necessary to create a dynamic example for the video. The purpose is to write the data to the text file, and you could substitute the fixed data for dynamic easily

You can see in the above PHP function step that it calls write_random_text_file and the content is the passed variable. Once this is passed to the below PHP script, it writes a text file based on the format of text_date_time_random.txt, and this is uploaded to wp-content/text-files/, which can then be passed on in your flow or passed as an external URL over an API or external webhook.

<?php
// Function to write a text file
function write_random_text_file($content) {
    $upload_dir = wp_upload_dir();
    $text_dir = WP_CONTENT_DIR . '/text-files/';

    // Ensure the directory exists
    if (!file_exists($text_dir)) {
        wp_mkdir_p($text_dir);
    }

    // Generate a unique filename based on date and time
    $filename = 'text_' . date('Ymd_His') . '_' . wp_generate_password(8, false) . '.txt';
    $file_path = $text_dir . $filename;

    // Write content to the file
    file_put_contents($file_path, $content);

    return $filename;
}

// Function to delete text files older than 24 hours
function delete_old_text_files() {
    $text_dir = WP_CONTENT_DIR . '/text-files/';
    
    if (!file_exists($text_dir)) {
        return;
    }

    $files = glob($text_dir . '*.txt'); // Get all .txt files
    $now = time();

    foreach ($files as $file) {
        if (is_file($file) && ($now - filemtime($file) > 86400)) { // 24 hours = 86400 seconds
            unlink($file); // Delete the file
        }
    }
}

// Schedule the cleanup event
function schedule_text_file_cleanup() {
    if (!wp_next_scheduled('delete_old_text_files_hook')) {
        wp_schedule_event(time(), 'daily', 'delete_old_text_files_hook');
    }
}
add_action('wp', 'schedule_text_file_cleanup');
add_action('delete_old_text_files_hook', 'delete_ol
PHP

You can see the code in action on the below youtube video and come back here for the code to run the solution

Support the Author

buy me a coffee
Really Useful Plugin Logo
Appoligies for any spelling and grammer issue. As a dyslexic i need to rely on tools for this they like me are not perfect but I do try my best