php - WordPress将样式表添加到插件菜单页面

标签 php css wordpress

已解决

我需要调用 new Form(),虽然我在 WebSecurity.php 中调用了

array(__CLASS__,'ws_page_render') //I though this would call new Form()

我正在尝试将样式表添加到 WordPress 插件中的菜单页面。我阅读了文档并应用了它,但它仍然没有显示在标题中。添加所有资源。我希望有人能帮助我。

我需要在 header 中添加style.css,以便request_form.php可以在网页上呈现css。

我已经用正确的方法添加了样式表:

我已经更新了 Form 类

    function __construct()
    {   
        //browser finds the style sheet
        // output: http://cms.local/wordpress/wp-content/plugins/web_security_extension/includes/resources/css/ws-style.css
        echo plugin_dir_url( __FILE__ ) . 'resources/css/ws-style.css';

        add_action( 'admin_enqueue_scripts', array($this, 'ws_load_css_1') );

        include_once WSS_EXTESION_DIR . '/includes/views/request_form.php';
    }

    function ws_load_css_1() 
    {
          wp_register_style( 'ws_css', plugin_dir_url( __FILE__ ) . 'resources/css/ws-style.css');
          wp_enqueue_style( 'ws_css' );
    }

元素结构

根插件

  • 包括
    • 资源
      • CSS
        • ws-style.css
    • 观看次数
      • request_form.php
    • 来自.php
  • 初始化文件
  • WebSecurity.php

初始化文件

<?php

/*
Plugin Name: websecurityscan
Description:
Version: 0.1
Author: 
*/

define('WSS_EXTESION', __FILE__);

define('WSS_EXTESION_DIR', untrailingslashit( dirname(WSS_EXTESION) ));

require_once WSS_EXTESION_DIR . '/WebSecurity.php';

网络安全.php

<?php

require_once WSS_EXTESION_DIR . '/includes/Form.php'; 

class WebSecurity
{

    function __construct()
    {
        add_action('admin_menu', array($this, 'ws_add_menu'));
    }

    function ws_add_menu()
    {

        add_menu_page(
            'Web Application Scanner', 
            'Web App Scanner', 
            'manage_options', 
            'Web_App_Scanner_menu',
            array(__CLASS__,'ws_page_render')
            );

    }

    function ws_page_render()
    {
        new Form();
    }

}

new WebSecurity();

?>

表单.php

<?php

class Form
{

    function __construct()
    {   
        //browser finds the style sheet
        echo plugin_dir_url( __FILE__ ) . 'resources/css/ws-style.css';

        add_action( 'admin_enqueue_scripts', array($this, 'ws_load_css_1') );

        include_once WSS_EXTESION_DIR . '/includes/views/request_form.php';
    }

    function ws_load_css_1() 
    {
          wp_register_style( 'ws_css', plugin_dir_url( __FILE__ ) . 'resources/css/ws-style.css');
          wp_enqueue_style( 'ws_css' );
    }    
}

请求表单.php

<html>
    <body>

        <div class="wrapper">
            <form action="request_form.php" method="post">

                <label>Naam</label>
                <input type="text" name="naam">

                <label>Bedrijf</label>
                <input type="text" name="bedrijf">

                <label>Email</label>
                <input type="email" name="email">

                <input type="submit" name="submit" value="verstuur">

            </form>
        </div>

    </body>
</html>

样式.css

.wrapper{
    margin: auto;
    width: 50%;
    padding: 10px;
}

最佳答案

使用 admin_enqueue_scripts Hook 而不是 wp_enqueue_scripts 将 Assets 排入管理面板。像下面这样编辑 Form 类构造函数:

function __construct()
{   
     add_action('admin_enqueue_scripts',array($this, 'ws_load_css'));
     include_once WSS_EXTESION_DIR . '/includes/views/request_form.php';
}

wp_enqueue_scripts 用于在首页排队的操作 Hook 。

admin_enqueue_scripts 用于在管理页面上排队的操作 Hook 。

更新

此外,您必须使用 css 文件 url 而不是路径。编辑您的 ws_load_css() 函数并使用 plugin_dir_url() 函数,如下所示:

function ws_load_css()
{
  wp_enqueue_style('style',  plugin_dir_url( __FILE__ ) . 'resources/css/ws-style.css');
} 

对于新版本的代码,编辑您的 load_custom_wp_admin_style() 函数,如下所示:

function load_custom_wp_admin_style() {
        wp_register_style( 'custom_wp_admin_css', plugin_dir_url( __FILE__ ) . 'resources/css/ws-style.css');
        wp_enqueue_style( 'custom_wp_admin_css' );
    }

注意:使用resources/css/ws-style.css 而不是/resources/css/ws-style.css

关于php - WordPress将样式表添加到插件菜单页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45864497/

相关文章:

php - 内容数据库 - 可以存储 HTML 吗?

css - Sass 除法不是有效的 CSS 值

ios - 谁能帮我解决这个 xcode 问题?

html - 在图片下定位 flexbox 中的文本

php - 在 WooCommerce 中选择产品重量后计算并显示新价格

php - 用于密码字段验证的正则表达式

php - 漂亮的 url 不重定向,htaccess

php - 在 html 中包含 php 页面后,CSS 行被破坏

wordpress - woocommerce 短代码显示具有相同类别的列表产品

wordpress - 在WP网站上嵌入youtube视频