Action uncanny-learndash-toolkit

ult_before_directory_modules

Fires before the directory modules are loaded, providing access to the module list for modification.

add_action( 'ult_before_directory_modules', $callback, 10, 1 );

Description

Fired before the ultimate directory modules are rendered. Developers can hook into this action to modify the $modules array, add custom content before the modules, or perform other actions related to module display. This hook provides an opportunity to influence the module list before it's output to the user.


Usage

add_action( 'ult_before_directory_modules', 'your_function_name', 10, 1 );

Parameters

$modules (mixed)
This parameter contains a list or array of modules that will be displayed in the directory.

Examples

<?php
/**
 * Example function to hook into ult_before_directory_modules.
 *
 * This function demonstrates how to intercept and potentially modify
 * the list of modules before they are displayed in the Ultimate Addons
 * directory. In this example, we'll conditionally hide a specific module
 * if a certain option is not enabled.
 *
 * @param array $modules An array of module objects or data.
 */
function my_custom_ult_modules_before_display( $modules ) {
    // Check if the $modules is indeed an array before proceeding.
    if ( ! is_array( $modules ) ) {
        return; // Exit early if it's not an array.
    }

    // Define the slug or identifier of the module we want to potentially hide.
    $module_to_hide_slug = 'advanced-forms';

    // Get the value of a hypothetical theme option that controls visibility.
    // This could be from the theme's customizer, theme options page, etc.
    $hide_advanced_forms = get_option( 'my_theme_hide_advanced_forms_module', false );

    // If the option to hide the advanced forms module is enabled,
    // we'll filter it out from the $modules array.
    if ( $hide_advanced_forms ) {
        // Use array_filter to create a new array containing only modules
        // whose slug does NOT match the one we want to hide.
        $modules = array_filter( $modules, function( $module ) use ( $module_to_hide_slug ) {
            // Assuming each $module is an object with a 'slug' property.
            // Adjust this property name if the actual module structure is different.
            return ! ( isset( $module->slug ) && $module->slug === $module_to_hide_slug );
        } );

        // Re-index the array after filtering to ensure clean iteration.
        $modules = array_values( $modules );
    }

    // IMPORTANT: Because this is an action hook, we don't return anything.
    // If it were a filter hook, we would return the modified $modules.
    // The modification happens by reassigning the variable within the scope
    // of this function. The original $modules variable outside this function
    // is not directly modified unless passed by reference, which is not the case here.
    // However, the `do_action` call within the Ultimate Addons plugin is designed
    // to make the `$modules` variable available to the hooked function's scope.
    // The subsequent `foreach` loop in `admin-modules.php` will iterate over
    // whatever `$modules` variable is active *after* this hook has run.
}

// Add the function to the 'ult_before_directory_modules' action hook.
// The third parameter '1' indicates that this function accepts 1 argument.
add_action( 'ult_before_directory_modules', 'my_custom_ult_modules_before_display', 10, 1 );
?>

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/templates/admin-modules.php:188

<span class="ult-icon ult-icon--th-list"></span>
						</div>
					</div>
				</div> -->

			</div>

			<?php do_action( 'ult_before_directory_modules', $modules ); ?>

			<div class="ult-directory-modules">

				<?php
				foreach ( $modules as $module ) {

					$css_classes = [];


Scroll to Top