When saving custom meta box inputs, I usually use get_current_screen() inside the function which hooks to save_post hook to determine current post’s post type1. However, a realisation comes when I create front end editing screen: get_current_screen() is only loaded on is_admin() environment and causes called undefined function message.

Some workarounds2 toward this:

  1. Assuming you’re only use the custom meta box on the dashboard screen, checking the existence of the function using function_exist() and return it if it fails is enough.
  2. If the custom meta box saving mechanism will be used on both dashboard and front end screen, using get_post() based on the post ID posted and checking its $post->post_type will do the work.

  1. So the function is only used when they’re on the appropriate post type 

  2. And pattern that need to be used from this time forward 

Share Your Thought