Skip to main content

Enqueue Scripts in a Class

Home Forums WordPress Enqueue Scripts and Styles Enqueue Scripts in a Class

Tagged: , , , ,

Viewing 0 reply threads
  • Author
    Posts
    • #2396

      Layer7web
      Keymaster
      <?php
      if ( ! defined( 'ABSPATH' ) ) { exit; } // Exit if accessed directly
      
      if ( ! class_exists( 'ExampleClass' )  ) {
          class ExampleClass {
           
              public function __construct() {
                  add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts_and_css' ) );
              }
      
              public function enqueue_scripts_and_css(){
                  wp_register_script( 'handle_of_script', 'path/to/script/example.js' );
                  wp_enqueue_script( 'handle_of_script' );
              }
          }
      }
      • This topic was modified 9 years, 7 months ago by Layer7web.
Viewing 0 reply threads

You must be logged in to reply to this topic.