Using WordPress ‘permalink_structure_changed’ PHP action

The permalink_structure_changed WordPress action fires after the permalink structure is updated.

Usage

add_action('permalink_structure_changed', 'your_custom_function', 10, 2);

function your_custom_function($old_permalink_structure, $permalink_structure) {
  // your custom code here

  return;
}

Parameters

  • $old_permalink_structure (string) – The previous permalink structure.
  • $permalink_structure (string) – The new permalink structure.

More information

See WordPress Developer Resources: permalink_structure_changed

Examples

Log the old and new permalink structure in a text file when it is changed.

function log_permalink_structure_changes($old_permalink_structure, $permalink_structure) {
  $log_file = 'permalink_changes_log.txt';
  $current_time = date('Y-m-d H:i:s');
  $log_message = "[$current_time] Old: $old_permalink_structure, New: $permalink_structure\n";

  file_put_contents($log_file, $log_message, FILE_APPEND);
}
add_action('permalink_structure_changed', 'log_permalink_structure_changes', 10, 2);

Send an email notification to the site administrator when the permalink structure is updated.

function email_admin_on_permalink_change($old_permalink_structure, $permalink_structure) {
  $to = get_option('admin_email');
  $subject = 'Permalink Structure Changed';
  $message = "The permalink structure has been updated:\nOld: $old_permalink_structure\nNew: $permalink_structure";

  wp_mail($to, $subject, $message);
}
add_action('permalink_structure_changed', 'email_admin_on_permalink_change', 10, 2);

Update a custom option in the database when the permalink structure is changed.

function update_custom_option_on_permalink_change($old_permalink_structure, $permalink_structure) {
  update_option('your_custom_option', $permalink_structure);
}
add_action('permalink_structure_changed', 'update_custom_option_on_permalink_change', 10, 2);

Clear the site cache when the permalink structure is updated.

function clear_cache_on_permalink_change($old_permalink_structure, $permalink_structure) {
  // Replace with your cache-clearing function
  your_cache_clearing_function();
}
add_action('permalink_structure_changed', 'clear_cache_on_permalink_change', 10, 2);

Post Update Notice on Permalink Structure Change

Display a notice on the admin dashboard when the permalink structure is changed.

function show_notice_on_permalink_change($old_permalink_structure, $permalink_structure) {
  set_transient('your_permalink_structure_changed_notice', true, 60 * 60 * 24);
}
add_action('permalink_structure_changed', 'show_notice_on_permalink_change', 10, 2);

function your_permalink_structure_changed_notice() {
  if (get_transient('your_permalink_structure_changed_notice')) {
    echo '<div class="notice notice-info is-dismissible"><p>Permalink structure has been updated.</p></div>';
    delete_transient('your_permalink_structure_changed_notice');
  }
}
add_action('admin_notices', 'your_permalink_structure_changed_notice');