Advertisement
digitalfastmind

edit WP single post appending /edit to the URL

Jun 19th, 2025 (edited)
438
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.24 KB | Source Code | 0 0
  1. //edit single post via /edit on post
  2. function redirect_to_editor() {
  3.     // Check if the request contains /edit
  4.     if (strpos($_SERVER['REQUEST_URI'], '/edit') !== false) {
  5.         // Get the current URL without the /edit part
  6.         $current_url = $_SERVER['REQUEST_URI'];
  7.         $edit_url = str_replace('/edit', '', $current_url);
  8.  
  9.         // Remove any trailing slashes to prevent issues
  10.         $edit_url = rtrim($edit_url, '/');
  11.  
  12.         // Log the edit URL for debugging
  13.         error_log('Edit URL: ' . $edit_url);
  14.  
  15.         // Get the post ID from the URL
  16.         $post_id = url_to_postid($edit_url);
  17.  
  18.         // Log the post ID for debugging
  19.         error_log('Post ID: ' . $post_id);
  20.  
  21.         // Check if a valid post ID is found
  22.         if ($post_id) {
  23.             // Redirect to the post editor
  24.             wp_redirect(admin_url('post.php?post=' . $post_id . '&action=edit'));
  25.             exit;
  26.         } else {
  27.             // If post ID is not found, return a 404
  28.             global $wp_query;
  29.             $wp_query->set_404();
  30.             status_header(404);
  31.             nocache_headers();
  32.             include(get_404_template());
  33.             exit;
  34.         }
  35.     }
  36. }
  37. add_action('template_redirect', 'redirect_to_editor');
Tags: wordpress
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement