Skip Navigation

[Resolved] FAO: Nigel – GenerateBlocks Workaround Broken with latest Toolset update/s

This support ticket is created 3 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 9 replies, has 2 voices.

Last updated by JoelK2744 3 years, 9 months ago.

Assisted by: Jamal.

Author
Posts
#2030839

Don't worry, it's to do with the dynamic assignment of the Content Template, it's resolved by manually selecting the correct template.

#2030867

My apologies it's not to do with the update (at least not directly) I've noticed it's happening on posts that have "Template Dynamically Assigned By Toolset" - any thoughts? Is there an option to avoid this or a way to bulk edit?

Thanks

#2031057

My issue is resolved now. Thank you!

#2031127

Sorry, reopening this as it reverts back to "Template Dynamically Assigned By Toolset" every time an edit is made and breaks the styling. Is there any way of turning off this feature or manually choose the correct template without it changing with each edit?

Thanks

#2031665

Hello and thank you for contacting the Toolset support.

Nigel does not work on weekends, if you don't mind I'll continue with you on this ticket. If you prefer to get Nigel, let me know and I'll assign this thread to him.

I am sorry, but I don't understand what do you mean by "Template Dynamically Assigned By Toolset", can you share a screenshot?

Would you like to reproduce this issue on one of our test sites? If yes, please log into this test site using this One-click-link hidden link
If the issue is reproduced, I'll escalate it to our developers and see if we can work around it somehow.

If it is not reproduced, we can assume that it is not a bug, rather an exception that happens only on your website. In that case, we'll need to take a copy of your website and debug it.

#2031677

Thanks Jamal. The same issue is happening on Sandbox. I have created a test post type with test post and created a content template. I have installed GenerateBlocks and added Nigel's workaround - https://toolset.com/errata/generateblocks-styles-lost-on-front-end-when-using-content-templates/ - via Code Snippets plugin.

I have tested by adding a GenerateBlocks button with pink background into the Content Template. With Nigel's snippet activated the pink button doesn't show unless you manually choose the "Template for Test". The problem is that it keeps reverting back eventually to the "Template Dynamically Assigned By Toolset".

To see "Template Dynamically Assigned By Toolset" just go to the "Test" post in "Test" CPT and click edit, on the right hand sidebar you should see the option to change the Content Template - it will be showing "Template Dynamically Assigned By Toolset" - if you change this to "Template for Test" and save then the pink button will show as it should do.

What I've found is that it stays as the manually chosen template for a while but eventually reverts back to "Template Dynamically Assigned By Toolset" - I've not been able to pinpoint why this happens, not sure if it just takes a bit of time or when you add more posts, or edit certain fields, I've just noticed that it does eventually happen.

Hope that all makes sense.

#2031729

Thank you very much for the collaboration. I checked our internal issues tracking tool and the issue is not resolved yes.

I believe that the workaround does not work anymore because of the new content template assignment system. Previously, we could assign only ONE content template to a post. Now, we can assign multiple templates with conditions and priority, and Toolset will decide what content template will be used.
I don't think this new system will rely on the _view_template metadata on the post that Nigel used in the workaround.

On the errata page, "stuart read" has suggested a workaround which seems to work. I added it to Toolset->Settings->Custom Code(why using another plugin for that 😉 ) as a snippet after I edited it because it was not fully correct(copy/paste issue).

add_filter( 'generateblocks_do_content', 'generateblock_template_support_testing', 10, 2);

function generateblock_template_support_testing($content) {
  
  $args = array(
    'numberposts' => -1, // get all posts.
    // 'include' => array(), //specify a list of ids comma seperated to include, for performance reasons
    // 'exclude' => array(), //specify a list of ids comma seperated to exclude, for performance reasons
    'post_type' => 'view-template', //change this is you need to use it on a differnet custom post type, useful for other plugins that uses GenerateBlocks and custom post types.
  );
  $my_posts = get_posts( $args );
  
  if( ! empty( $my_posts ) ){
    foreach ( $my_posts as $p ){
      if ( has_blocks( $p ) ) { //used to check if its using blocks or not
        $content .= $p->post_content;
      }
    }
  }
    return $content;
};

Please check it on your website and let me know what you will get.

I'll check with Nigel if we can add it to the errata page or come up with another workaround.

#2031825

Thanks Jamal...of course I didn't think to use the Toolset custom code option for the snippet!

Yes I did see that but thought it best to see if there is an 'official' Toolset workaround as he does make the point that he's not a php expert and that it may have performance issues?

I can confirm it works but I'll see if Nigel has a better alternative before implementing on our site.

Thanks

#2033175

Hello! Nigel has updated the workaround with a working code. https://toolset.com/errata/generateblocks-styles-lost-on-front-end-when-using-content-templates/

add_filter('generateblocks_do_content', function ($content) {

    global $post;

    // is the post being displayed with a Content Template?
    $template_id = apply_filters( 'wpv_content_template_for_post', 0, $post );

    if (isset($template_id) && !empty($template_id)) {

        if (has_blocks($template_id)) {
            $template = get_post($template_id);
            if (!$template) {
                return $content;
            }

            // Append the template "content" to the post content, for scanning by GP
            $content .= $template->post_content;
        }
    }

    return $content;
});

With the latest update, Toolset Blocks 1.5, the content template used by a post is no longer stored in postmeta (as _view_template) because of the conditional template assignment. Now we need to use the following to retrieve the ID of the template that will be used for a post using its $post object:

$template_id = apply_filters( 'wpv_content_template_for_post', 0, $post );
#2033199

Great, thanks both!