1 min read

Add A CSS Style Sheet For WP Admin Section

1. Define $theme_version within the function

You can define $theme_version within your function by getting the current theme’s version using the wp_get_theme() function:

function my_custom_admin_styles() {
    $theme_data = wp_get_theme();
    $theme_version = $theme_data->get('Version');
    wp_enqueue_style('amdminmain', get_theme_file_uri('assets/main.css'), array(), $theme_version, 'all');
}
add_action('admin_enqueue_scripts', 'my_custom_admin_styles');

2. Use a static value for the version

If you prefer not to dynamically fetch the theme version, you can simply use a static value like '1.0.0' or increment it whenever you update your CSS file:

function my_custom_admin_styles() {
    wp_enqueue_style('amdminmain', get_theme_file_uri('assets/main.css'), array(), '1.0.0', 'all');
}
add_action('admin_enqueue_scripts', 'my_custom_admin_styles');

Either approach will fix the undefined variable notice you are seeing, and your style should be enqueued correctly in the admin area.

Also, ensure there is no typo in the handle name 'amdminmain'; it looks like it might be a typo for 'adminmain'. Make sure it matches your intended naming convention.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.