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();
// Registered, not enqueued — available as a dep for other scripts.
Enqueue::script('my-lib')
->src(PLUGIN_BASE_URL . 'assets/js/lib.js')
->register_only()
->register();
// Later, another script can depend on it:
Enqueue::script('my-app')
->src(PLUGIN_BASE_URL . 'assets/js/app.js')
->deps('my-lib')
->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_block_script')
->src('http://www.site.com/block-script.js')
->for_block()
->register();
// Register block as normal, naming 'my_block_script' in its script handles.
});
/**
* 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.
* Performs a HEAD request to the src URL via wp_remote_head().
*
* @return self
*/
public function latest_version(): self
/**
* Register the script for i18n via wp_set_script_translations().
*
* @param string $domain Text domain.
* @param string|null $path Optional path to JSON translation files.
* @return self
*/
public function translations( string $domain, ?string $path = null ): self
/**
* Attach an inline JS snippet to the registered handle via wp_add_inline_script().
*
* @param string $code JavaScript source.
* @param string $position 'before' or 'after'. Default 'after'.
* @return self
*/
public function with_code( string $code, string $position = 'after' ): self
/**
* Attach an inline CSS snippet to the registered handle via wp_add_inline_style().
*
* @param string $css CSS source.
* @return self
*/
public function with_style( string $css ): self
/**
* Register the asset with WP without enqueueing it.
*
* @param bool $register_only Default true.
* @return self
*/
public function register_only( bool $register_only = true ): self
/**
* Defer the registration to the enqueue_block_editor_assets hook.
*
* @param bool $for_block_editor Default true.
* @return self
*/
public function for_block_editor( bool $for_block_editor = true ): self
/**
* Attach a style to a named block via wp_enqueue_block_style().
*
* @param string $block_name e.g. 'core/paragraph' or 'my-plugin/my-block'.
* @return self
*/
public function for_block_style( string $block_name ): self
/**
* Register a block type from a block.json directory.
*
* @param string $path Absolute path to the directory containing block.json.
* @return self
*/
public function block_json( string $path ): 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.