Filter uncanny-automator

uap_option_get_all_ads

Filters all available ad options before they are retrieved for display or management.

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

Description

Fires when retrieving all available ads from Advanced Ads. Developers can use this filter to modify the list of ads or add custom ad types before they are used in the UI, providing flexibility for custom integrations and ad management.


Usage

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

Parameters

$option (mixed)
This parameter represents the option code used to retrieve specific ads or all ads.

Return Value

The filtered value.


Examples

add_filter( 'uap_option_get_all_ads', 'my_custom_uap_get_all_ads', 10, 1 );

/**
 * Filters the list of available ads for the Ultimate Affiliate Program (UAP)
 * to include only ads with a specific keyword in their title.
 *
 * @param array $options The original array of available ad options.
 * @return array The filtered array of ad options.
 */
function my_custom_uap_get_all_ads( $options ) {
    // Only proceed if $options is an array and not empty.
    if ( ! is_array( $options ) || empty( $options ) ) {
        return $options;
    }

    $filtered_options = array();
    $keyword_to_filter = 'sponsored'; // Example: only include ads with 'sponsored' in their title.

    foreach ( $options as $ad_id => $ad_data ) {
        // Check if the ad data has a 'label' and if it contains the keyword.
        if ( isset( $ad_data['label'] ) && strpos( strtolower( $ad_data['label'] ), strtolower( $keyword_to_filter ) ) !== false ) {
            $filtered_options[ $ad_id ] = $ad_data;
        }
    }

    // Return the modified options array.
    return $filtered_options;
}

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/advanced-ads/helpers/advanced-ads-helpers.php:64

public function get_all_ads( $option_code, $is_any = false, $all_option = false, $label = null ) {
		$options = array();
		$args    = array(
			// phpcs:ignore WordPress.WP.PostsPerPage.posts_per_page_posts_per_page
			'posts_per_page' => apply_filters( 'automator_select_all_posts_limit', 999, 'post' ),
			'orderby'        => 'title',
			'order'          => 'DESC',
			'post_type'      => 'advanced_ads',
			'post_status'    => array( 'publish', 'draft' ),
		);
		if ( true === $all_option ) {
			$all_ads = Automator()->helpers->recipe->options->wp_query( $args, $all_option, esc_html__( 'All ads', 'uncanny-automator' ) );
		} else {
			$all_ads = Automator()->helpers->recipe->options->wp_query( $args, $is_any, esc_html__( 'Any ad', 'uncanny-automator' ) );
		}

		foreach ( $all_ads as $ad_id => $title ) {
			if ( empty( $title ) ) {
				// translators: 1: Ad ID
				$title = sprintf( esc_attr__( 'ID: %1$s (no title)', 'uncanny-automator' ), $ad_id );
			}

			$options[ $ad_id ] = $title;
		}

		if ( $label === null ) {
			$label = esc_attr__( 'Ad', 'uncanny-automator' );
		}

		$option = array(
			'input_type'            => 'select',
			'option_code'           => $option_code,
			/* translators: HTTP request method */
			'label'                 => $label,
			'required'              => true,
			'supports_custom_value' => true,
			'relevant_tokens'       => array(),
			'options'               => $options,
		);

		return apply_filters( 'uap_option_get_all_ads', $option );
	}

Scroll to Top