Filter Uncanny Redemption Codes

ulc_codes_enqueue_backend_assets

Filters whether backend assets for the Ultimate. (WooCommerce) Codes plugin should be enqueued.

add_filter( 'ulc_codes_enqueue_backend_assets', $callback, 10, 2 );

Description

Allows developers to conditionally prevent or enable the enqueuing of backend assets for Uncanny Codes. This filter is applied before assets are loaded, providing control over which admin pages receive the necessary scripts and styles based on the current screen or custom logic.


Usage

add_filter( 'ulc_codes_enqueue_backend_assets', 'your_function_name', 10, 2 );

Parameters

$should_enqueue (mixed)
This parameter is a boolean flag that determines whether the backend assets should be enqueued.
$current_screen (mixed)
This parameter is a boolean that determines whether the backend assets should be enqueued.

Return Value

The filtered value.


Examples

/**
 * Conditionally enqueues backend assets for Uncanny Codes based on the current screen.
 *
 * This filter allows developers to modify whether backend assets for Uncanny Codes
 * should be loaded. By default, assets are loaded on specific Uncanny Codes admin pages
 * and when editing/creating WooCommerce products.
 *
 * @param bool         $should_enqueue The current decision on whether to enqueue assets.
 * @param WP_Screen|null $current_screen The current WordPress screen object.
 *
 * @return bool Whether to enqueue the backend assets.
 */
add_filter( 'ulc_codes_enqueue_backend_assets', function( $should_enqueue, $current_screen ) {

	// If already decided to enqueue, no need to check further.
	if ( true === $should_enqueue ) {
		return true;
	}

	// If we don't have a current screen object, we can't make a decision.
	if ( ! $current_screen ) {
		return false;
	}

	// Define the specific admin page IDs where assets should always be loaded.
	$uncanny_codes_admin_pages = array(
		'uncanny-codes_page_uncanny-codes-kb',
		'uncanny-codes_page_uncanny-codes-plugins',
		'uncanny-codes_page_uncanny-learndash-codes-settings',
		'toplevel_page_uncanny-learndash-codes',
	);

	// Check if the current screen matches one of the Uncanny Codes admin pages.
	if ( in_array( $current_screen->id, $uncanny_codes_admin_pages, true ) ) {
		return true;
	}

	// Check if we are on a post edit/create screen for the 'product' post type.
	// This is useful if Uncanny Codes integrates with WooCommerce products.
	if ( 'post' === $current_screen->base && 'product' === $current_screen->post_type ) {
		return true;
	}

	// If none of the above conditions are met, do not enqueue assets.
	return false;

}, 10, 2 );

Placement

This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.


Source Code

src/boot.php:682

public static function should_enqueue_backend_assets() {
		global $pagenow, $current_screen;

		$should_enqueue = false;

		$allowed_screens = apply_filters(
			'uc_allowed_screens',
			array(
				'uncanny-codes_page_uncanny-codes-kb',
				'uncanny-codes_page_uncanny-codes-plugins',
				'uncanny-codes_page_uncanny-learndash-codes-settings',
				'toplevel_page_uncanny-learndash-codes',
			),
			$current_screen
		);

		// Check if it's creating or editing a WooCommerce product
		if ( 'post' === (string) $current_screen->base && 'product' === (string) $current_screen->id ) {
			$should_enqueue = true;
		}

		// Check if it's one of the allowed pages
		if ( 'admin.php' === (string) $pagenow &&
			 ( strpos( (string) $current_screen->base, 'codes_page_uncanny' ) || in_array( $current_screen->base, $allowed_screens ) )
		) {
			$should_enqueue = true;
		}

		return apply_filters( 'ulc_codes_enqueue_backend_assets', $should_enqueue, $current_screen );
	}


Scroll to Top