Skip to content

Instantly share code, notes, and snippets.

@damiencarbery
Last active June 7, 2020 00:01
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save damiencarbery/d3023dd2c3e4ac4b49a20272dc14f5d6 to your computer and use it in GitHub Desktop.
Standalone WordPress Scripts
<?php
define('WP_USE_THEMES', false);
require( 'wp-blog-header.php' );
bloginfo( 'name' );
<?php
// Restrict who can access this script.
$permitted_ips = array('12.34.56.78', '87.65.43.21');
if (in_array($_SERVER['REMOTE_ADDR'], $permitted_ips) == false) {
header('HTTP/1.0 403 Forbidden');
die();
}
define('WP_USE_THEMES', false);
/** Loads the WordPress Environment */
//require( dirname( __FILE__ ) . '/wp-blog-header.php' );
require( 'wp-blog-header.php' );
bloginfo( 'name' );
# Limit access to a single script.
<Files basic-with-ip-restriction.php>
order deny,allow
deny from all
allow from 12.34.56.78, 87.76.43.21
</Files>
# Limit access to a number of similarly named files.
<FilesMatch "basic-.*\.php">
order deny,allow
deny from all
allow from 12.34.56.78, 87.76.43.21, 79.97.245.78
</FilesMatch>
<?php $time_start = microtime(true); ?>
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8" />
<title>Woocommerce - Get Some Product IDs</title>
<meta name="viewport" content="width=device-width, initial-scale=1" />
</head>
<body>
<h1>Woocommerce - Get Some Product IDs</h1>
<?php
define('WP_USE_THEMES', false);
require( dirname( __FILE__ ) . '/wp-blog-header.php' );
// WooCommerce not automatically loaded so load it manually.
require_once WP_PLUGIN_DIR.'/woocommerce/woocommerce.php';
new WooCommerce();
// Get list of product IDs.
$args = array('post_type'=>'product', 'fields' => 'ids', 'no_found_rows' => true, 'update_post_term_cache' => false, 'update_post_meta_cache' => false);
$products_query = new WP_Query($args);
$product_ids = array();
// No need for a loop because we only requested the 'id' field!
$product_ids = $products_query->posts;
wp_reset_postdata();
// Display the product IDs
echo '<pre>', var_export($product_ids), '</pre>';
// Display some stats.
echo '<p>Memory usage: ', intval(memory_get_usage() / (1024 * 1024)), "MB\n";
echo '<br />Peak memory usage: ', intval(memory_get_peak_usage() / (1024 * 1024)), "MB</p>\n";
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "<p>Process Time: {$time} seconds.</p>";
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment