Last active
July 19, 2024 02:07
-
-
Save karamgaby/bd64440e798a105c217894b461e5327e to your computer and use it in GitHub Desktop.
WordPress PHP script to export WP Store Locator plugin stores into a csv file. With customisation this can work with any post type in WordPress
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This is a quick and ready for production script. | |
* | |
* Include this script at the bottom of your functions.php file, e.g. | |
* | |
* ```php | |
* // Your functions.php file | |
* // ... | |
* | |
* include_once "export-store-locations.php" | |
* ``` | |
* | |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
* SOFTWARE. | |
*/ | |
// Redirect to 404 if script is accessed directly | |
if ( ! defined("ABSPATH")) { | |
header("Location: http://{$_SERVER['HTTP_HOST']}/404"); | |
die; | |
}; | |
/** | |
* Show insert posts button in admin backend | |
*/ | |
add_action("admin_notices", function () { | |
?> | |
<div class='updated'> | |
<p> | |
To export the store locations from the database, click the button to the right. | |
<a class='button button-primary' style="margin:0.25em 1em" | |
href="<?php echo admin_url('admin-post.php?action=export_stores.csv') ?>"> | |
Export Stores | |
</a> | |
</p> | |
</div> | |
<?php | |
}); | |
add_action('admin_post_export_stores.csv', 'export_stores_csv'); | |
function export_stores_csv() | |
{ | |
if ( ! current_user_can('manage_options')) { | |
return; | |
} | |
$output_filename = 'export_' . strftime('%Y-%m-%d') . '.csv'; | |
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); | |
header('Content-Description: File Transfer'); | |
header('Content-type: text/csv'); | |
header('Content-Disposition: attachment; filename=' . $output_filename); | |
header('Expires: 0'); | |
header('Pragma: public'); | |
// output the CSV data | |
/** | |
* Export fields: array of strings representing $post properties to output to CSV. | |
*/ | |
$export_fields = array( | |
'post_title', | |
); | |
$export_meta_fields = array( | |
'wpsl_address', | |
'wpsl_city', | |
'wpsl_state', | |
'wpsl_country', | |
'wpsl_lat', | |
'wpsl_lng', | |
'wpsl_phone', | |
); | |
/** | |
* Export query parameters for WP_Query | |
* @link http://codex.wordpress.org/Function_Reference/WP_Query WP_Query parameter reference | |
*/ | |
$export_query = array( | |
'posts_per_page' => -1, | |
'post_status' => 'publish', | |
'post_type' => 'wpsl_stores', | |
); | |
/** | |
********************************************************************* | |
* Data export | |
********************************************************************* | |
*/ | |
// Posts query | |
$posts = new WP_Query($export_query); | |
$posts = $posts->posts; | |
// Output file stream | |
$output_handle = @fopen('php://output', 'w'); | |
fputcsv($output_handle, array( | |
'Permalink', | |
'Church Title', | |
'Address', | |
'City', | |
'State', | |
'Country', | |
'Latitude', | |
'Longitude', | |
'Phone', | |
)); | |
// Output data to file | |
foreach ($posts as $post) { | |
// Get post permalink | |
$permalink = get_post_permalink($post->ID); | |
// Build export array | |
$post_export = array($permalink); | |
$meta = get_post_meta($post->ID); | |
foreach ($export_fields as $export_field) { | |
$post_export[] = $post->$export_field; | |
} | |
foreach ($export_meta_fields as $export_meta_field) { | |
$post_export[] = $meta[$export_meta_field][0]; | |
} | |
// Add row to file | |
fputcsv($output_handle, $post_export); | |
} | |
// Close output file stream | |
fclose($output_handle); | |
// We're done! | |
exit; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Super helpful, thank you!