advanced custom fields – Function wp_enqueue_style was called incorrectly


WordPress uses a system of so-called “hooks”, which are functions that are run at specific points within the process of building the webpage for the browser.

The system runs like this:

  • A style is supposed to be within the <head> portion of an HTML document.
  • within the function that outputs the <head> portion, WordPress executes a function named wp_enqueue_scripts. (this is for the “frontend” portion of the website. On the “backend” portion, a function named admin_enqueue_scripts is executed, and on the login page, the function is named login_enqueue_scripts)

Within your own code, you can “inject” your own stylesheets into this function like this:

add_action( 'wp_enqueue_scripts', 'my_awesome_additional_styles' );  
//the previous line essentially says "Hey WordPress, when you output all your scripts and styles, 
//please do whatever is written in the function my_awesome_additional_styles
function my_awesome_additional_styles(){
    $css_path   = plugins_url( "css/style.css", __FILE__ );
    $css_dir    = plugin_dir_path( __FILE__ ) . "css/style.css";
    if( file_exists( $css_dir ) ) {
       wp_enqueue_style( 'acf-block-css' , $css_path, array(), filemtime( $css_dir ) ); 
       //The previous line essentially says: "Hey WordPress, output the link to this stylesheet, okay?"

You can find more information here and here.
Happy Coding!

kero 1 month 2022-07-05T05:43:39-05:00 0 Answers 0 views 0

Leave an answer