acf_shortcode() │ ACF 1.1.1
Эта функция обработки шорткода [acf]
. Пример шорткода: [acf field="heading" post_id="123" format_value="1"]
.
Это внутренняя функция, которая используется для регистрации шорткода. Её не нужно использовать где-либо.
Возвращает
Строку
.
Использование
acf_shortcode( $atts );
- $atts(массив)
Массив параметров. Возможны следующие аргументы:
-
$field(строка) (обязательный)
Имя, ключ поля.
-
$post_id(разное) (обязательный)
ID поста поле которого нужно получить.
- $format_value(true/false)
Нужно ли форматировать значение поля.
По умолчанию: true
Примеры
Список изменений
Код acf_shortcode() acf shortcode
ACF 6.0.4
function acf_shortcode( $atts ) {
// Return if the ACF shortcode is disabled.
if ( ! acf_get_setting( 'enable_shortcode' ) ) {
return;
}
if ( function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) {
// Prevent the ACF shortcode in FSE block template parts by default.
if ( ! doing_filter( 'the_content' ) && ! apply_filters( 'acf/shortcode/allow_in_block_themes_outside_content', false ) ) {
return;
}
}
// Limit previews of ACF shortcode data for users without publish_posts permissions.
$preview_capability = apply_filters( 'acf/shortcode/preview_capability', 'publish_posts' );
if ( is_preview() && ! current_user_can( $preview_capability ) ) {
return apply_filters( 'acf/shortcode/preview_capability_message', __( '[ACF shortcode value disabled for preview]', 'acf' ) );
}
// Mitigate issue where some AJAX requests can return ACF field data.
$ajax_capability = apply_filters( 'acf/ajax/shortcode_capability', 'edit_posts' );
if ( wp_doing_ajax() && ( $ajax_capability !== false ) && ! current_user_can( $ajax_capability ) ) {
return;
}
$atts = shortcode_atts(
array(
'field' => '',
'post_id' => false,
'format_value' => true,
),
$atts,
'acf'
);
$access_already_prevented = apply_filters( 'acf/prevent_access_to_unknown_fields', false );
$filter_applied = false;
if ( ! $access_already_prevented ) {
$filter_applied = true;
add_filter( 'acf/prevent_access_to_unknown_fields', '__return_true' );
}
// Try to get the field value.
$value = get_field( $atts['field'], $atts['post_id'], $atts['format_value'] );
if ( $filter_applied ) {
remove_filter( 'acf/prevent_access_to_unknown_fields', '__return_true' );
}
if ( is_array( $value ) ) {
$value = implode( ', ', $value );
}
return $value;
}
Cвязанные функции