0

Creating a WP plugin for third party usage (first time doing this), and typically links to stylesheets are put in the header or in the theme generated options panel. That's fine for personal use but not for this.

I found wp_enqueue_style via a search on here, and I'm trying to apply it. Below is the code I have right now.

function wpse_load_plugin_css() {
    $plugin_url = plugin_dir_url( __FILE__ );

    wp_enqueue_style( 'style', $plugin_url . 'style.css' );
}
add_action( 'wp_enqueue_scripts', 'wpse_load_plugin_css' );




function test() {
global $con;
wp_enqueue_style('style');

   ** the bulk of my code - query and output **

}

None of my styling is working at this point. I've cleared the cache, and this early stage, just trying to change the size of fonts.

EDIT: Directory: /wp-content/plugins/player-match/

Both functions are on the same file pm_main.php. style.css is in the same directory.

Am I on the right track?

Thank you.

8
  • "That's fine for personal use but not for this." - why, and what are you really trying to do? Why do you call wp_enqueue_style() twice? But if you're trying to manually print the <link> tag in the body, then note that the MDN web docs says, "<link rel="stylesheet"> is permitted in the body. However, this isn't a good practice to follow; it makes more sense to separate your <link> elements from your body content, putting them in the <head>".
    – Sally CJ
    Sep 20 at 17:16
  • There are a few examples saying to do so without a clear way of utilizing it. This link dug a little deeper: stackoverflow.com/questions/3760222/…
    – Jwrbloom
    Sep 20 at 19:06
  • I think I misread the "are put" in "typically links to stylesheets are put in the header or...", i.e. you're now just trying to enqueue the stylesheet manually via wp_enqueue_style() and it's not working for you, right? And if so, then I guess you're not using the correct hook - if test() is used on an admin page (like a plugin options page), then you should use the admin_enqueue_scripts hook instead of wp_enqueue_scripts (or use login_enqueue_scripts on the login page). You should also ensure that $plugin_url . 'style.css' outputs the correct URL.
    – Sally CJ
    Sep 20 at 20:12
  • test() is being used on a regular WP page. Do I use a leading /style.css or just style.css?
    – Jwrbloom
    Sep 21 at 0:13
  • If you use plugin_dir_url(), then style.css would work fine, i.e. the leading / is not needed. However, in what file did you put the code in the question? Is it in your main plugin file and are you actually referring to a standalone plugin in wp-content/plugins? Can you answer that by editing your question and preferably, show your plugin structure (the directory tree).
    – Sally CJ
    Sep 21 at 1:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.