Registering Basic ACF Block in Sage 9

Hello, I have a basic question for Sage 9, no Bedrock.

I’m trying to setup the normal, boring ACF Blocks, no custom plugins that I know some of you have. I’m using this guide.

The first thing I’ve done is add the following to my setup.php file:

function register_acf_block_types() {

    // register a testimonial block.
    acf_register_block_type(array(
        'name'              => 'testimonial',
        'title'             => __('Testimonial'),
        'description'       => __('A custom testimonial block.'),
        'render_template'   => 'app/Blocks/Testimonial/testimonial.php',
        'category'          => 'formatting',
        'icon'              => 'admin-comments',
        'keywords'          => array( 'testimonial', 'quote' ),
    ));
}

// Check if function exists and hook into setup.
if( function_exists('acf_register_block_type') ) {
    add_action('acf/init', 'register_acf_block_types');
}

I get the following error though:

Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'register_acf_block_types' not found or invalid function name in /app/public/wp-includes/class-wp-hook.php on line 286

I feel this should be a very simple thing, but I’m not sure what’s going on. My Sage theme doesn’t seem to want to communicate very well with the main WordPress files or plugins. Any suggestions?

If you look at the top of setup.php you’ll see

<?php

namespace App;

This means that setup.php is in the App namespace. Whenever you pass callbacks to add_action or add_filter or whatever you must include the namespace. You can write the whole thing out, but in this context the easiest way is to use the __NAMESPACE__ magic constant. So something like this:

if( function_exists('acf_register_block_type') ) {
    add_action('acf/init', __NAMESPACE__.'\\register_acf_block_types');
}

If you don’t anticipate needing to un-hook that action later, you can do what many of us do and pass an anonymous function as a callback instead:

if( function_exists('acf_register_block_type') ) {
    add_action('acf/init', function() {

      // register a testimonial block.
      acf_register_block_type(array(
        'name'              => 'testimonial',
        'title'             => __('Testimonial'),
        'description'       => __('A custom testimonial block.'),
        'render_template'   => 'app/Blocks/Testimonial/testimonial.php',
        'category'          => 'formatting',
        'icon'              => 'admin-comments',
        'keywords'          => array( 'testimonial', 'quote' ),
      ));
    });
}
5 Likes

OMG, you’re amazing @alwaysblank!! Thank you very much for the detailed explanation and response.
:smiley: :smiley: :smiley:

This topic was automatically closed after 42 days. New replies are no longer allowed.