Skip to content

Instantly share code, notes, and snippets.

@seemly
Last active May 3, 2024 12:21
Show Gist options
  • Save seemly/1e57547ea07a954f5825f438a40e4254 to your computer and use it in GitHub Desktop.
Save seemly/1e57547ea07a954f5825f438a40e4254 to your computer and use it in GitHub Desktop.
Enable the use of ACF shortcodes within the native Query Loop gutenberg block.
<?php
/**
* Shortcode Extended
*
* @package ShortcodeExtended
* @author Chris Sparshott
* @copyright 2022 Chris Sparshott
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: Shortcode Extended
* Plugin URI: https://github.com/seemly/shortcode-extended
* Description: Enable ACF custom field usage in Query Loop block
* Version: 0.0.1
* Requires at least: 5.2
* Author: Chris Sparshott
* Author URI: https://sparshott.co.uk
* Text Domain: shortcode-extended
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
if(!class_exists('ShortcodeExtended'))
{
class ShortcodeExtended
{
public function render($attributes, $content, $data)
{
// just a failsafe
if(!($data instanceof WP_Block))
{
return $content;
}
// if no ACF not activated or installed, then return early.
if(!function_exists('get_field'))
{
return $content;
}
// if no ACF shortcode found in content, then return early.
if(strpos($content, '[acf ') === false)
{
return $content;
}
// Native functionality is to call `wpautop`, which means we lose access to the Post ID and ACF related data
return do_shortcode($content);
}
}
add_filter('register_block_type_args', function ($args, $name)
{
// Here we list the native blocks we are likely to include ACF shortcodes in.
// This list probably needs to be expanded, but suits my immediate requirements.
$validBlocks = ['core/shortcode', 'core/paragraph', 'core/list'];
// override the native render_callback function to ensure ACF shortcodes run as expected.
if(in_array($name, $validBlocks))
{
$args['render_callback'] = [new ShortcodeExtended, 'render'];
}
return $args;
}, 10, 2);
}
@seemly
Copy link
Author

seemly commented May 12, 2023

Thank you very much for the plugin. I got my ACF fields working on a loop that iterates courses(custom posts) in a page thanks to this plugin. But I had a failure putting it to work in a loop that is in WP full site editor(edit site). I was trying to do this on a custom taxonomy template. Feeling sad, anyone have any ideas how to do this? Or how to modify or start to understand what is so different there? An image from WP fill site editor Screenshot 2023-05-11 at 20 18 12

Unfortunately, I have no experience with full site editing (yet). It's also not 100% clear to me what the problem is here, and I don't have the time to try to recreate and troubleshoot your specific issue.

Sorry!

@kaiakonsap
Copy link

Yes, i understand, it needs quite long investigation. If I do come up with anything I will post it here. Of course all hints still very welcome from others.

@kaiakonsap
Copy link

kaiakonsap commented May 12, 2023

Thank you again Seemly. I solved it for my own use and still with the use of your great and simple plugin.
In short, I added to your conditional statement which checked if acf shortcode is NOT being used to check the same also for my own shortcode. So in the end, my shortcode is being treated like the acf shortcode. And this did the trick. I still do not understand why, what is the difference between my shortcode and acf shortcode. But it just worked. Beforehand the ID of elements in the loop was the same. It was not the parent page/ taxonomy ID but it took the first ID of the element in the loop and repeated it for the others in the loop.

My own shortcode comes from simple custom plugin, firstly uses get_the_ID() function to get the id of current item in the loop and then acf function to get the field - get_field('field','id')
And somehow this combination works in the custom template for taxonomy archive created in WP full site editor(WP 2023 theme ) and simple acf shortcode will not work.
Some images for those interested or having trouble with solving the same issue.
Highlighted edited part of original plugin from Seemly:
Screenshot 2023-05-12 at 17 44 15

My plugin here:
Screenshot 2023-05-12 at 17 52 35

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment