En muchos sitios WordPress que administro, necesitaba una forma simple y visible de mostrar promociones, mensajes importantes o anuncios destacados. Algo que no recargue el sitio, que sea fácil de configurar y que se vea bien en cualquier diseño.
Así nació este desarrollo: una barra superior de anuncios animada para WordPress, con modo marquee y slider, totalmente gratuita y pensada para que cualquiera pueda instalarla sin complicaciones.
Este topbar para WordPress te permite mostrar mensajes dinámicos en la parte superior del sitio. Ideal para:
Funciona como un carrusel de mensajes o un ticker de texto animado, según el modo que elijas.
Desde el panel de administración de WordPress podés:
Podés instalarlo como:
Plugin ZIP: subís el archivo desde el panel de WordPress y lo activás. (clic para descargar)
Snippet: copiás el código en un plugin tipo Code Snippets y lo activás desde ahí.
(más abajo te dejo el código)
El código es abierto y editable. Podés modificar estilos, agregar funciones o adaptarlo a tu diseño sin restricciones.
Si necesitás ayuda para ajustarlo, escribime y lo vemos juntos.
/**
* Plugin Name: Tu Web Master - TopBar Slider
* Plugin URI: https://www.tuwebmaster.com.ar
* Description: Muestra un topbar deslizante o marquee con anuncios, mensajes o promociones en todo el sitio o páginas seleccionadas.
* Version: 1.0.0
* Author: Tu Web Master
* Author URI: https://www.tuwebmaster.com.ar
* License: GPLv2 or later
* Text Domain: twm-topbar
*/
if ( ! defined('ABSPATH') ) exit;
/** ---------------------------
* Settings: register options
* --------------------------- */
add_action('admin_menu', 'twm_topbar_add_menu');
function twm_topbar_add_menu() {
add_menu_page(
'TopBar Slider',
'TopBar Slider',
'manage_options',
'topbar-slider',
'twm_topbar_settings_page',
'dashicons-megaphone',
65
);
add_action('admin_init', 'twm_topbar_register_settings');
}
function twm_topbar_register_settings() {
// Slides: newline or JSON array
register_setting('twm_topbar_group', 'twm_topbar_slides', [
'type' => 'string',
'sanitize_callback' => 'twm_topbar_sanitize_slides',
]);
// Display mode (where): all | only | exclude
register_setting('twm_topbar_group', 'twm_topbar_display_mode', [
'type' => 'string',
'default' => 'all',
'sanitize_callback' => function($v){ $v = sanitize_key($v); return in_array($v, ['all','only','exclude'], true) ? $v : 'all'; }
]);
register_setting('twm_topbar_group', 'twm_topbar_pages_only', [
'type' => 'string',
'sanitize_callback' => 'twm_topbar_sanitize_csv_tokens'
]);
register_setting('twm_topbar_group', 'twm_topbar_pages_exclude', [
'type' => 'string',
'sanitize_callback' => 'twm_topbar_sanitize_csv_tokens'
]);
// Behavior mode (how): slides | marquee
register_setting('twm_topbar_group', 'twm_topbar_mode', [
'type' => 'string',
'default' => 'slides',
'sanitize_callback' => function($v){ $v = sanitize_key($v); return in_array($v, ['slides','marquee'], true) ? $v : 'slides'; }
]);
// Slides mode options
register_setting('twm_topbar_group', 'twm_topbar_interval_ms', [
'type' => 'integer',
'default' => 4000,
'sanitize_callback' => function($v){ $v = intval($v); return $v > 200 ? $v : 4000; }
]);
// Marquee mode options
register_setting('twm_topbar_group', 'twm_topbar_speed_px_s', [
'type' => 'integer',
'default' => 80, // pixels per second
'sanitize_callback' => function($v){ $v = intval($v); return $v > 10 ? $v : 80; }
]);
register_setting('twm_topbar_group', 'twm_topbar_gap_px', [
'type' => 'integer',
'default' => 40,
'sanitize_callback' => function($v){ $v = intval($v); return $v >= 0 ? $v : 40; }
]);
// Custom CSS (admins only)
register_setting('twm_topbar_group', 'twm_topbar_custom_css', [
'type' => 'string',
'sanitize_callback' => 'twm_topbar_sanitize_css'
]);
}
function twm_topbar_sanitize_slides($raw) {
$raw = is_string($raw) ? trim(wp_unslash($raw)) : '';
if ($raw === '') return '';
if (str_starts_with($raw, '[')) {
$arr = json_decode($raw, true);
if (is_array($arr)) {
$arr = array_map(function($s){ return trim(wp_strip_all_tags((string)$s)); }, $arr);
$arr = array_filter($arr, fn($s)=> $s !== '');
return implode("\n", $arr);
}
}
$lines = preg_split('/\r\n|\r|\n/', $raw);
$lines = array_map(function($s){ return trim(wp_strip_all_tags((string)$s)); }, $lines);
$lines = array_filter($lines, fn($s)=> $s !== '');
return implode("\n", $lines);
}
function twm_topbar_sanitize_csv_tokens($raw) {
$raw = is_string($raw) ? $raw : '';
$parts = array_map('trim', explode(',', $raw));
$parts = array_filter($parts, fn($t)=> $t !== '');
$clean = array_map(function($t){
if (is_numeric($t)) return (string)intval($t);
$t = strtolower($t);
$t = preg_replace('~[^a-z0-9/_-]+~', '', $t);
return trim($t, '/');
}, $parts);
$clean = array_unique(array_filter($clean, fn($t)=> $t !== ''));
return implode(',', $clean);
}
function twm_topbar_sanitize_css($raw) {
$raw = is_string($raw) ? $raw : '';
return str_replace(["\r\n","\r"], "\n", $raw);
}
/** -----------------------------
* Admin page (simple settings)
* ----------------------------- */
function twm_topbar_settings_page() {
if ( ! current_user_can('manage_options') ) return;
$slides = get_option('twm_topbar_slides', "");
$display_mode = get_option('twm_topbar_display_mode', "all");
$pages_only = get_option('twm_topbar_pages_only', "");
$pages_exclude = get_option('twm_topbar_pages_exclude', "");
$mode = get_option('twm_topbar_mode', 'slides');
$interval_ms = intval(get_option('twm_topbar_interval_ms', 4000));
$speed_px_s = intval(get_option('twm_topbar_speed_px_s', 80));
$gap_px = intval(get_option('twm_topbar_gap_px', 40));
$custom_css = get_option('twm_topbar_custom_css', "");
?>
TopBar Slider — Ajustes
🔹 Conocé nuestra web: Tu Web Master — Diseño, desarrollo y soporte técnico para tiendas online.
🔹 Si necesitás ayuda para adaptar este desarrollo, no dudes en contactarnos.
post_name)) {
$current_tokens[] = strtolower($post->post_name); // by slug
}
}
}
// 2) WooCommerce special pages (shop, cart, checkout, account)
if (function_exists('is_shop') && is_shop()) {
// shop page can be a special archive backed by a page
$shop_id = (int) get_option('woocommerce_shop_page_id');
if ($shop_id) {
$current_tokens[] = (string) $shop_id;
$shop = get_post($shop_id);
if ($shop && !empty($shop->post_name)) {
$current_tokens[] = strtolower($shop->post_name); // ej: "tienda"
}
}
$current_tokens[] = 'shop';
}
if (function_exists('is_cart') && is_cart()) {
$current_tokens[] = 'cart';
$cart_id = (int) get_option('woocommerce_cart_page_id');
if ($cart_id) $current_tokens[] = (string)$cart_id;
}
if (function_exists('is_checkout') && is_checkout()) {
$current_tokens[] = 'checkout';
$chk_id = (int) get_option('woocommerce_checkout_page_id');
if ($chk_id) $current_tokens[] = (string)$chk_id;
}
if (function_exists('is_account_page') && is_account_page()) {
$current_tokens[] = 'my-account';
$acc_id = (int) get_option('woocommerce_myaccount_page_id');
if ($acc_id) $current_tokens[] = (string)$acc_id;
}
// 3) Product category / tag (taxonomy archives)
if (function_exists('is_product_category') && is_product_category()) {
$term = get_queried_object();
if ($term && !empty($term->slug)) $current_tokens[] = strtolower($term->slug);
$current_tokens[] = 'product-category';
}
if (function_exists('is_product_tag') && is_product_tag()) {
$term = get_queried_object();
if ($term && !empty($term->slug)) $current_tokens[] = strtolower($term->slug);
$current_tokens[] = 'product-tag';
}
// 4) Fallback por path (soporta subdirectorios / idiomas)
$path = '';
$url = home_url(add_query_arg([]));
$p = wp_parse_url($url);
if (!empty($p['path'])) {
$path = trim($p['path'], '/');
if ($path !== '') $current_tokens[] = strtolower($path); // full path
// también el último segmento (slug final)
$segments = array_filter(explode('/', $path));
if (!empty($segments)) {
$current_tokens[] = strtolower(end($segments));
}
}
// Normaliza tokens del usuario
$normalize = function($arr){
return array_values(array_unique(array_filter(array_map(function($t){
$t = strtolower(trim($t, " \t\n\r\0\x0B/"));
return $t;
}, $arr))));
};
$only = $normalize($only);
$exclude = $normalize($exclude);
$current = $normalize($current_tokens);
// Comparador: match exacto por ID/slug, o path que termine igual
$matches = function($user_tokens) use ($current, $path) {
foreach ($user_tokens as $t) {
if (in_array($t, $current, true)) return true;
if ($path !== '' && twm_str_ends_with($path, $t)) return true;
}
return false;
};
if ($mode === 'all') return true;
if ($mode === 'only') return $matches($only);
if ($mode === 'exclude') return ! $matches($exclude);
return true;
}
/** -------------------------------
* Frontend: render bar and logic
* ------------------------------- */
add_action('wp_footer', 'twm_topbar_render');
function twm_topbar_render() {
if (is_admin()) return;
if ( ! twm_topbar_should_display() ) return;
$raw = (string) get_option('twm_topbar_slides', '');
$lines = array_filter(preg_split('/\r\n|\r|\n/', $raw), fn($s)=> trim($s) !== '');
$slides = array_map(fn($s)=> trim($s), $lines);
if (empty($slides)) return;
$mode = get_option('twm_topbar_mode', 'slides');
$interval = intval(get_option('twm_topbar_interval_ms', 4000));
if ($interval < 200) $interval = 4000;
$speed_px_s = max(10, intval(get_option('twm_topbar_speed_px_s', 80)));
$gap_px = max(0, intval(get_option('twm_topbar_gap_px', 40)));
$custom_css = (string) get_option('twm_topbar_custom_css', '');
?>
$txt): ?>
No es un producto comercial. Es una solución real a un problema común en sitios WordPress: cómo mostrar mensajes importantes sin depender de plugins pesados ni soluciones complejas.
Usar esta barra de anuncios animada puede ayudarte a:
Mejorar la visibilidad de tus promociones
Aumentar la interacción con tus visitantes
Hacer tu sitio más dinámico y profesional
GitHub / Descarga: TopBar Slider para WordPress
Podés usarlo tal cual o adaptarlo a tu proyecto. Comentarios, sugerencias o mejoras son bienvenidos: la idea es que sea útil para toda la comunidad WordPress.
Si te sirve, genial. Si lo adaptás, mejor. Y si tenés dudas, sugerencias o querés compartir tu experiencia, los comentarios están abiertos. Me interesa que esto sea útil para otros técnicos que trabajen con Odoo y pasarelas de pago.
Soy Sasha Herscovich, y me dedico al diseño, desarrollo y soporte técnico de tiendas online en WordPress y Odoo. Trabajo todos los días resolviendo problemas reales, y cada tanto me gusta compartir lo que me toca enfrentar, por si a alguien más le sirve.
En este blog vas a encontrar ideas, soluciones y fragmentos de código que uso en proyectos reales. Algunas cosas están pensadas para quienes quieren autogestionar su sitio, otras para desarrolladores o diseñadores que buscan mejorar lo que hacen, ahorrar tiempo o simplemente entender cómo resolver algo puntual.
No vendo fórmulas mágicas ni soluciones universales. Solo comparto lo que me funciona, lo que pruebo y lo que aprendo en el camino. Si te sirve, bienvenido. Y si querés comentar, mejorar o adaptar algo, me encantaría que lo hagas.