将 CSS 添加到 PHP Wordpress 插件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10897755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:18:19  来源:igfitidea点击:

Add CSS to Php Wordpress plugin

phpcsswordpressplugins

提问by WtFudgE

I wrote a simple plugin which sets some css code using wp_options. It all looks similar like this:

我写了一个简单的插件,它使用 wp_options 设置一些 css 代码。这一切看起来都像这样:

add_action('init','easy_style');

function easy_style()
{
    ?>
    <style>
    #header a {
        color: <?php echo get_option('topcolor'); ?>;
        font-size: <?php echo get_option('topsize'); ?>px;
        <?php
            if (get_option('topstyle') == "bold")
            { echo "font-weight: bold;"; echo "font-style: normal;"; }
            elseif (get_option('topstyle') == "italic")
            { echo "font-style: italic;"; echo "font-weight: normal;"; }
            elseif (get_option('topstyle') == "bolditalic")
            { echo "font-weight: bold;"; echo "font-style: italic;"; }
            else { echo "font-weight: normal;"; echo "font-style: normal;"; }
        ?>;
    }

    </style>
    <?php
}

Now this works, but if I activate my "Contact Form 7" plugin, the contact form 7 doesn't work anymore. It can't send any mails. So I think what I do is wrong. If I remove this piece of code, the contact form works again...

现在这有效,但如果我激活我的“联系表格 7”插件,联系表格 7 将不再工作。它不能发送任何邮件。所以我认为我的做法是错误的。如果我删除这段代码,联系表会再次工作......

I think I do it wrong because the css needs to be loaded in the header, no? So what I thought I would do as a test is put the same code in the header. However then some other css (i don't know where) overwrites these, so this also doesn't work.

我想我做错了,因为需要在标题中加载 css,不是吗?所以我认为我会做的测试是将相同的代码放在标题中。但是,其他一些 css(我不知道在哪里)会覆盖这些,所以这也不起作用。

I think there are some wp functions to add css code to the header but I don't know how exacly.

我认为有一些 wp 函数可以将 css 代码添加到标题中,但我不知道有多精确。

Any ideas?

有任何想法吗?

Thanks

谢谢

采纳答案by WtFudgE

I solved my problem by loading in the styles.css file, edit it and save again

我通过加载styles.css文件解决了我的问题,编辑它并再次保存

回答by MrCode

A safe way to add CSS to your plugin is to use wp_enqueue_style.

将 CSS 添加到插件的一种安全方法是使用wp_enqueue_style.

/**
 * Register with hook 'wp_enqueue_scripts', which can be used for front end CSS and JavaScript
 */
add_action( 'wp_enqueue_scripts', 'prefix_add_my_stylesheet' );

/**
 * Enqueue plugin style-file
 */
function prefix_add_my_stylesheet() {
    // Respects SSL, Style.css is relative to the current file
    wp_register_style( 'prefix-style', plugins_url('style.css', __FILE__) );
    wp_enqueue_style( 'prefix-style' );
}

Reference.

参考

回答by mishu

Maybe the plugin you are using wants to send some http headers.

也许您使用的插件想要发送一些 http 标头。

Since you are using the inithook (detailed here: http://codex.wordpress.org/Plugin_API/Action_Reference/init) you are running some actions when the plugin developers consider that the headers are not already sent.

由于您正在使用init钩子(详细信息:http: //codex.wordpress.org/Plugin_API/Action_Reference/init),当插件开发人员认为标头尚未发送时,您正在运行一些操作。

But since you send some output (echo some styles) you are forcing the headers to be sent sooner. I suggest you to use another hook that occurs after the output is already started.

但是,由于您发送了一些输出(回显了一些样式),因此您会强制更快地发送标头。我建议您使用在输出已经开始之后发生的另一个钩子。

Update:you could use the wp_enqueue_style function (reference here: http://codex.wordpress.org/Function_Reference/wp_enqueue_style) as it is the recommended way to display styles. You only need to save those rules in a css file and en-queue it to be loaded before the wp_head function is called

更新:您可以使用 wp_enqueue_style 函数(参考此处:http://codex.wordpress.org/Function_Reference/wp_enqueue_style ),因为它是显示样式的推荐方式。您只需要将这些规则保存在一个 css 文件中,并在调用 wp_head 函数之前将其加入队列以加载

回答by Longshot

In a plugin dev, do not forget to send the reference of the class ($this)

在插件开发中,不要忘记发送class ($this)

add_action( 'wp_enqueue_scripts', array($this,'prefix_add_my_stylesheet' ));