• Resolved Martin Taylor

    (@docdunning)


    I’m using block_lab_add_field, in the block_lab_add_blocks action (as per the docs).

    In my associated function, I want to populate the options in a <select> field with the names of the terms in a particular taxonomy.

    Problem – I can’t use get_terms() for this, because the hook runs so early in WP’s init() action. I tracked down the line in class-loader.php, which says:
    add_action( ‘init’, $this->get_callback( ‘retrieve_blocks’ ), 1 );

    That priority “1” is the issue – it means that the taxonomies haven’t yet been set up, so get_terms() just returns an error. If I change the priority to “10”, then my code works OK. Obviously, I’ve reverted to “1” now, lest it causes any issues for Block Lab.

    I’m having to work around this by doing a wp_query to the database, so it’s kind of OK, but it would be better to be able to use get_terms().

    Is there a reason for the high priority 1 as things stand? Could it change to something like 10?

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Ryan Kienstra

    (@ryankienstra)

    Hi @docdunning,
    Thanks, that’s a good suggestion.

    Could you please test this build of the plugin, with the suggestion applied (of course on a non-production server):

    https://github.com/getblocklab/block-lab/pull/515#issuecomment-584847591

    Thread Starter Martin Taylor

    (@docdunning)

    Thanks, Ryan

    Confirmed – all working. In my theme functions.php, I have this code that now works, whereas previously “get_terms” returned a WP Error object.

    $terms = get_terms( array(
    ‘taxonomy’=>’ddownload_category’,
    ‘hide_empty’=>false
    ));
    $options = array(array(‘label’=>’Select’, ‘value’=>’none’));
    foreach ($terms as $term) {
    $name = $term->name;
    $slug = $term->slug;
    $options[] = array(‘label’=>$name, ‘value’=>$slug);
    }
    block_lab_add_field (‘download’, ‘download-category’,
    array (
    ‘label’ => ‘Download category’,
    ‘control’ => ‘select’,
    ‘width’ => ’50’,
    ‘options’ => $options
    )
    );

    I also went through your test script as per the github page, with no issues.

    Thread Starter Martin Taylor

    (@docdunning)

    Hmm. I’m just trying this on another WP test site, and the problem has returned. Using get_terms() in the block_lab_add_blocks action returns a WP Error. I can’t work out why at the moment, but just to note that I’m not sure whether this fix has been successful. Sorry ??

    Plugin Author Ryan Kienstra

    (@ryankienstra)

    All good, thanks for testing this.

    Plugin Author Ryan Kienstra

    (@ryankienstra)

    When is the register_taxonomy( 'ddownload_category’, ... ) called? Is it called before the block_lab_add_blocks action?

    Thread Starter Martin Taylor

    (@docdunning)

    Good question ?? The taxonomy is created by a plugin, and I realised overnight that my two test sites had different versions of the plugin. After I updated the second site to the later version, all was well. The author must have changed the code around and the register_taxonomy() is now in a “good” place.

    Edit: Confirmed – the earlier version of the plugin had
    add_action( ‘init’, ‘register_taxonomies’ );
    The new version has
    add_action( ‘init’, ‘register_taxonomies’, 3 );

    Don’t know why this was changed – the plugin isn’t supported any more (which is one of the reasons I’m using Block Lab).

    Anyway, you’re not here to support other authors’ work ??

    Thanks for the help.

    Plugin Author Ryan Kienstra

    (@ryankienstra)

    Hi @docdunning,
    Sounds good, good to hear that you tracked it down.

    So it sounds like the Block Lab build above does fix the issue, right? If so, I’ll request a code review from my teammate Luke and we’ll work on getting it merged.

    Thanks, Martin!

    Thread Starter Martin Taylor

    (@docdunning)

    So it sounds like the Block Lab build above does fix the issue, right

    Yes indeed. Or at least, it gives custom taxonomies a chance to be registered first.

    Thanks

    Plugin Author Ryan Kienstra

    (@ryankienstra)

    Sounds good, thanks. I’ll request a review of that PR.

    Plugin Author Ryan Kienstra

    (@ryankienstra)

    Hi @docdunning,
    Hope you’re having a great day.

    The PR that you tested is now merged. It’ll be deployed in the next release to wp.org. Though I’m not sure when that’ll be.

    https://github.com/getblocklab/block-lab/pull/515

    Thread Starter Martin Taylor

    (@docdunning)

    Thanks, @ryankienstra

    Thread Starter Martin Taylor

    (@docdunning)

    Hi @ryankienstra

    Just to report that while the change you’ve made is very welcome, it turns out that I still can’t reply on other plugins to register their taxonomies ahead of the block_lab_add_blocks hook.

    I came across another today (The Events Calendar). I tried to use get_terms() to grab the list of event categories for a block field, but it didn’t return anything. The Events Calendar presumably registers its taxonomies too late.

    No worries – I can use a WP Term Query to grab the values, which is probably what happens in get_terms() anyway.

    Thanks, though ??

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Can priority of “init” actions be reduced (for taxonomies)?’ is closed to new replies.