Skip to content
This repository has been archived by the owner on Oct 22, 2023. It is now read-only.

Commit

Permalink
Create acf-shortcode-extended.php
Browse files Browse the repository at this point in the history
  • Loading branch information
3n3a authored May 25, 2023
1 parent 34d4488 commit 79847f4
Showing 1 changed file with 68 additions and 0 deletions.
68 changes: 68 additions & 0 deletions acf-shortcode-extended.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/**
* ACF Shortcode Extended
*
* @package ShortcodeExtended
* @author 3n3a
* @copyright 2023 3n3a
* @license GPL-2.0-or-later
*
* @wordpress-plugin
* Plugin Name: ACF Shortcode Extended
* Plugin URI: https://github.com/seemly/shortcode-extended
* Description: Enable ACF custom field usage, in a shortcode, in Query Loop block
* Version: 0.0.2
* Requires at least: 6.0
* Author: 3n3a
* Author URI: https://3n3a.ch
* Text Domain: acf-shortcode-extended
* License: GPL v2 or later
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/

if(!class_exists('ACFShortcodeExtended'))
{
class ACFShortcodeExtended
{
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 field="') === 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);

}

0 comments on commit 79847f4

Please sign in to comment.