Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Plugin Name: Network Flush Rewrite Rules Button
- * 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.
- * Version: 1.0
- * Network: true
- */
- add_action('admin_bar_menu', 'add_network_flush_button', 100);
- /**
- * Adds a button to the WordPress admin bar if the user is an editor or administrator.
- */
- function add_network_flush_button($wp_admin_bar) {
- if (!is_user_logged_in()) {
- return;
- }
- if (!current_user_can('edit_others_posts')) { // Checks if the user can edit other users' posts, common among editors and administrators
- return;
- }
- $args = array(
- 'id' => 'network-flush-rewrite',
- 'title' => 'Flush Network Rewrites',
- 'href' => '#',
- 'meta' => array(
- 'onclick' => 'flushNetworkRewrites();',
- 'class' => 'flush-rewrite-button',
- 'title' => 'Flush Rewrite Rules Across All Sites'
- )
- );
- $wp_admin_bar->add_node($args);
- }
- add_action('admin_footer', 'flush_rewrite_javascript');
- /**
- * JavaScript to handle the flush action when the button is clicked.
- */
- function flush_rewrite_javascript() {
- ?>
- <script type="text/javascript">
- function flushNetworkRewrites() {
- if (confirm("Are you sure you want to flush rewrite rules across all network sites? This action can affect site performance.")) {
- window.location.href = "<?php echo admin_url('admin-post.php?action=flush_network_rewrites'); ?>";
- }
- }
- </script>
- <?php
- }
- add_action('admin_post_flush_network_rewrites', 'handle_network_flush_rewrite');
- /**
- * Handles the flush action when the button is clicked.
- */
- function handle_network_flush_rewrite() {
- if (!current_user_can('edit_others_posts')) {
- wp_die('You do not have sufficient permissions to perform this action.');
- }
- flush_network_rewrite_rules(); // Assumes the function is defined in previous examples
- wp_redirect(add_query_arg(['flushed' => '1'], admin_url()));
- exit;
- }
- add_action('admin_notices', 'show_flush_notification');
- /**
- * Shows a notification after the flush operation has been completed.
- */
- function show_flush_notification() {
- if (isset($_GET['flushed']) && $_GET['flushed'] == '1') {
- echo '<div class="notice notice-success is-dismissible"><p>Flush completed. Do not forget to clear the "Site Cache".</p></div>';
- }
- }
- // The function flush_network_rewrite_rules is assumed to be defined in previous examples
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement