Dynamic Content - site option

Hi,

I’ve used X, and later Pro, for a long time now. I’ve written a lot of custom code over the years, and a lot of it can be replaced with the new features that have been released over the years, in particular Dynamic Content, in combination with Advanced Custom Fields.
One thing I haven’t been able to solve is using a site_option through Dynamic Content. ACF uses site options, but some of my custom code does it as well. For example through using the core update_option() function.

The challenge I’m currently facing is how to retrieve the value of that option using Dynamic Content.
So for example, somewhere in my code there’s this line, which updates an option in the DB: update_option('eb_gameresults_all_failed_abs', $failedGames);

How can I retrieve the value of this option, so I can use it within Pro?

Hello @dhunink,

Thanks for posting in!

You can use {{dc:acf:post_field field="eb_gameresults_all_failed_abs"}} in a Text element to get the value of the option.

To be familiar with ACF Pro and the Dynamic Content, please check this out:

As for the site options, it may not be possible with dynamic content.
Kindly let us know how it goes.

Hi @ruenel,

thanks for getting back at me!
I know how to get an ACF Field that belongs to a post, like in your example. The challenge I’m facing is retrieving an site option that has been set using update_option().

When diving into the codebase of Pro, I found that class-dynamic-content-global.php does not contain a function that allows a certain site option to be retrieved. It wouldn’t be that hard tough, I believe. The file class-dynamic-content-post.php does contain a function that could be converted to retrieve site options, I believe.

First, register the DC Field:

cornerstone_dynamic_content_register_field(array(
      'name'  => 'siteoption',
      'group' => 'global',
      'type'  => 'mixed',
      'label' => 'Site Option (Custom Field)',
      'controls' => array( 'option')
    ));

Next, for the callback, I believe it should be something like this:

public function supply_field( $result, $field, $args) {

    switch ($field) {
      case 'siteoption':
        if ( isset( $args['option'] ) ) {
          $result = get_option( $args['option'] , true)
          $result = get_post_meta( $post->ID, $args['key'], true );
         //@TODO: create a filter for site_options, similar to $result = apply_filters( 'cs_format_dynamic_content_post_meta', $result, $key );
        }
        break;
    }

    return $result;
  }

My question is: is there something already build into Pro’s DC, that allows retrieval of the value of a siteoption? If not, would implementing the suggested piece of code something worth considering (@alexander :wink: ) ?

Hello @dhunink,

I will forward this to Alexander and add your thoughts as a feature request.

Thanks.

Thanks @ruenel!
But in the meantime, I would like to incorporate this function into my child-theme, so I can test it and create a full working example, which is ready to be used. And possibly be copied by @alexander.
I haven’t found any documentation on how to extend on the theme php-classes, so I can continue the development. Could you share the go-to route to do so? Thanks!

Hello @dhunink,

In most cases, you can place your custom PHP code in your Child Theme’s functions.php file. You might want to check out this documentation:

If in some case your custom PHP code in the functions.php file of the child theme will not work, you will have to add your code into the parents theme file temporarily until the features that you want will be integrated with the theme by our developers.

Best Regards.

Hi @dhunink,

We don’t have anything to pull through a WordPress site option in Dynamic Content. You could certainly setup your own, and the code above looks like a great start. We’ll consider adding it officially, but it will need to wait until we get to the next cycle at least. We’re planning on adding more ways in Cornerstone to add and manage Global data so we want to make sure that’s in first.

Hi @alexander,

thanks for your comment on this. I’m looking forward to the next updates, the previews shared in the monthly update and on Facebook look promising!

In the mean time I would be more then happy to convert the code-sample I wrote into a ready to use snippet, which might make things easier when including it into the next cycle. Although I know there’s now guarantee of including it in any upcoming release, i would like to code and test it into my child-theme.

Should I use it as php-‘class CHILD extends NATIVE’, or do you recommend another approach when extending php-classes from Pro in a child-theme?

Hi @dhunink,

I’m really grateful for the offer to share the code with us. You’re welcome to share anything here that may benefit other users, but it won’t be necessary for the purposes of us potentially introducing it into Cornerstone. It will be fairly easy for us to add something like it but the question at the moment is if it’s something we want to support long term, and if it fits into some other changes we have planned for Dynamic Content.

1 Like

Hi @alexander,
thanks for responding! I might incorporate this into my child theme, as soon as I found out how to tap into Pro’s PHP-classes nicely. If I have some working code I’ll share it here, for the purpose of helping others.

The fact that it’s something that’s missing for me, as a big big fan of DC, doesn’t mean the single user case justifies for adding (and supporting it) into a next update. I totally understand that. But speaking from my personal point of view, it would be awesome :wink:

You are most welcome, @dhunink.

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