Cynthia A Lockley
Cynthia A Lockley

Reputation: 110

How can I override the URL used by a WordPress plugin to link its stylesheet?

Some browsers flagged that some http:// links in my page were being blocked as insecure. I looked at my page source and discovered in the header that a WordPress plugin sets up a <link rel='stylesheet' id='... to link in its own stylesheet but it is using the wrong protocol for the URL.

<link rel='stylesheet' id='fbw-css'  href='http://wdcb.stcwdc.org/wp-content/plugins/.....

I have looked in the plugin and I can't find where that is being set up.

Is there a way in my theme child's stylesheet or in the functions.php file to override this so it uses https?

I was wondering if something like this might work in my CSS?

link#fbw-css href { url:https://wdcb.stcwdc.org/wp-content/plugins/.....;
}

Or is there a way to do this in a function?

Upvotes: 1

Views: 1045

Answers (1)

user7236046
user7236046

Reputation:

There's a few methods of doing this (as is the WordPress way), but this should get you going for what you need.

Find the stylesheet id, in this case it looks like fbw. Ignore the css suffix here as that's added to stylesheets by WordPress by default.

Add this somewhere in your themes functions.php file:

add_action( 'wp_enqueue_scripts', 'so_50112358_enqueue_css', 20, 0 );

function so_50112358_enqueue_css() {

    // Remove the old stylesheet
    wp_dequeue_style( 'fbw' );
    wp_deregister_style( 'fbw' );

    // The new URL to the stylesheet with HTTPS
    $css_link = 'https://...';

    // Add it in again
    wp_register_style( 'fbw', $css_link, array(), '50112358', 'screen' );
    wp_enqueue_style( 'fbw' );

}

More information on wp_register_style can be found in the Developer docs. Handy if you want to customise things. Oh and so can wp_deregister_style.

Upvotes: 2

Related Questions