Use variables in Code Snippet

Hi,

I’m going to use Code Snippet to edit the wp-config.php file on my child sites. I need to include a value that’s found in the MainWP site tags. Can I retrieve the value of the tag for each site into a variable and use it in my code?

Thanks in advance,
Best regards

Bertrand

Interesting case, but I haven’t seen this usage yet. Can you explain what data you have in your dashboard, that would not be available on your child site? If the data could also be retrieved from the child site, you wouldn’t need the variables.

Hey @bfontaine

Welcome to the MainWP community.

I’m checking with the dev for more info on this.

@bfontaine I am not sure what exactly you need to accomplish, could you please provide us with more details? Maybe you can share some exact example?

Hi,

I have a WordPress plugin that reads a variable from the wp-config.php file and adjusts the site’s colors and logos based on the site category (value “site_cat” on the code below).

define( 'FRI_PRODUCT_TYPE', 'site_cat' );

In MainWP, each site has a tag indicating its category.
I would like to automatically retrieve the value of this tag into a variable that could be used in Code Snippet to insert the correct category into the wp-config.php file in place of the value “site_cat”.

I hope my explanation is detailed enough…

Thanks in advance for your ideas,
have a nice day.

Best Regards,
Bertrand

thanks @bfontaine for the additional details.

At the moment, the Code Snippets Extension does not support dynamically replacing snippet values with Dashboard-side data per child site.

Code Snippets sends the snippet code to the selected child sites, and the code is executed or saved on the child site side. It is not executed on the Dashboard site first, and there is no built-in placeholder/template system that would let the Dashboard inject values like the site’s assigned tag before sending the snippet.

However, for this specific case, unless you have a lot of tags, you can accomplish this with the following steps:

  1. Start new snippet
  2. Hard code the first group into the snippet: define( 'FRI_PRODUCT_TYPE', 'cat_1' );
  3. In the site selection, switch to Per Tag selection and select the first tag that matches your cat_1.
  4. Set the correct snippet type
  5. Run the snippet
    – This should set the FRI_PRODUCT_TYPE constant to targeted group of sites. And, you can repeat this for the rest of tags/categories… if there is a few, you ca finish this quickly. Before doing it, you should just confirm that all groups/tags are set properly.

I know this is not perfect solution, but it could work if there is no much categories and they don’t change too often.