-
-
Save seemly/1e57547ea07a954f5825f438a40e4254 to your computer and use it in GitHub Desktop.
<?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); | |
} |
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.
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:
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!