[wp-trac] [WordPress Trac] #41235: New load_screen_** action

WordPress Trac noreply at wordpress.org
Tue Jul 4 04:03:55 UTC 2017


#41235: New load_screen_** action
--------------------------+-----------------------------
 Reporter:  aussieguy123  |      Owner:
     Type:  enhancement   |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  trunk
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 WordPress includes a load-<page> hook, however this is not specific enough
 to have a function that runs only on one single screen in the admin area
 without additional logic. Lets say I have a custom post type
 "killed_attachment" and I want to have a function which will only run on
 the edit posts screen for this post type.

 My proposed solution:

 {{{#!php
 <?php
 /**
  * Fire load_screen_* hooks
  * @action admin_init (or a better action, suggestions?)
  */
  public function run_load_screen_hooks() {
         $current_screen = function_exists( 'get_current_screen' ) ?
 get_current_screen()->id : null;
         do_action('load_screen_' . $current_screen);
  }
 }}}

 With this new action, instead of requiring logic to detect the screen as
 in:
 {{{#!php
 <?php
         /**
          * @action admin_init
          */
         public function run_on_killed_attachments_edit_post_only() {
                 $current_screen = function_exists( 'get_current_screen' )
 ? get_current_screen()->id : null;

                 switch ( $current_screen ) {
                         case 'edit-killed_attachment':
                                 //Code which will only run on the killed
 attachment edit posts screen
                                 break;
                         default:
                                 break;
                 }
      }
 }}}

 You could simplify this as:
 {{{#!php
 <?php
         /**
          * @action load_screen_edit-killed_attachment
          */
         public function admin_enqueue_scripts() {
             //Code which will only run on the killed attachment edit posts
 screen
         }
 }}}

 Basically both functions do the same thing but one is 9 lines long and
 contains extra logic, the other is one line long.

 Shorter functions and less logic means less bugs and more readable +
 maintainable code.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/41235>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list