Last active
August 13, 2019 03:59
-
-
Save parzibyte/bc9b130fe1bf22f602b681743a86af32 to your computer and use it in GitHub Desktop.
Ejemplo de un plugin para WordPress - https://parzibyte.me/blog/2019/08/12/creacion-plugin-wordpress/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: Insertar publicidad entre párrafos | |
Plugin URI: https://parzibyte.me/blog | |
Description: Insertar publicidad entre párrafos | |
Version: 1.0 | |
Author: Luis Cabrera Benito a.k.a Parzibyte | |
Author URI: https://parzibyte.me/blog | |
*/ | |
defined("ABSPATH") or die("No es permitido el acceso directo al archivo"); | |
if (!class_exists("ParzibytePublicidad")) { | |
class ParzibytePublicidad | |
{ | |
public static function iniciar() | |
{ | |
add_filter("the_content", "ParzibytePublicidad::agregar_publicidad"); | |
} | |
public function agregar_publicidad($contenido) | |
{ | |
$expresionRegular = '/<p(>|\s+[^>]*>)(.*?)<\/p>/im'; | |
$publicidad = "<div style='border: 1px solid #8bc34a;font-size:0.7em'> | |
<strong>¿Quieres un plugin para WordPress?</strong> | |
<p>Visita mi <a href='//parzibyte.me/blog'>página web</a> para hablar ;)</p> | |
</div>"; | |
$expresionRegular = '/<p(>|\s+[^>]*>)(.*?)<\/p>/im'; | |
if (is_single() && in_the_loop() && is_main_query()) { | |
$conteo = 0; | |
$ponerPublicidadCada = 2; | |
return preg_replace_callback($expresionRegular, function ($coincidencias) use (&$publicidad, &$conteo, $ponerPublicidadCada) { | |
$conteo++; | |
if ($conteo % $ponerPublicidadCada === 0) { | |
return "<p" . $coincidencias[1] . $coincidencias[2] . "</p>" . $publicidad; | |
} | |
return "<p" . $coincidencias[1] . $coincidencias[2] . "</p>"; | |
}, $contenido); | |
} | |
return $contenido; | |
} | |
} | |
ParzibytePublicidad::iniciar(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment