WPUtils: utility per integrare e usare jQuery in Wordpress

WPUtils: utility per integrare e usare jQuery in Wordpress

WPUtils è una mia piccola creazione in jQuery che, come dice il suo nome, vuole essere un aiuto per chi vuole integrare e usare jQuery in Wordpress. Dispone di alcune utili funzioni helper che permettono di identificare la sezione corrente di Wordpress e collegarvi il codice jQuery corrispondente. Attualmente è ancora in fase di sviluppo e questa versione 1.0 non sarà inclusa in Bitbucket prima di aver raggiunto una certa consistenza. Vediamola insieme.

WPUtils è un oggetto inserito direttamente all'interno del namespace di jQuery:


/** WPUtils: A generic jQuery utility for Wordpress
 *
 *  @version 1.0
 *  @author Gabriele Romanato <gabriele.romanato@gmail.com>
 *  @requires jQuery 1.5+
 *  @license This work is public domain
 */

(function($) {

	$.WPUtils = {
	
	    /** Check if a document is the home page
	     *  @return Boolean
	     */
	
		isHome: function() {
		
			if(!$('body').hasClass('home')) {
			
				return false;
			
			}
			
			return true;
		
		},
		
		 /** Check if a document is a Wordpress page
	      *  @return Boolean
	      */
	
		isPage: function() {
		
			if(!$('body').hasClass('page')) {
			
				return false;
			
			}
			
			return true;
		
		},
		
		 /** Check if a document is a single post page
	      *  @return Boolean
	      */
		
		isSingle: function() {
		
			if(!$('body').hasClass('single')) {
			
				return false;
			
			}
			
			return true;
		
		
		},
		
		 /** Check if a document is an archive page
	      *  @return Boolean
	      */
		
		isArchive: function() {
		
			if(!$('body').hasClass('archive')) {
			
				return false;
			
			}
			
			return true;
		
		
		},
		
		 /** Check if a document is a tag page	     
		  *  @return Boolean
	      */
		
		isTag: function() {
		
			if(!$('body').hasClass('tag')) {
			
				return false;
			
			}
			
			return true;
		
		
		},
		
		/** Binds a callback function to the ready() event
		 *  of the document object
		 *  @return void
		 */
				
		load: function(callback) {
		
			$(document).bind('ready', callback);
		
		
		}
	
	
	};



})(jQuery);

Le sue funzioni helper non fanno altro che verificare la presenza di una data classe CSS nell'elemento body per identificare la sezione corrente di Wordpress. Il metodo load(), infine, carica il nostro codice quando il DOM è pronto. Un esempio:


$.WPUtils.load(function() {

	alert($.WPUtils.isHome()); // true

});

Aspetto vostri consigli per migliorare questa utility ed ampliarla ulteriormente.

Demo versione 1.0

esempio non disponibile

Download

  1. esempio non disponibile (1 Kb)
  2. esempio non disponibile (500 B)
Torna su