It doesn’t work because WordPress isn’t loaded within the context of editor-style.php
, and you’re actually getting a fatal error (get_theme_mod
undefined). Enable error logging and you’ll see what I mean.
Rather than pointing directly to a custom PHP file, you should use an “endpoint” within WordPress:
add_editor_style( admin_url( 'admin-ajax.php?action=editor-style' ) );
Now you can “listen” for when this virtual stylesheet is requested and kick out all your awesome CSS with WordPress loaded:
function wpse_226550_editor_style() {
header( 'Content-Type: text/css; charset=UTF-8' );
echo $css; // Example!
exit;
}
add_action( 'wp_ajax_nopriv_editor-style', 'wpse_226550_editor_style' );
add_action( 'wp_ajax_editor-style', 'wpse_226550_editor_style' );
Don’t be put off by the “ajax” reference – admin-ajax.php
is a popular technique in WordPress for sending all kinds of custom responses, but without having to manually load WordPress yourself (e.g. require './path/to/wordpress/wp-load.php';
– bad practice, path assumptions).