• Resolved Karis

    (@askel45)


    Hi. This is some code I got from Tom to enqueue block editor CSS in my custom plugin. However, I would like to ask for your help to shorten the URL in a way that it does not rely on the plugin-name in the case where the plugin name is changed so it does not throw an error. I don’t know how that functions works and I saw it in the past but Google has not been fruitful. The style does not work without the full path, but I would like to truncate the part shown in the code due to the reason stated above. Thanks in advance.

    add_action( 'enqueue_block_editor_assets', function() {
        wp_enqueue_style( 'editor', '/wp-content/plugins/plugin-name/assets/css/editor-style.css', array(), '1.0');
    } );
Viewing 5 replies - 1 through 5 (of 5 total)
  • Theme Author Tom

    (@edge22)

    Hi there,

    Just to confirm, you’re wanting to enqueue an asset inside a specific plugin without referencing the path to that plugin?

    Thread Starter Karis

    (@askel45)

    Hi Tom. I think that is what I want. Like how the editor styles work straight out of the theme without including the theme’s name when enqueuing them. In this case, the block editor style CSS file.

    Theme Author Tom

    (@edge22)

    The code would need to be in the plugin itself (or the theme) for that to work. It’s not possible to enqueue an asset without explicitly providing a URL to enqueue. When working inside the plugin or theme, we can use PHP functions to get the URL. From outside the plugin/theme, I don’t believe there’s a way to do it.

    Thread Starter Karis

    (@askel45)

    @edge22 the code is actually inside the plugin (related issue). But it does not work unless the full URL is entered (hence the /wp-content/plugin-name/ section of the code. But in cases where I might change the plugin’s name and I forgot about that code, that might not work. (Sorry I was confusing the CSS enqueuing process and the process of including a PHP file which causes an error if that file does not exist)

    Edit: I found it is working this time using plugins_url( '/assets/css/editor-style.css', __FILE__ ) when enqueuing the file. As reported in the related issue, I have discovered it was a cache issue and I added array(), 'insert number here') to the code and it purges the cache when I updated the version number. You can ignore this now. Thanks.

    Theme Author Tom

    (@edge22)

    Glad you got it sorted 🙂

Viewing 5 replies - 1 through 5 (of 5 total)

The topic ‘Enqueuing editor CSS’ is closed to new replies.