Option to avoid adding core filters?
-
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_shortcodewon’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_idfilter 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 );
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
You must be logged in to reply to this topic.