Filter uncanny-automator-pro

automator_endpoint_all_taxonomies_by_post_type_fields_default

Filters the default fields for all taxonomies associated with a specific post type when generating automator endpoint data.

add_filter( 'automator_endpoint_all_taxonomies_by_post_type_fields_default', $callback, 10, 1 );

Description

This filter hook allows developers to modify the default taxonomy fields returned by the `automator_endpoint_all_taxonomies_by_post_type` AJAX endpoint. It fires when fetching taxonomies for a specific post type. Developers can add, remove, or alter taxonomy data before it's JSON encoded and sent to the client.


Usage

add_filter( 'automator_endpoint_all_taxonomies_by_post_type_fields_default', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

/**
 * Add default fields to the 'all taxonomies by post type' endpoint.
 *
 * This filter allows developers to pre-populate the list of available taxonomies
 * for a given post type when Uncanny Automator is fetching them.
 *
 * @param array $fields The default fields to return.
 *
 * @return array The modified array of fields.
 */
add_filter(
	'automator_endpoint_all_taxonomies_by_post_type_fields_default',
	function ( $fields ) {
		// If the 'post_type' is not provided in the request, we can't filter taxonomies.
		// In this case, we might want to return an empty array or throw an error,
		// but for this example, we'll just ensure we have a placeholder.
		if ( ! isset( $_POST['post_type'] ) ) {
			return $fields;
		}

		$post_type = sanitize_text_field( $_POST['post_type'] );

		// Get all taxonomies associated with the given post type.
		$taxonomies = get_object_taxonomies( $post_type, 'objects' );

		$taxonomy_options = array();

		// Loop through each taxonomy and add it as an option.
		foreach ( $taxonomies as $slug => $taxonomy ) {
			$taxonomy_options[] = array(
				'value' => $slug,
				'label' => $taxonomy->label,
			);
		}

		// Merge the dynamically generated taxonomy options with any existing default fields.
		// This example assumes the original $fields array might contain other metadata
		// or default selections.
		$fields['taxonomies'] = array_merge(
			isset( $fields['taxonomies'] ) ? $fields['taxonomies'] : array(),
			$taxonomy_options
		);

		return $fields;
	},
	10, // Priority
	1  // Accepted args
);

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

uncanny-automator-pro/src/integrations/wp/helpers/wp-pro-helpers.php:492

public function endpoint_all_taxonomies_by_post_type() {

		Automator()->utilities->ajax_auth_check();

		$fields = apply_filters( 'automator_endpoint_all_taxonomies_by_post_type_fields_default', array() );

		if ( ! automator_filter_has_var( 'values', INPUT_POST ) ) {

			echo wp_json_encode( $fields );

			die();

		}

		// Check post type trigger key ( Actions VS Triggers ).
		$post_type_key = isset( $_POST['values']['WPSPOSTTYPES'] ) ? 'WPSPOSTTYPES' : false;
		$post_type_key = ! $post_type_key && isset( $_POST['values']['WPPOSTTYPES'] ) ? 'WPPOSTTYPES' : $post_type_key;
		// Get post type or default to 'post'.
		$request_post_type = $post_type_key ? sanitize_text_field( $_POST['values'][ $post_type_key ] ) : 'post';
		$group_id          = automator_filter_has_var( 'group_id', INPUT_POST ) ? automator_filter_input( 'group_id', INPUT_POST ) : '';
		$post_type         = get_post_type_object( $request_post_type );

		if ( ! empty( $group_id ) && 'WPSETTAXONOMY' !== $group_id ) {
			$fields[] = array(
				'value' => - 1,
				'text'  => __( 'Any taxonomy', 'uncanny-automator-pro' ),
			);
		}

		if ( null !== $post_type ) {

			$taxonomies = get_object_taxonomies( $post_type->name, 'object' );

			if ( ! empty( $taxonomies ) ) {

				foreach ( $taxonomies as $taxonomy ) {

					$fields[] = array(
						'value' => $taxonomy->name,
						'text'  => esc_html( $taxonomy->labels->singular_name ),
					);

				}
			}
		}

		echo wp_json_encode( $fields );

		die();

	}

Scroll to Top