Filter uncanny-automator

uap_option_get_post_types_options

Filters the available post types that can be selected for import by WP All Import.

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

Description

Fires after WP All Import has generated a list of available post types for selection. Developers can filter this list to add, remove, or modify the post types offered within the plugin's interface, enabling custom integrations or restricting available options.


Usage

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

Parameters

$option (mixed)
This parameter is a placeholder for potential future options or parameters that could be passed to the `get_post_types_options` function.

Return Value

The filtered value.


Examples

add_filter( 'uap_option_get_post_types_options', 'my_custom_uap_post_type_options', 10, 1 );

/**
 * Customize the options for post types displayed in the User Approval Pro integration with WP All Import.
 *
 * This function allows developers to filter the post types that are made available
 * when configuring WP All Import within User Approval Pro.
 *
 * @param mixed $option The original array of post type options.
 * @return mixed The modified array of post type options.
 */
function my_custom_uap_post_type_options( $option ) {
	// Example: Exclude a specific post type from being selectable.
	// Let's say we want to exclude the built-in 'attachment' post type.
	if ( isset( $option['post_types'] ) && is_array( $option['post_types'] ) ) {
		foreach ( $option['post_types'] as $key => $post_type_data ) {
			if ( $post_type_data['value'] === 'attachment' ) {
				unset( $option['post_types'][ $key ] );
				break; // Assuming there's only one 'attachment' entry.
			}
		}
		// Re-index the array after unsetting an element to maintain array keys if necessary.
		// This might not be strictly necessary depending on how the $option is used later,
		// but it's good practice for clean arrays.
		$option['post_types'] = array_values( $option['post_types'] );
	}

	// Example: Add a custom post type if it's not already present.
	// Let's assume 'my_custom_plugin_item' is a post type registered by another plugin.
	$custom_post_type_slug = 'my_custom_plugin_item';
	$custom_post_type_label = __( 'My Custom Plugin Items', 'text-domain' );
	$found_custom_post_type = false;

	if ( isset( $option['post_types'] ) && is_array( $option['post_types'] ) ) {
		foreach ( $option['post_types'] as $post_type_data ) {
			if ( $post_type_data['value'] === $custom_post_type_slug ) {
				$found_custom_post_type = true;
				break;
			}
		}
	} else {
		// Initialize post_types if it doesn't exist.
		$option['post_types'] = array();
	}

	if ( ! $found_custom_post_type && post_type_exists( $custom_post_type_slug ) ) {
		$option['post_types'][] = array(
			'label' => $custom_post_type_label,
			'value' => $custom_post_type_slug,
		);
	}

	return $option;
}

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/integrations/wp-all-import/helpers/wp-all-import-helpers.php:88

public function get_post_types_options( $label = '', $option_code = 'ALLPOSTTYPES', $args = array() ) {

		$defaults = array(
			'token'               => false,
			'comments'            => false,
			'is_ajax'             => false,
			'is_any'              => true,
			'plural_label'        => false,
			'target_field'        => '',
			'endpoint'            => '',
			'options_show_id'     => true,
			'use_zero_as_default' => intval( '-1' ),
		);

		$args = wp_parse_args( $args, $defaults );

		$options = array();

		if ( true === $args['is_any'] ) {
			$zero_as_default = ( intval( '-1' ) !== intval( $args['use_zero_as_default'] ) ) ? 0 : intval( '-1' );

			// Backwards compatibility for Any option with value of '0' instead of '-1'.
			$options[ $zero_as_default ] = esc_html__( 'Any post type', 'uncanny-automator' );
		}

		$post_types = get_post_types( array(), 'objects' );

		if ( ! empty( $post_types ) ) {

			foreach ( $post_types as $post_type ) {

				if ( $this->is_post_type_valid( $post_type ) ) {

					$options[ $post_type->name ] = ( true === $args['plural_label'] ) ? esc_html( $post_type->labels->name ) : esc_html( $post_type->labels->singular_name );

				}
			}
		}

		// Dropdown supports comments.
		if ( $args['comments'] ) {

			foreach ( $options as $post_type => $opt ) {

				if ( intval( $post_type ) !== intval( '-1' ) && ! post_type_supports( $post_type, 'comments' ) ) {

					unset( $options[ $post_type ] );

				}
			}
		}

		$option = array(
			'input_type'      => 'select',
			'option_code'     => $option_code,
			'label'           => ! empty( $label ) ? $label : esc_html__( 'Post type', 'uncanny-automator' ),
			'required'        => true,
			'supports_tokens' => $args['token'],
			'is_ajax'         => $args['is_ajax'],
			'fill_values_in'  => $args['target_field'],
			'endpoint'        => $args['endpoint'],
			'options'         => $options,
			'relevant_tokens' => array(),
			'options_show_id' => false,
		);

		return apply_filters( 'uap_option_get_post_types_options', $option );

	}

Scroll to Top