We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

Custom Global Variables

Development and Programming

vw000's avatar
vw000
241 posts
3 years ago
vw000's avatar vw000

I was following this article here: https://u.expressionengine.com/article/custom-global-variables-using-the-config-file

But it does not see me to work for me.

I added the example code in: /expressionengine/user/config.php

Then tried a variable, but I just get the variable printed in my template, such as:

{global:some_api_key}

Instead of the actual value.

       
Andy McCormick's avatar
Andy McCormick
183 posts
3 years ago
Andy McCormick's avatar Andy McCormick

vw000 , I’m not sure if there’s some special that Brian has in his environment that allows him to do that in his config.php file, but that always only works for me in my /index.php file. There’s even a section in there for custom variables.

       
Brian Litzinger's avatar
Brian Litzinger
693 posts
3 years ago
Brian Litzinger's avatar Brian Litzinger

You might need to add this at the beginning:

global $assign_to_config;

I’m not sure if the global part is required, but it’s what I have in one particular environment.

The final code in your config.php file would look like this

global $assign_to_config;

$default_global_vars = [
    'global:disabled_params' => 'disable="trackbacks|pagination|member_data"',
    'global:disabled_params_all' => 'disable="trackbacks|pagination|member_data|category_fields|categories|custom_fields"',
    'global:disabled_params_strict' => 'disable="trackbacks|pagination|member_data|category_fields|categories"',
    'global:blog_category_groups' => '2|3|5|6',
    'global:some_api_key' => '98ufahaskdfnasd312',
];

if (!isset($assign_to_config['global_vars'])) {
    $assign_to_config['global_vars'] = [];
}

$assign_to_config['global_vars'] = array_merge(
    $default_global_vars,
    $assign_to_config['global_vars']
);
👍 1
       
Andy McCormick's avatar
Andy McCormick
183 posts
3 years ago
Andy McCormick's avatar Andy McCormick

yep. that seems to be the missing piece. I add that line to my config and can now use this in my config.php file. Nice work Brian.

       
vw000's avatar
vw000
241 posts
3 years ago
vw000's avatar vw000

Thank you, Brian, I will test it out again.

Is there any preference on the position? Top of config or bottom? I assume not, since it just parses the code in the file regardless where it’s located.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.