Skip to content

Instantly share code, notes, and snippets.

@bomsn
Created July 30, 2022 11:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bomsn/e41f9e23af24c8c123534643da8ad2c8 to your computer and use it in GitHub Desktop.
Save bomsn/e41f9e23af24c8c123534643da8ad2c8 to your computer and use it in GitHub Desktop.
Export AutomateWoo plugin optin emails ( for WordPress - WooCommerce )
<?php
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename="optins-data-' . date("d/m/Y") . '.csv"');
header('Pragma: no-cache');
header('Expires: 0');
$file = fopen('php://output', 'w');
fputcsv($file, array('Name', 'Email'));
$query = new AutomateWoo\Customer_Query();
if (AutomateWoo\Options::optin_enabled()) {
$query->where('subscribed', true);
$query->set_ordering('subscribed_date');
} else {
$query->where('unsubscribed', true);
$query->set_ordering('unsubscribed_date');
}
$query->set_calc_found_rows(true);
$query->set_limit(4000);
$query->set_page(1);
$results = $query->get_results();
if (!empty($results)) {
foreach ($results as $customer) {
$name = esc_attr( $customer->get_full_name() );
$email = esc_attr( $customer->get_email() );
fputcsv($file, array($name, $email));
}
}
fclose($file);
exit();
Copy link

ghost commented Dec 27, 2022

Where should I place this code to run? And where the file will be created?

@bomsn
Copy link
Author

bomsn commented Dec 27, 2022

You should place this code inside a function and wrap it in a condition to call it when you need. This will not work out of the box for you. So you'll need some understanding of PHP and WP Hooks.

This is a sample code

function restrict_admin_with_redirect() {
        if (isset($_GET['custom_action'])) {
            // some code to execute here
        }
}

add_action( 'admin_init', 'restrict_admin_with_redirect', 1 );

When the full code is added to functions.php file of the child theme, run any page on admin area with the query parameter custom_action in the URL (eg; domain.com/wp-admin/?custom_action=1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment