Advertisement
ofmarconi

Flush Links

May 5th, 2024
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.65 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Plugin Name: Network Flush Rewrite Rules Button
  4.  * Description: Adds a button to the WordPress admin bar to flush rewrite rules across all sites in a Multisite network for Editors and Administrators. Reminds to clear site cache after flushing.
  5.  * Version: 1.0
  6.  * Network: true
  7.  */
  8.  
  9. add_action('admin_bar_menu', 'add_network_flush_button', 100);
  10.  
  11. /**
  12.  * Adds a button to the WordPress admin bar if the user is an editor or administrator.
  13.  */
  14. function add_network_flush_button($wp_admin_bar) {
  15.     if (!is_user_logged_in()) {
  16.         return;
  17.     }
  18.     if (!current_user_can('edit_others_posts')) { // Checks if the user can edit other users' posts, common among editors and administrators
  19.         return;
  20.     }
  21.  
  22.     $args = array(
  23.         'id'    => 'network-flush-rewrite',
  24.         'title' => 'Flush Network Rewrites',
  25.         'href'  => '#',
  26.         'meta'  => array(
  27.             'onclick' => 'flushNetworkRewrites();',
  28.             'class'   => 'flush-rewrite-button',
  29.             'title'   => 'Flush Rewrite Rules Across All Sites'
  30.         )
  31.     );
  32.     $wp_admin_bar->add_node($args);
  33. }
  34.  
  35. add_action('admin_footer', 'flush_rewrite_javascript');
  36.  
  37. /**
  38.  * JavaScript to handle the flush action when the button is clicked.
  39.  */
  40. function flush_rewrite_javascript() {
  41.     ?>
  42.     <script type="text/javascript">
  43.         function flushNetworkRewrites() {
  44.             if (confirm("Are you sure you want to flush rewrite rules across all network sites? This action can affect site performance.")) {
  45.                 window.location.href = "<?php echo admin_url('admin-post.php?action=flush_network_rewrites'); ?>";
  46.             }
  47.         }
  48.     </script>
  49.     <?php
  50. }
  51.  
  52. add_action('admin_post_flush_network_rewrites', 'handle_network_flush_rewrite');
  53.  
  54. /**
  55.  * Handles the flush action when the button is clicked.
  56.  */
  57. function handle_network_flush_rewrite() {
  58.     if (!current_user_can('edit_others_posts')) {
  59.         wp_die('You do not have sufficient permissions to perform this action.');
  60.     }
  61.  
  62.     flush_network_rewrite_rules(); // Assumes the function is defined in previous examples
  63.     wp_redirect(add_query_arg(['flushed' => '1'], admin_url()));
  64.     exit;
  65. }
  66.  
  67. add_action('admin_notices', 'show_flush_notification');
  68.  
  69. /**
  70.  * Shows a notification after the flush operation has been completed.
  71.  */
  72. function show_flush_notification() {
  73.     if (isset($_GET['flushed']) && $_GET['flushed'] == '1') {
  74.         echo '<div class="notice notice-success is-dismissible"><p>Flush completed. Do not forget to clear the "Site Cache".</p></div>';
  75.     }
  76. }
  77.  
  78. // The function flush_network_rewrite_rules is assumed to be defined in previous examples
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement