Action uncanny-automator

automator_template_library_load

Fires when the Automator template library is loaded, allowing for custom template modifications or additions.

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

Description

Fired after the recipe template library data has been loaded from the feed. Developers can use this action to modify or extend the loaded templates, integrations, categories, or pagination settings before they are used. The `$this` parameter provides access to the `Recipe_Template_Library` object.


Usage

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

Parameters

$this (mixed)
This parameter refers to the `Recipe_Template_Library` object itself, providing access to its methods and properties.

Examples

<?php
/**
 * Example: Modify the template library data before it's loaded.
 * This could be used to add custom templates or filter existing ones.
 *
 * @param Recipe_Template_Library $recipe_template_library The instance of the Recipe_Template_Library class.
 */
add_action( 'automator_template_library_load', function( $recipe_template_library ) {

    // Example: Add a custom template to the library if it doesn't exist.
    $custom_template = array(
        'id'          => 'my_custom_recipe_template',
        'name'        => 'My Custom Recipe',
        'description' => 'A template for a custom automation.',
        'template'    => 'custom_template.php', // Assumes this file exists in your theme/plugin.
        'icon'        => 'dashicons-hammer',
        'categories'  => array( 'custom', 'examples' ),
        'integrations' => array(),
    );

    // Get the current templates
    $current_templates = $recipe_template_library->templates;

    // Check if our custom template ID already exists
    $template_exists = false;
    foreach ( $current_templates as $template ) {
        if ( isset( $template['id'] ) && $template['id'] === $custom_template['id'] ) {
            $template_exists = true;
            break;
        }
    }

    // If it doesn't exist, add it
    if ( ! $template_exists ) {
        $current_templates[] = $custom_template;
        // Update the templates property of the library object
        // Note: Direct property modification might not be the intended way depending on the class's design.
        // A better approach would be to see if the class offers a method to add templates.
        // For this example, we'll assume direct modification for demonstration.
        $recipe_template_library->templates = $current_templates;
    }

    // Example: Add a new integration category if it doesn't exist.
    $new_integration_category = 'my_custom_integration';
    $current_categories = $recipe_template_library->categories;

    if ( ! in_array( $new_integration_category, $current_categories ) ) {
        $current_categories[] = $new_integration_category;
        $recipe_template_library->categories = $current_categories;
    }

}, 10, 1 ); // Priority 10, accepts 1 argument ($this which is the Recipe_Template_Library object).
?>

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/core/lib/class-recipe-template-library.php:117

public function load() {

		// Get the feed.
		$feed = $this->get_library_feed();
		if ( ! empty( $feed ) && is_array( $feed ) ) {
			$this->templates    = isset( $feed['templates'] ) ? $feed['templates'] : array();
			$this->integrations = isset( $feed['integrations'] ) ? $feed['integrations'] : array();
			$this->categories   = isset( $feed['categories'] ) ? $feed['categories'] : array();
			$this->per_page     = isset( $feed['per_page'] ) ? $feed['per_page'] : 24;
		}

		// Do action to load the library.
		do_action( 'automator_template_library_load', $this );
	}


Scroll to Top