<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Johan de Bruin &#187; Programacion wordpress</title>
	<atom:link href="http://www.johandebruin.com/t/programacion-wordpress/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.johandebruin.com</link>
	<description>Programación en php, tutorial de api wordpress y posicionamiento en buscadores seo.</description>
	<lastBuildDate>Sun, 05 Feb 2012 15:18:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Estructura de la clase php de un plugin de wordpress</title>
		<link>http://www.johandebruin.com/estructura-de-la-clase-php-de-un-plugin-de-wordpress/</link>
		<comments>http://www.johandebruin.com/estructura-de-la-clase-php-de-un-plugin-de-wordpress/#comments</comments>
		<pubDate>Sun, 14 Mar 2010 15:31:58 +0000</pubDate>
		<dc:creator>Johan</dc:creator>
				<category><![CDATA[Wordpress]]></category>
		<category><![CDATA[clase php]]></category>
		<category><![CDATA[Clase plugin]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[Plugin wordpress]]></category>
		<category><![CDATA[Programacion wordpress]]></category>

		<guid isPermaLink="false">http://www.johandebruin.com/?p=450</guid>
		<description><![CDATA[Los buenos plugins de wordpress están desarrollados a través de una clase php con la que se optimiza el código. Aquí traigo un ejemplo de la estructura básica de la programación php para declarar la clase e insertar el constructor y un método que se autoejecutará al iniciar la clase que instalará el plugin. &#60;?php [...]]]></description>
			<content:encoded><![CDATA[<p>Los buenos plugins de wordpress están desarrollados a través de una clase php con la que se optimiza el código. Aquí traigo un ejemplo de la estructura básica de la programación php para <strong>declarar la clase </strong>e insertar el constructor y un <strong>método </strong>que se autoejecutará al iniciar la clase <strong>que instalará el plugin</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/*
Plugin name: Nombre de mi plugin
Version: 0.1
Plugin URI: http://www.johandebuin.com/miplugin
Author: Johan de Bruin
Author URI: http://www.johandebruin.com
Description: una descripción sobre que hace el plugin en cuestión
*/</span></pre></div></div>

<p><span id="more-450"></span><br />
Ahora declaramos un if para comprobar si la clase ya existe, en caso contrario la definimos.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">class_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'clasePlugin'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span>
<span style="color: #000000; font-weight: bold;">class</span> clasePlugin
<span style="color: #009900;">&#123;</span>
     <span style="color: #666666; font-style: italic;">//Esta variable sirve para obtener la url del plugin</span>
     <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$plugin_url</span><span style="color: #339933;">;</span>
&nbsp;
     <span style="color: #666666; font-style: italic;">//Constructor del plugin</span>
     <span style="color: #000000; font-weight: bold;">function</span> clasePlugin<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#123;</span>
          <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">plugin_url</span> <span style="color: #339933;">=</span> trailingslashit<span style="color: #009900;">&#40;</span> WP_PLUGIN_URL<span style="color: #339933;">.</span><span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span> plugin_basename<span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #666666; font-style: italic;">//Línea para incluir los scripts a la pagina</span>
          add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_print_scripts'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'misScripts'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">function</span> instalar<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#123;</span>
          <span style="color: #666666; font-style: italic;">//Sentencias como la creación de una BD</span>
     <span style="color: #009900;">&#125;</span>
&nbsp;
     <span style="color: #000000; font-weight: bold;">function</span> misScripts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
     <span style="color: #009900;">&#123;</span>
          <span style="color: #666666; font-style: italic;">//Añadimos un script en jquery</span>
          wp_enqueue_script<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'jquery'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          wp_enqueue_script<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'clasePlugin'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">plugin_url</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/rutaDeljquery.js'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'jquery'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
          <span style="color: #666666; font-style: italic;">//Añadimos un css</span>
          <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;link rel=&quot;stylesheet&quot; href=&quot;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">plugin_url</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/estilo.css&quot; type=&quot;text/css&quot; /&gt;'</span><span style="color: #339933;">;</span>
     <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">else</span> <span style="color: #339933;">:</span>
     <span style="color: #990000;">exit</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;La clase clasePlugin ya esta declarada&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">endif</span><span style="color: #339933;">;</span></pre></div></div>

<p>Por último creamos la instancia de la clase, emplearemos la función de wordpress <strong>register_activation_hook</strong> cuyos parametros son fichero del <em>plugin </em>y <em>callback</em>, para especificar que queremos que se ejecute la función <strong>instalar </strong>al activar el plugin.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$instanciaPlugin</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> clasePlugin<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$instanciaPlugin</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
     <span style="color: #666666; font-style: italic;">//Recuerda que la &amp; sirve para que los cambios realizados en la variable en la función</span>
     <span style="color: #666666; font-style: italic;">//surtan efecto a nivel global y no solo dentro de la variable.</span>
     register_activation_hook<span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">__FILE__</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$instanciaPlugin</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'instalar'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Y recuerda, el codigo es poesia <img src='http://www.johandebruin.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> .</p>
]]></content:encoded>
			<wfw:commentRss>http://www.johandebruin.com/estructura-de-la-clase-php-de-un-plugin-de-wordpress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

