acf/shortcode/prevent_access


目录

Description

Allows you to limit the fields which can be output by the ACF Shortcode. Returning true from this filter will prevent the value being output by the shortcode.

Changelog

Parameters

apply_filters( 'acf/shortcode/prevent_access', $access_prevented, $atts, $decoded_post_id, $decoded_post_type, $field_type, $field )

Example: Deny access to options pages

This example prevents the ACF Shortcode outputting any values stored in an options page.

functions.php

<?php
function my_acf_prevent_access_to_options( $access_prevented, $atts, $decoded_post_id, $decoded_post_type ) {
    if ( 'option' === $decoded_post_type ) {
        return true;
    }
    return $access_prevented;
}
add_filter( 'acf/shortcode/prevent_access', 'my_acf_prevent_access_to_options', 10, 4 );

Example: Deny access to field values on private posts

This example prevents the ACF Shortcode outputting any values stored in a post which is not publicly visible.

Please note: Since ACF 6.3.4, this behaviour has become default.

functions.php

<?php
function my_acf_prevent_access_to_fields_on_private_posts( $access_prevented, $atts, $decoded_post_id, $decoded_post_type ) {
    if ( 'post' === $decoded_post_type && ! is_post_publicly_viewable( $decoded_post_id ) ) {
        return true;
    }
    return $access_prevented;
}
add_filter( 'acf/shortcode/prevent_access', 'my_acf_prevent_access_to_fields_on_private_posts', 10, 4 );

Example: Deny access to every field apart from “my_field_name”

This example prevents the ACF Shortcode outputting any values other than the “my_field_name” field.

functions.php

<?php
function my_acf_prevent_access_to_all_but_one_field( $access_prevented, $atts, $decoded_post_id, $decoded_post_type, $field_type, $field_array ) {
    if ( 'my_field_name' === $field_array['name'] ) {
        return false;
    }
    return true;
}
add_filter( 'acf/shortcode/prevent_access', 'my_acf_prevent_access_to_all_but_one_field', 10, 6 );

版本号 #1
由 壳壳虫 创建于 2026-08-29 15:44:03 UTC
由 壳壳虫 更新于 2026-08-29 15:44:03 UTC