0

I am creating a simple "global settings" plugin, which simply saves a few text fields. Saving works as intended, and I can retrieve my data in frontend as follows:

<?php $options = get_option('wpglobalsettings'); ?>
<?php echo $options['wpglobalsettings_image1']; ?>

What I would like though, would be to avoid having to call get_option('wpglobalsettings'); on each page I plan to use my plugin on.

So I was thinking I could add my $options to an action hook. I did the following, but it doesn't work ( I get an empty array.

function wp_global_settings_frontend(){
  $options = get_option( 'wpglobalsettings' );
}

add_action('the_content', 'wp_global_settings_frontend');

Can You help? Many thanks!

1

First of all, the_content is a filter hook and not action. Secondly, it will only work when post content will be access on your site front end. You can probably make use of global variables instead.

e.g.

$options = get_option( 'wpglobalsettings' );

and then use wherever you want to access as follows -

global $options;
1
  • I did it like this and it works – akmur Jul 31 '14 at 12:52
0

Because your $options variable is limited to just the scope of the wp_global_settings_frontend() function.

What you want to do here is set it as a constant instead of a local variable.

function wp_global_settings_frontend(){
  define('OPTIONS', get_option('wpglobalsettings');
}

add_action('the_content', 'wp_global_settings_frontend');

http://php.net/manual/en/language.constants.php

Just make sure the hook you tie it to fires before you use it.

Also, maybe pick a constant name that will be more unique.

2
  • This looks cool, but when I try it and reload my theme it's blank O__O I'm sure it's me doing something wrong – akmur Jul 31 '14 at 12:53
  • Like WisdmLabs said 'the_content' isn't the right hook for this. How were you trying to access your constant after you set it? – Christopher Lamm Jul 31 '14 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.