我的wordpress中有一个加载动态css的函数。我检查了这个admin-ajax.php调用是否使网站慢了(+5s)。
add_action( 'wp_enqueue_scripts', 'theme_custom_style_script', 12 );
function theme_custom_style_script() {
wp_enqueue_style( 'dynamic-css', admin_url('admin-ajax.php').'?action=dynamic_css', '');
}
add_action('wp_ajax_dynamic_css', 'dynamic_css');
function dynamic_css() {
require( get_template_directory().'/css/custom.css.php' );
exit;
} 每次对admin进行编辑时,我是否可以将该文件的输出保存在文件夹中,并加载这样的css链接而不是每次通过admin-ajax加载?还是用不同的方式来避免这个问题?谢谢
发布于 2019-04-14 11:51:25
如果要将生成的css保存到文件中,请查看file_get_contents()和file_put_contents()原生PHP函数。https://www.php.net/manual/en/function.file-put-contents.php
你也可以在SO和WPSE中找到一些关于这个主题的问答,例如https://stackoverflow.com/questions/25559913/write-from-wordpress-plugin-to-text-file-with-php。
然后,您可以像任何其他css文件一样,在wp_enqueue_scripts中对创建的css文件进行排队。您可能希望在file_exists条件检查中包装该队列,以避免潜在的错误。
另一种选择可能是在wp_enqueue_scripts中对动态css进行排队,但是包装在一个合适的if语句中。
https://wordpress.stackexchange.com/questions/334334
复制相似问题