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!

2 Answers 2

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
    Commented Jul 31, 2014 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
    Commented Jul 31, 2014 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? Commented Jul 31, 2014 at 15:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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