Global Block Shortcode variable

I have been trying to find a solution for adding/modifying a global block parameter via the shortcode:
[cs_gb id=182748 custom-text=“THIS TEXT HERE”]

This particular shortcode is for an offscreen content toggle. the “custom-text” would be the text that populates the “Primary Text” in the “Text Setup” section of the “toggle”

I have been able to get this to work by adding in a custom field which then is editable via a meta box in the standard editor–however, this is not very intuitive for my users (they have to toggle between Cornerstone and the standard editor to access this field). {{dc:post:meta key=“button-text”}}

My Goal would be to grab the attribute that is added to the shortcode to, in this case, modify the button text. Even if I can’t get this to write directly to the button, at least adding it as a parameter to the href would be huge. I could then use some jquery to replace the text.

Hi @speckledjay,

Regretfully, there isn’t a simple way to do this. I’ve added some notes to a feature request we already have about building this out natively. Meanwhile, I do have an idea of how you could get close with custom code. Custom development is outside the scope of support we can offer so I can’t promise a full implementation but I can share some advice on how I would get started. If you’re comfortable with PHP you can try playing around with this in a child theme.

global $recent_gb_atts = [];
    add_filter( 'shortcode_atts_cs_gb', function( &$atts ){
      global $recent_gb_atts = $atts;
      return $atts;
    });

This will listen for anytime the WordPress shortcode_atts function is called on cs_gb and stash the attributes so they’re accessible later.

add_filter('cs_dynamic_content_gb', function( $result, $field, $args ){
  global $recent_gb_atts;
  if ($field === 'att' && isset( $args['key'] ) && isset( $recent_gb_atts[ $args['key'] ] )) {
    $result = $recent_gb_atts;
  }
  return $result;
},10, 3);

This registers a new dynamic content handler that will look for {{dc:gb:att key="text"}} which should output the shortcode attribute text.

So that’s a basic overview of the approach. I’ve not tested it, so you might need to make adjustments to get it fully working. One key flaw in this technique is that if you nest Global Blocks, you end up overwriting that global variable, so be careful in how you use it.

What we will probably do in a future release is add a custom attribute control to the Global Blocks element in addition to exposing the shortcode attributes.

Thank you so much Alexander. This is a great start and I head this direction.
Do you have a “rough” ETA on if/when this functionality may be built in? I would like to weigh my time vs. the potential native integration (if this is going to be done in the next two months, then not worth billing for my time).

Thank you!

Hi @speckledjay,

Unfortunately we don’t have a timescale that we can share at this stage. Although Alex has taken some notes on this, it’s still very much on the drawing board and we cannot guarantee that it will be implemented, let alone where it would fall in terms of prioritisation. So at this stage, I would encourage you to pursue this directly if it’s something that is important to you.

Thanks!

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.