• Resolved meteorlxy

    (@meteorlxy)


    Hi, I’m now using the ACF plugin and enabled its shortcodes feature.

    However, as the ACF shortcode require the global post context to determine which post to read custom fields from, using a simple do_shortcode won’t work properly.

    add_filter( 'the_title', 'do_shortcode' );
    add_filter( 'single_post_title', 'do_shortcode' );
    add_filter( 'wp_title', 'do_shortcode' );
    add_filter( 'the_excerpt', 'do_shortcode' );

    As a workaround, we can either:

    • Remove the filters added by DMYIP and implement a new one
    • Load / restore global post context before / after DMYIP filters
    • Add / remove acf/pre_load_post_id filter before / after DMYIP filters

    I think it could be more flexible if we can have an option / filter to avoid adding the core filters.

    Another option is to compat with ACF shortcodes. Then we may need something like this:

    add_filter( 'the_title', function($title, $post_id) {
    $filter = fn($null, $shortcode_post_id) => $shortcode_post_id ?: $post_id;
    add_filter('acf/pre_load_post_id', $filter, 10, 2);
    try {
    return do_shortcode($title);
    } finally {
    remove_filter('acf/pre_load_post_id', $filter, 10);
    }
    }, 10, 2 );
    • This topic was modified 3 months, 2 weeks ago by meteorlxy.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Gaurav Tiwari

    (@gauravtiwari)

    I will add a filter to avoid adding do_shortcode to the native WP elements. It’s not just about ACF – maybe the user doesn’t want it at all.

    I just don’t want to add a settings page, otherwise the plugin could have been very customizable.

    Plugin Author Gaurav Tiwari

    (@gauravtiwari)

    Added in 1.7.1:

    Disable all core filters (shortcodes will work but will only work in post content and blocks):

    add_filter( 'dmyip_core_filters', '__return_empty_array' );

    Disable specific filters — e.g., stop do_shortcode on the_title (fixes ACF field name conflicts):

    add_filter( 'dmyip_core_filters', function ( $filters ) {
        $filters['the_title'] = false;
        return $filters;
    } );

    Disable multiple filters at once:

    add_filter( 'dmyip_core_filters', function ( $filters ) {
        $filters['the_title']         = false;
        $filters['single_post_title'] = false;
        $filters['wp_title']          = false;
        return $filters;
    } );

    Only keep excerpt processing (disable everything else):

    add_filter( 'dmyip_core_filters', function () {
        return array(
            'the_title'             => false,
            'single_post_title'     => false,
            'wp_title'              => false,
            'the_excerpt'           => true,
            'get_the_excerpt'       => true,
            'get_the_archive_title' => false,
        );
    } );

    Available filter keys Key WordPress filter What it does the_titlethe_title Processes shortcodes in post titles displayed on the page single_post_titlesingle_post_title Processes shortcodes in the browser tab title (single posts) wp_titlewp_title Processes shortcodes in wp_title() output the_excerptthe_excerpt Processes shortcodes in displayed excerpts get_the_excerptget_the_excerpt Processes shortcodes when retrieving raw excerpt get_the_archive_titleget_the_archive_title Processes shortcodes in archive page titles

    All default to true. Set to false to disable.

Viewing 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.