code listing: http://pastebin.com/0LEqgRUZ

I ran into a trouble with my plugin. (It is a class based plugin actually) I'm trying to use get_option function. When I just make var_dump(get_option('option_name', 1)) somewhere in plugin_options function (you'll see the code listing) - it outputs int(1)

The problem is that I cannot use get_option inside my log_to method. The log_to method is bound to 'all' hook. So I guess that's why it doesn't work. (even wp is not loading at all)

Anyone knows the resolution?

  • 2
    Please post the relevant code inline here. A question should not rely on a third party site.
    – s_ha_dum
    Sep 29 '13 at 17:35

get_option fires hooks itself so you are triggering a infinite loop (or something similar if not actually infinite). Your callback calls get_option, which triggers the hooks used by get_option. Each hook triggers the callback again, which used get_option which triggers the hooks used by get_option. And so on...

Get your option value in the constructor, outside of the callback method. That way, when your callback runs it is inadvertently triggering itself. Proof of concept:

class WP_Test_Logging_Plugin {

  static $data = array();

  public function __construct() {
    $this->data = get_option('option_name', 1);
    add_action( 'all', array( $this, 'log_to' ) );

  public function log_to() {
    $hook = current_filter();
$ntlp = new WP_Test_Logging_Plugin();
  • Yep, this is helpful. But also, I can't get why I am unable to add an option to a database. I tried both register_setting and add_action. No result. Sep 29 '13 at 21:20
  • Your problem with register_setting and with add_action is not part of the original question but if you are attempting to use those inside the callback you are going to have the same issue described in this answer.
    – s_ha_dum
    Sep 29 '13 at 21:23
  • oh, yeah. You're quite right. Have just changed it the appropriate way. Thank you a lot! Sep 29 '13 at 21:30
  • If this solved the question please mark it as "Accepted". Look for the check mark on the left.
    – s_ha_dum
    Sep 29 '13 at 21:36
  • Excuse me by the way. I really need to process form submission somehow. But I seen to run into the same trouble with endless loop. Are there any ways to go about it in my case? Thanks Sep 29 '13 at 21:44

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.