Vaibhav
Vaibhav

Reputation: 9

Can we call CSS in Wordpress plugin with custom function and call in file

function Custom_style_for_code(){ ?>
        <style>
        .container{
        //css goes here
        font-weight:bold;

        }
           </style><?php }

Custom_style_for_code(); 

I want to call styles in plugin page with function like that can we call styles like this.Does Wordpress Accept this

Upvotes: 0

Views: 110

Answers (2)

WP Punk
WP Punk

Reputation: 1444

Use something like this:

class Admin {
    public function __construct() {
        add_action( 'admin_enqueue_scripts', [ &$this, 'load_styles' ] );
    }
    public function load_styles() {
        if ( $this->is_plugin_page() ) {
            wp_enqueue_style(...);
        }
    }
    private function is_plugin_page(): bool {
        //All conditions for you plugin
        return ! empty ( $_GET['page'] ) && 'plugin-name' == $_GET['page'] ? true : false;
    }
}

Use current conditions in is_plugin_page

Upvotes: 1

indextwo
indextwo

Reputation: 5905

You should be enqueuing your styles using the built-in functionality for loading stylesheets rather than trying to force-print inline styling. If it's for a plugin page, then you should use admin_enqueue_scripts:

function vnm_load_my_css($hook) {

    //  Check if the passed $hook matches your plugin - no point enqueuing your CSS across all of the admin

    if ($hook == 'myplugin') {

        //  This assumes a path relative to your plugin file; so if your file is in /myplugin/ then your CSS will be in /myplugin/css/

        $cssPath = plugin_dir_path(__FILE__) . '/css/';
        $cssURI = plugins_url('/css/', __FILE__);

        wp_enqueue_style('my-plugin-style', $cssURI . '/mystyles.css');
    }
}

add_action('admin_enqueue_scripts', 'vnm_load_my_css', 20, 1);

Upvotes: 0

Related Questions