Sarathlal N

Add custom style sheet on new post or edit post admin pages - WordPress

In one of my recent WordPress customisation, I want to add custom style sheet on new post and edit post admin pages.

To do so, we can use below code snippet in our theme functions file or plugin functions file.

/**
 * is_edit_page 
 * function to check if the current page is a post edit page
 */
function is_edit_page($new_edit = null){
	global $pagenow;
	//make sure we are on the backend
	if (!is_admin()) return false;

	if($new_edit == "edit")
		return in_array( $pagenow, array( 'post.php',  ) );
	elseif($new_edit == "new") //check for new post page
		return in_array( $pagenow, array( 'post-new.php' ) );
	else //check for either new or edit
		return in_array( $pagenow, array( 'post.php', 'post-new.php' ) );
}

/**
 * Adds the meta box stylesheet when appropriate
 */
function add_metabox_admin_style( $hook ) {
   if (is_edit_page()){ 
		wp_enqueue_style( 'prfx_meta_box_styles', get_template_directory_uri() . '/css/meta-box-styles.css' );
   }
}
add_action( 'admin_print_styles', 'add_metabox_admin_style', 10, 1 );

If you plan to add style sheet from theme, use the code as it is. If you have to add style sheet from custom plugin, use plugin_dir_url( __FILE__ ) instead of get_template_directory_uri() that will return current plugin URL.

Looking for a skilled WordPress/WooCommerce developer? I'm currently available for freelance, contract, or full-time remote opportunities! Let's create something amazing together. Send me a quick message, and I'll respond within 24 hours!

Recent Posts

  1. Scaling WordPress - How Custom Database Tables Solve the Post Meta Bottleneck
  2. WordPress Transients Explained - A Developer's Guide to Site Performance
  3. Behind the Click - The Hidden Journey of Your Web Requests
  4. Automating Code Linting with GitHub Actions for WordPress Plugins
  5. Comprehensive Guide to Linting PHP, JavaScript, and CSS in WordPress Plugins Using Composer

Your Questions / Comments

If you found this article interesting, found errors, or just want to discuss about it, please get in touch.