0

I have a class which loads the image sliders on some add_action. There is a function/method "render_slider" which renders the sliders on the page based on the slider selected. The theme tries to get the image slider object before the sliders are loaded.

I tried with init,plugins_loaded,to load the slider.

But still I get the error object not found

class Sliders {
  public function __construct() {
    add_filter( 'add_sliders', array( $this, 'add_sliders' ) );
    add_filter('plugins_loaded',array($this,'load_sliders'),10);
  }
  public function add_sliders($sliders) {
  }
  public function  load_sliders() {  
     $sliders_to_load = apply_filters( 'add_sliders', array() );
     foreach ( $sliders_to_load as $slider )
        $this->sliders[ $slider->id ] = $slider;
  }
  public function render_slider($slider_type = ''){  
      switch($slider_type){
          case 'rotator' : $rotator = $this->sliders[$slider_type];break; //Object Not Found
          default: break;
      }
  }
}
5
  • when you construct the class? i.e. when you call new Sliders?
    – gmazzap
    Apr 1, 2014 at 16:49
  • Instance is created by the action init Apr 1, 2014 at 17:07
  • 1
    That's your problem. 'plugins_loaded' is triggered before than 'init'. So when you create an the Slider instance and add hook to 'plugins_loaded' it does nothing: you can't add a function to an hook that have been already fired... use 'plugins_loaded' to create the instance, and inside __construct use add_filter('after_setup_theme',array($this,'load_sliders'),10);
    – gmazzap
    Apr 1, 2014 at 17:28
  • I created instanse on action plugins_loaded and then I load the sliders on action after_theme_setup. Till here it works fine. It loads the slider. Then again I get the object not found. I checked the code it loads the slider only once. Dont know where I am missing? Apr 2, 2014 at 7:58
  • You should load the slides once, and then use all the times you need. You don't say how are you using the slider (from a shortcode? from template tag?) nor what the add_slider method actually does...
    – gmazzap
    Apr 2, 2014 at 15:33

2 Answers 2

1

setup_theme run on init theme, before after_setup_theme.

The follow order on frontend, was fired in my test:

  1. load_textdomain
  2. plugins_loaded
  3. auth_cookie_valid
  4. set_current_user
  5. sanitize_comment_cookies
  6. setup_theme
  7. after_setup_theme
  8. init
5
  • Thanks in listing the actions trigger list in sequence. I instance the slider using plugins_loaded and the load the slider using after_setup_theme. but still the object is not found. while rendering the slider. (Sliders are loaded after_setup_theme and again intialized to empty). Apr 2, 2014 at 8:02
  • If you want to be sure, than trigger the same hook with different priority, like slider via priority 10 on after_setup_theme and render slider via priority 11 on after_setup_theme.
    – bueltge
    Apr 2, 2014 at 13:06
  • when the admin_enqueue_scripts action is triggerd? Apr 3, 2014 at 9:44
  • Is after_setup_theme is triggerd in ajax call? Apr 3, 2014 at 9:55
  • Maybe, but the right hook is wp_ajax_*
    – bueltge
    Apr 4, 2014 at 9:59
0

Ok. I found an other way by saving the permalink path to the wp_options. And checking whether the current page's url path is in the options list.

1
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Dec 13, 2021 at 20:40

Your Answer

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

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