@s_ha_dum is right, the ajax plugin api is the way to go.
I ran into this issue myself with a dynamic js file I was generating a little while ago.
Basically you would enqueue your style as follows:
wp_enqueue_style(
'dynamic-css',
admin_url('admin-ajax.php').'?action=dynamic_css',
$deps,
$ver,
$media
);
Then create a function to load your dynamic css file:
function dynaminc_css() {
require(get_template_directory().'/css/dynamic.css.php');
exit;
}
And add the ajax actions:
add_action('wp_ajax_dynamic_css', 'dynaminc_css');
add_action('wp_ajax_nopriv_dynamic_css', 'dynaminc_css');
where wp_ajax_*
will fire the named function and have access to all fundamental wp functions
For the front; end wp_ajax_nopriv_*
executes non logged in visitors
Note that I’ve found the EDIT: I just tested this again, and cant repeat it. Apparently there is no such restriction.add_action('wp_ajax_custom_function', 'custom_function')
, if in a plugin, must be in the plugin’s base activation file else it will be ignored. There is terse note about it in the action reference doc.
This explanation is an expanded version of this codex forum post: http://Wordpress.org/support/topic/best-way-to-create-a-css-file-dynamically#post-4857705