Created
January 22, 2026 16:24
-
-
Save mlbd/fadc5ee2fb2664afdc2f0c505bbcbb86 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add_action('load-post-new.php', function () { | |
| if (!is_admin()) return; | |
| $post_type = $_GET['post_type'] ?? 'post'; | |
| if ($post_type !== 'page') return; | |
| // ✅ Change this to your role slug(s) | |
| $target_roles = ['program_editor']; // e.g. ['author'], ['shop_manager'], etc. | |
| $user = wp_get_current_user(); | |
| if (!$user || empty($user->roles)) return; | |
| // Admins unaffected | |
| if (user_can($user, 'administrator')) return; | |
| $is_target = (bool) array_intersect($target_roles, (array) $user->roles); | |
| if (!$is_target) return; | |
| // ✅ Optional cleanup: delete old auto-drafts by this user (avoid "continue editing" behavior) | |
| $old_ids = get_posts([ | |
| 'post_type' => 'page', | |
| 'post_status' => 'auto-draft', | |
| 'author' => get_current_user_id(), | |
| 'fields' => 'ids', | |
| 'posts_per_page' => 50, | |
| 'orderby' => 'date', | |
| 'order' => 'DESC', | |
| ]); | |
| // Keep the most recent 0 auto-drafts (delete all) | |
| foreach ($old_ids as $old_id) { | |
| wp_delete_post($old_id, true); | |
| } | |
| // ✅ Create a fresh empty auto-draft | |
| $new_id = wp_insert_post([ | |
| 'post_type' => 'page', | |
| 'post_status' => 'auto-draft', | |
| 'post_author' => get_current_user_id(), | |
| 'post_title' => '', | |
| ], true); | |
| if (is_wp_error($new_id) || !$new_id) return; | |
| // ✅ Hard safety: wipe any meta that some plugins might auto-copy | |
| $keep = [ | |
| '_edit_lock', | |
| '_edit_last', | |
| '_wp_page_template', | |
| ]; | |
| $all_meta = get_post_meta($new_id); | |
| if (is_array($all_meta)) { | |
| foreach (array_keys($all_meta) as $key) { | |
| if (in_array($key, $keep, true)) continue; | |
| delete_post_meta($new_id, $key); | |
| } | |
| } | |
| // Redirect straight to edit screen (works for Classic + Block editor) | |
| wp_safe_redirect(admin_url('post.php?post=' . $new_id . '&action=edit')); | |
| exit; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment