Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Last active August 13, 2019 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/bc9b130fe1bf22f602b681743a86af32 to your computer and use it in GitHub Desktop.
Save parzibyte/bc9b130fe1bf22f602b681743a86af32 to your computer and use it in GitHub Desktop.
<?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