Skip to content

Instantly share code, notes, and snippets.

@Pebblo
Last active February 11, 2021 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pebblo/a6d2b949d0ae784c09de442b9848ff7d to your computer and use it in GitHub Desktop.
Save Pebblo/a6d2b949d0ae784c09de442b9848ff7d to your computer and use it in GitHub Desktop.
Example plugin to log the rewrite_rules to debug.log. WP_DEBUG_LOG needs to be enabled on the site.
<?php
/*
* Plugin Name: rewrite_rules log plugin
* Description: This plugin adds a log entry to debug.log each time permalinks are flushed.
* Author: Tony Warwick
* Version: 1.0.0
*/
// ==== Debug ====
if ( ! function_exists('ee_write_log')) {
function ee_write_log ( $log ) {
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
error_log( $log );
}
}
}
// ==== Function to log rewrite_rule ====
function tw_log_rewrite_rules_array($rewrite_array)
{
$e = new \Exception;
ee_write_log(
array(
'time' => date("Y-m-d H:i:s"),
'rewrite_array' => $rewrite_array,
'stacktrace' => $e->getTraceAsString(),
'EE-Check' => isset($rewrite_array['events/?$']) ? 'true' : 'false'
)
);
return $rewrite_array;
}
add_filter('rewrite_rules_array', 'tw_log_rewrite_rules_array', 10, 1);
<?php // Do not include the opening PHP tag if you alreayd have one.
// Use this function with the above to confirm what the rewrite rules are when a request returns 404
// This function checks if the current request URI contains 'event' to coverthe default events slug
// if you have change the slug you'll need to update it in the function.
function tw_ee_log_rewrite_rules_on_404() {
// Hooking into wp_footer so we can uss is_404()
if( is_404() ) {
// So have a 404, lets checks its for an EE event using the global $wp->request
global $wp;
if( strpos($wp->request, 'event') !== false) {
// Request contains 'event', pull the rewrite rules.
$rules = get_option( 'rewrite_rules' );
// Write the rules to the log.
ee_write_log(
array(
'rewrite rules on 404' => $rules,
'request' => $wp->request,
)
);
}
}
}
add_action('wp_footer', 'tw_ee_log_rewrite_rules_on_404');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment