Filter uncanny-toolkit-pro

uncanny_toolkit_restrict_access_post_types

Filters the available post types that can be restricted by the Uncanny Toolkit for content access.

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

Description

Filters the list of public post types available for access restriction by Uncanny Toolkit's Page Redirects feature. Developers can add, remove, or modify post types to control which content can be restricted. This hook fires after the initial public post types are fetched and attachments are excluded.


Usage

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

Parameters

$raw_post_types (mixed)
This parameter contains an array of post type slugs that are publicly available, with the exception of 'attachment'.

Return Value

The filtered value.


Examples

/**
 * Example function to filter the post types available for Uncanny Toolkit's
 * "Restrict Page Access" module. This example will exclude custom post types
 * starting with 'ut_'.
 *
 * @param array $raw_post_types An array of registered public post types.
 * @return array The filtered array of post types.
 */
add_filter( 'uncanny_toolkit_restrict_access_post_types', 'my_custom_exclude_ut_post_types', 10, 1 );

function my_custom_exclude_ut_post_types( $raw_post_types ) {
    // Iterate through the post types and remove any that start with 'ut_'
    foreach ( $raw_post_types as $key => $post_type ) {
        if ( strpos( $post_type, 'ut_' ) === 0 ) {
            unset( $raw_post_types[ $key ] );
        }
    }
    return $raw_post_types;
}

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/classes/restrict-page-access.php:851

public static function page_redirect_select( $post, $settings ) {

		$raw_post_types 	 = get_post_types( array( 'public' => true ) );
		unset( $raw_post_types['attachment'] );

		$post_types      = apply_filters( 'uncanny_toolkit_restrict_access_post_types', $raw_post_types );

		$available_posts = array();

		foreach ( $post_types as $post_type ) {
			$post_type_labels = get_post_type_object( $post_type )->labels;

			$posts = get_posts( array(
				'post_type'      => $post_type,
				'posts_per_page' => 200,
				'orderby'        => 'post_title',
				'order'          => 'ASC'
			) );

			if ( count( $posts ) > 0 ) {
				$available_posts[ $post_type ] = [
					'name'  => $post_type_labels->singular_name,
					'posts' => []
				];
			}

			foreach ( $posts as $post ) {
				$available_posts[ $post_type ]['posts'][ $post->ID ] = $post->post_title;
			}
		}

		$label = esc_attr__( 'Page', 'uncanny-pro-toolkit' );

		?>

        <div class="ultp-restrict-access__option ultp-restrict-access__option--redirect-to-post-option">
            <div class="ult-form-element">
                <div class="ult-form-element__field">
                    <label for="ultp-restrict-access__groups">
						<?php echo $label; ?>
                    </label>
                    <select name="uo_restriction_settings[page_redirect]">
						<?php

						foreach ( $available_posts as $post_type => $data ) {

							echo '<optgroup label = "' . $data['name'] . '" > ';

							foreach ( $available_posts[ $post_type ]['posts'] as $id => $post_name ) {
								echo '<option value = "' . $id . '"' . selected( $id, $settings['page_redirect'], false ) . ' > ' . $post_name . '</option > ';
							}

							echo '</optgroup > ';
						}

						?>
                    </select>
                </div>
            </div>
        </div>

		<?php
	}

Scroll to Top