1. Go to this page and download the library: Download pinkcrab/enqueue library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
pinkcrab / enqueue example snippets
add_action('wp_enqueue_scripts', function(){
// Enqueue a script
Enqueue::script('My_Script')
->src('https://url.tld/wp-content/plugins/my_plugn/assets/js/my-script.js')
->deps('jquery')
->latest_version()
->register();
// Enqueue a stylesheet
Enqueue::style('My_Stylesheet')
->src('https://url.tld/wp-content/plugins/my_plugn/assets/css/my-stylesheet.css')
->media('all and (min-width: 1200px)')
->latest_version()
->register();
});
$enqueue_script = new Enqueue( 'my_script', 'script');
$enqueue_style = new Enqueue( 'my_style', 'style');
// OR
$enqueue_script = Enqueue::script('my_script');
$enqueue_style = Enqueue::style('my_style');
$enqueue_script = new Enqueue( 'my_script', 'script');
$enqueue_script->src('.....');
$enqueue_script->register();
// OR
Enqueue::script('my_script')
->src('.....')
->register();
class My_Thingy{
/**
* Returns a partly finalised Enqueue scripts, with defined url.
*
* @param string $script The file location.
* @return Enqueue The populated enqueue object.
*/
protected function enqueue($script): Enqueue {
return Enqueue::script('My_Script')
->src($script)
->deps('jquery')
->latest_version();
}
/**
* Called to initialize the class.
* Registers our JS based on a conditional.
*
* @return void
*/
public function init(): void {
if(some_conditional()){
add_action('wp_enqueue_scripts', function(){
$this->enqueue(SOME_FILE_LOCATION_CONSTANT)->register()
});
} else {
add_action('wp_enqueue_scripts', function(){
$this->enqueue(SOMEOTHER_FILE_LOCATION_CONSTANT)->register()
});
}
}
}
add_action('wp_loaded', [new My_Thingy, 'init']);
add_action('init', function(){
Enqueue::script('my_style')
->src('http://www.site.com/my-scripts.js')
->defer()
->for_block()
->register();
// Register block as normal
});
/**
* Creates an Enqueue instance.
*
* @param string $handle
* @param string $type
*/
public function __construct( string $handle, string $type )
/**
* Creates a static instance of the Enqueue class for a script.
*
* @param string $handle
* @return self
*/
public static function script( string $handle ): self
/**
* Creates a static instance of the Enqueue class for a style.
*
* @param string $handle
* @return self
*/
public static function style( string $handle ): self
/**
* Defined the SRC of the file.
*
* @param string $src
* @return self
*/
public function src( string $src ): self
/**
* Defined the Dependencies of the enqueue.
*
* @param string ...$deps
* @return self
*/
public function deps( string ...$deps ): self
/**
* Defined the version of the enqueue
*
* @param string $ver
* @return self
*/
public function ver( string $ver ): self
/**
* Define the media type.
*
* @param string $media
* @return self
*/
public function media( string $media ): self
/**
* Sets the version as last modified file time.
*
* @return self
*/
public function lastEditedVersion(): self
/**
* Should the script be called in the footer.
*
* @param boolean $footer
* @return self
*/
public function footer( bool $footer = true ): self
/**
* Should the script be called in the inline.
*
* @param boolean $inline
* @return self
*/
public function inline( bool $inline = true ):self
/**
* Pass any key => value pairs to be localised with the enqueue.
*
* @param array $args
* @return self
*/
public function localize( array $args ): self
/**
* Adds a Flag (attribute with no value) to a script/style tag
*
* @param string $flag
* @return self
*/
public function flag( string $flag ): self
/**
* Adds an attribute tto a script/style tag
*
* @param string $key
* @param string $value
* @return self
*/
public function attribute( string $key, string $value ): self
/**
* Marks the script or style as deferred loaded.
*
* @return self
*/
public function defer(): self
/**
* Marks the script or style as async loaded.
*
* @return self
*/
public function async(): self
/**
* Set denotes the script type.
*
* @param string $script_type Denotes the script type.
* @return self
*/
public function script_type( string $script_type ): self
/**
* Set if being enqueued for a block.
*
* @param bool $for_block Denotes if being enqueued for a block.
* @return self
*/
public function for_block( bool $for_block = true ): self
/**
* Registers the file as either enqueued or inline parsed.
*
* @return void
*/
public function register(): void
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.