简体   繁体   English

将CSS放入Wordpress插件中

[英]Enqueue CSS in Wordpress Plugin

I'm at my wit's end here... here is the code I use to try and get a CSS file to work in my WordPress plugin: 我的机智在这里...这是我用来尝试在CSS插件中使用CSS文件的代码:

add_action('wp_enqueue_scripts', 'initial_admin_links_hide_stylesheet');

function initial_admin_links_hide_stylesheet() {
    wp_enqueue_style( 'prefix-style', plugins_url('initial_hide_admin_links.css', __FILE__));   
}

Then I call the function by using this line of code: 然后,我使用以下代码行调用该函数:

initial_admin_links_hide_stylesheet();

If I comment out the call to the function I receive no notices. 如果我注释掉对该函数的调用,则不会收到任何通知。 If I leave it uncommented, the notice I receive is: 如果我不加评论,我收到的通知是:

Notice: wp_enqueue_style was called incorrectly. 注意:wp_enqueue_style被错误地调用。 Scripts and styles should not be registered or enqueued until the wp_enqueue_scripts, admin_enqueue_scripts, or login_enqueue_scripts hooks. 在wp_enqueue_scripts,admin_enqueue_scripts或login_enqueue_scripts钩子之前,脚本和样式不应注册或排队。 Please see Debugging in WordPress for more information. 请参阅WordPress中的调试以获取更多信息。 (This message was added in version 3.3.) in /home2/jakereva/public_html/wp-includes/functions.php on line 3547 (此消息是在版本3.3中添加的。)在第3547行的/home2/jakereva/public_html/wp-includes/functions.php中

As far as I can tell, the code has been written correctly, but I absolutely cannot get that notice to go away when I call the function. 据我所知,代码已正确编写,但是当我调用该函数时,我绝对无法消除该通知。 Help! 救命! Thanks in advance. 提前致谢。

add_action('wp_enqueue_scripts', 'initial_admin_links_hide_stylesheet');

This tells Wordpress to hook the script into the process for you. 这告诉Wordpress为您挂钩脚本。 There is no need to call the function manually. 无需手动调用该函数。

Make sure you have wp_head() and wp_footer() calls in your theme files where appropriate or it want be added 确保在主题文件中有适当位置或要添加的地方有wp_head()和wp_footer()调用

/**
 * Proper way to enqueue scripts and styles
 */
function theme_name_scripts() {
    wp_enqueue_style( 'style-name', get_stylesheet_uri() );
    wp_enqueue_script( 'script-name', get_template_directory_uri() . '/js/example.js', array(), '1.0.0', true );
}

add_action( 'wp_enqueue_scripts', 'theme_name_scripts' );

Change hook name to wp_head. 将挂钩名称更改为wp_head。 This will enqueue style in header. 这将在标题中加入样式。

Check the WordPress action reference to see what actions being called. 查看WordPress操作参考以查看正在调用的操作。 https://codex.wordpress.org/Plugin_API/Action_Reference https://codex.wordpress.org/Plugin_API/Action_Reference

In front you can use wp_head. 在前面,您可以使用wp_head。 In admin pages: admin_enqueue_scripts 在管理页面中:admin_enqueue_scripts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM