0
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

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

"is_plugin_page " checkes directory for current page of my plugin ? as per wordpress norm this function deprecated developer.wordpress.org/reference/functions/is_plugin_page
it's method of the class. You can change whatever you want.
0

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);

3 Comments

i have include these files but after plugin main page they are not working . i have multiple page in my plugin.
@Vaibhav without seeing what you're doing, it's impossible to diagnose. First, take out that if () statement. Then try adding echo '<pre style="padding-left:15rem;">' . $hook . '</pre>'; before the if() statement to see what your hook is like; if all your plugin pages share a common hook (e.g. strpos('myplugin_') !== false), then use that as your conditional. Otherwise I would recommend posting a new question.
Yes i know that without viewing code it is quite difficult i will upload my code on Github i have upgrading code of following plugin you will get some idea with this plugin made by me now as per wordpress norms i am upgrading i will upload ASAP. github.com/Viv31/Wordpress_CRUD_Plugin/blob/master/CRUD-Plugin/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.