• I am having a widget which can be multiple times, and each time the CSS for it differs. I have accomplished it by using a static variable inside the widget class and naming the required HTML elements using that variable. Also I have created a custom PHP which creates the CSS and it is enqueued into functions.php. My problem is that I want to know how many of that widget was instantiated. I have tried the following methods:

    1. calling the static variable by the class. But it shows 0.
    2. making a global variable in functions.php, enqueuing it into the init hook. Using that global variable in my widgets and then seeing the value of the variable in my css.php. Still it returns 0.
    3. I am at a loss!
      Here is a bi of code for the second point!

      functions.php

      add_action( 'init', 'trial_declare_globals', 1 );
      
      function trial_declare_globals() {
      	$GLOBALS['tr_widgets_no'] = 0;
      }
      add_action( 'wp_enqueue_scripts', 'trial_enqueue_styles' );
      
      function trial_enqueue_styles() {
        wp_enqueue_style( 'trial-custom-style', get_stylesheet_directory_uri() . '/trial-custom-css.php' );
      }

      widgets.php

      class trial_widget extends WP_Widget {
      
        public static $tr_widgets_no = 0;
      
        public function get_tr_widgets_no() {
      
          return self::$tr_widgets_no;
        }
      
      	public function __construct() {
      
      		parent::__construct(
      			'yo-widget',
      			__( 'YO' )
      		);
      
      	}
      
          function widget($args, $instance) {
      
            	extract($args);
      
            	self::$tr_widgets_no++;
      
            	$GLOBALS['tr_widgets_no'] = self::$tr_widgets_no;

      and so on…
      `

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Number of Widgets Instantiated’ is closed to new replies.