Code Snippets

This is a collection of useful code snippets that can be used to achieve certain things in Simply Static. Some of them will be included in Simply Static in the future. Others will stay as a code snippet.

Fire webhook after static export

You can use the following code snippet to send a request to an external service. I also added authentication as it’s needed for some services (GitHub, for example):

add_action( 'ss_after_cleanup', function() {
	$webhook_url = 'https://webhook-url.com';
	wp_remote_get( $webhook_url, array() );
});

Run simply static with WP-Cron

This code snippet can be used to run different kinds of exports with Simply Static without touching the UI at all.

register_activation_hook( __FILE__, 'ssp_setup_static_export_cron' );

/**
 * Setup a cron job to run daily.
 *
 * @return void
 */
function ssp_setup_static_export_cron() {
	if ( ! wp_next_scheduled( 'ssp_static_export_cron' ) ) {
		wp_schedule_event( time(), 'daily', 'ssp_static_export_cron' );
	}
}

add_action( 'ssp_static_export_cron', 'ssp_run_static_export_cron' );
/**
 * Run a full static export daily via WP-CRON.
 *
 * @return void
 */
function ssp_run_static_export_cron() {
    // Full static export
	$simply_static = Simply_Static\Plugin::instance();
	$simply_static->run_static_export();
}

Single Static Export after publishing a post

You may want to simplify your publishing workflow by automatically exporting a single page/post after you hit the publish button. Usually, this is done by clicking the “Export static” button within a post, but you can entirely automate that with the following code snippet:

add_filter( 'ssp_single_auto_export','__return_true' );

Add URLs with a specific shortcode in Single Exports

When updating your static website, you will most likely use Single Exports to push new content. Single Exports automatically include the homepage, blog page, archive-, tag-, and category pages of the exported post.

You may want to extend that list with all posts that contain a specific shortcode (maybe you have a recent post’s shortcode or something similar). The following code snippet will do exactly that, just replace [your-shortcode] with your own shortcode.

add_filter( 'ssp_single_export_additional_urls', function ( $related_urls ) {
	global $wpdb;

	// Get all pages that have a [your-shortcode] shortcode.
	$query    = "SELECT ID FROM " . $wpdb->posts . " WHERE post_content LIKE '%[your-shortcode%' AND post_status = 'publish'";
	$page_ids = $wpdb->get_results( $query );

	foreach ( $page_ids as $id ) {
		$related_urls[] = get_permalink( $id );
	}

	return $related_urls;
} );