-
-
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); | |
} |
Whilst looking at your screenshot I can see it's fine, for others reading this, it's worth paying attention to the list of Valid Blocks that do_shortcode
should run on. You can find an array of valid blocks in the code.
The other thing is your ACF price field variable name is odd. Usually, you just access it by name. It looks like you've renamed the key manually to look like a variable. Is that right?
And if you have manually renamed the key, it might be worth reverting it to a normal text string just to troubleshoot your issue.
From:
{$price}
To:
price
for the naming within the shortcode I followed the ACF documentation.Indeed this was the issue. Just naming it "price" solved it! Thanks
Awesome! I'm pleased to hear you got it working!
Wow, this is wonderful, thank you! I'm actually using it at the moment with the query loop that comes with the (free version of the) GenerateBlocks plugin and it works beautifully.
Wow, this is wonderful, thank you! I'm actually using it at the moment with the query loop that comes with the (free version of the) GenerateBlocks plugin and it works beautifully.
I'm pleased to hear that it has helped you, @Blindeman! Thanks for letting me know.
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
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
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!
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:
Tried but failed. Custom Post Type (course) is created and usable, ACF field (price) is attached to it and showing on single posts (image 1), but adding the ACF shortcode to the Query Loop Block (image 2) does not give any result (image 3):
Getting the custom taxonomy to the list of posts is the next challenge. But first things first ;-)