Halo, nama saya Agung. Berasal dari Denpasar, Bali. Blog ini adalah blog harian saya, dari tips dan trick code, curahan hati, dan beberapa tulisan tidak penting.

Menu & Search

Passing Parameter pada filter & action (WordPress)

February 27, 2013

Sangat sering pada wordpress developer menginginkan untuk melempar parameter ke filter / action. sayang nya tidak terdapat cara gampang yang diberikan oleh wordpress untuk melakukan hal tersebut, bahkan hal tersebut hampir mustahil untuk dilakukan dengan cara biasa.

Tapi sebetulnya ada sebuah cara untuk melakukan passing parameter tersebut. setelah browsing browsing, ternyata melempar parameter ke filter / action dapat dilakukan dengan cara berikut : http://wordpress.stackexchange.com/questions/45901/passing-a-parameter-to-filter-and-action-functions

nah skrng pertanyaan nya, gimana nih kalau misalnya kita mau lempar function dari instance object (bukan hanya nama string function) sebagai parameter filter / class. Saya memodifikasi sedikit dari code tersebut dan juga cara penggunaannya sebagai berikut :

Contoh penggunaan :

class jeg_shortcode {
    public function attach_shortcode($shortcodelist) 
    {
        if ( current_user_can('edit_posts') &&  current_user_can('edit_pages') &&  get_user_option('rich_editing') == 'true')
        {
            $scobj = new Jeg_Param_Storage($shortcodelist, $this);
            add_filter('mce_buttons_3'   , array($scobj, 'register_button'));
            add_filter('mce_external_plugins' , array($scobj, 'add_tinymce_plugin')); 
        }
    }
}

kemudian Jeg Param Storage Obj tersebut dideklarasikan sebagai berikut :

class Jeg_Param_Storage
{
    /**
     * Filled by __construct(). Used by __call().
     *
     * @type mixed Any type you need.
     */
    protected $values;
 
    
    /** 
     * Save passed object instance
     * 
     * @var unknown_type
     */
    protected $instance;
    
    /**
     * Stores the values for later use.
     *
     * @param  mixed $values
     * @return void
     */
    public function __construct( $values, $instance = null )
    {
        $this->values = $values;        
        $this->instance = $instance;
    }

    /**
     * Catches all function calls except __construct().
     *
     * Be aware: Even if the function is called with just one string as an
     * argument it will be sent as an array.
     *
     * @param  string $callback Function name
     * @param  array  $arguments
     * @return mixed
     */
    public function __call( $callback, $arguments )
    {
     if($this->instance !== null) 
     {
      if(is_callable( array($this->instance, $callback))) {
       return call_user_func( array($this->instance, $callback) , $arguments, $this->values );
      }
  } else 
  {
   if(is_callable( $callback )) {
    return call_user_func( $callback, $arguments, $this->values );
   }
     }
     
        // Wrong function called.
        throw new InvalidArgumentException(
            sprintf(
                'File: %1$s

                Line %2$d

                There is no function %3$s',
                __FILE__,
                __LINE__,
                $name
            )
        );
    }
}

 

Related article

Sekolah Anak

Beberapa hari lalu, istri bertanya kepada saya, kemana anak kami…

Istri, IRT atau Kerja atau Usaha?

Pernah ga cowo – cowo yang akan nikah berpikir, istri…

Apple Vision Pro dan Facebook

Beberapa bulan lalu, apple merelease sebuah product disebut Apple Vision…

Discussion about this post

Leave a Reply

Your email address will not be published. Required fields are marked *

Type your search keyword, and press enter to search