Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Defaults of a Plugin

I understand that you can create a plugin with public defaults like this:

(function($){
$.fn.hilight = function(options) {
   var opts = $.extend({}, $.fn.hilight.defaults, options);
   this.css(opts);
};
$.fn.hilight.defaults = {
   foreground: 'red'
};
})(jQuery);

And then I can change the defaults from the outside by:

$.fn.hilight.defaults.foreground='blue';

My question(s) is how can this be done with the $.fn.extend() syntax:

(function($){
   $.fn.extend({
     hililght: function(options){
       var defaults={foreground: 'red',}
       var opt=$.extend(defaults,options);
      }
   });
})(jQuery)

And how can I change multiple defaults?

like image 494
Theopile Avatar asked Jan 12 '11 00:01

Theopile


People also ask

Can plugin be modified?

Fortunately, it's possible to alter existing plugins instead. When done properly, you can extend a plugin's functionality even further, and improve how it works on your website.

What are the default plugins installed in the WordPress?

Plugins included with every installation on our systems: Jetpack – Jetpack is a WordPress plugin that supercharges your self-hosted WordPress site with the awesome cloud power of WordPress.com. object-cache. php – This drop-in plugin extends WordPress' Object Cache via Memcached. advanced-cache.

How do I change my default email in WordPress?

To change the WordPress website email address, go to Settings » General and change the 'Email Address' option. Don't forget to save your changes. WordPress will now save your new admin email address.

Can you have too many plugins?

Poor Speed and PerformanceInstalling too many plugins on a website can slow down its response time and hinder overall performance. In addition to a plugin's quality, plugin incompatibility issues may also slow down the website.


1 Answers

You could do this:

(function($){
   $.fn.extend({
     hilight: function(options){
       var opt=$.extend(defaults,options);
     }
   });
   $.extend($.fn.hilight, {defaults: {
        foreground: 'red'
   }});
})(jQuery)

But there isn't much point. $.fn.extend is only useful when you have multiple properties to extend, but you only have hilight. Stick to your first example, it's more readable and (slightly) more efficient.

jQuery's extend functionality comes in handy for your second question, about changing multiple defaults. This is what extend was really designed for:

$.extend($.fn.hilight.defaults, {
    foreground: "red",
    background: "yellow",
    height: 100
    // and so on...
});
like image 195
David Tang Avatar answered Nov 15 '22 03:11

David Tang