Created
March 26, 2025 16:12
-
-
Save dantetesta/4fed4c70f38660851cc36e3aeb7b9954 to your computer and use it in GitHub Desktop.
Repeater de Options Pages - Cria um Botão com Label icone e link
This file contains hidden or 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 | |
//SERVE APENAS PARA LISTAR DADOS DE UM REPEATER PARA MOSTRAR UM BOTÃO COM - LABEL, LINK, ICONE | |
// SHORTCODE: [btn_rep_unico index="todos" icon_position="right" bg_color="#6e2c2c" hover_color="#ccc" text_color="#ffffff"] | |
// OU NO INDEX PASSE O INDEX DO REPEATER - [btn_rep_unico index="0" icon_position="right" bg_color="#6e2c2c" hover_color="#ccc" text_color="#ffffff"] | |
function shortcode_botao_rep_unico($atts) { | |
// 🔧 CONFIGURAÇÕES DO SEU OPTIONS PAGES | |
$options_slug = 'ajustes-do-site'; | |
$repeater_slug = '_rep_botoes'; | |
$field_label = '_label'; | |
$field_link = '_link'; | |
$field_icon = '_icone'; | |
$style_prefix = 'btn-style-'; | |
// 🎛️ ATRIBUTOS DO SHORTCODE | |
$atts = shortcode_atts(array( | |
'index' => 0, | |
'icon_position' => 'left', | |
'bg_color' => '#6e2c2c', | |
'hover_color' => '#4e1f1f', | |
'text_color' => '#ffffff', | |
), $atts); | |
$options = get_option($options_slug); | |
if (!isset($options[$repeater_slug]) || !is_array($options[$repeater_slug])) { | |
return '<p>Repeater <code>' . esc_html($repeater_slug) . '</code> não encontrado em <code>' . esc_html($options_slug) . '</code>.</p>'; | |
} | |
$repeater = $options[$repeater_slug]; | |
$output = ''; | |
// Se for para exibir todos | |
if ($atts['index'] === 'todos') { | |
foreach ($repeater as $item) { | |
$output .= gerar_botao_html($item, $atts, $field_label, $field_link, $field_icon, $style_prefix); | |
} | |
return $output; | |
} | |
// Exibir apenas um item | |
$item_key = 'item-' . strval($atts['index']); | |
if (!isset($repeater[$item_key])) { | |
return '<p>Botão não configurado no índice informado.</p>'; | |
} | |
$botao = $repeater[$item_key]; | |
return gerar_botao_html($botao, $atts, $field_label, $field_link, $field_icon, $style_prefix); | |
} | |
add_shortcode('btn_rep_unico', 'shortcode_botao_rep_unico'); | |
// 🧩 Função que gera o HTML do botão | |
function gerar_botao_html($botao, $atts, $field_label, $field_link, $field_icon, $style_prefix) { | |
$titulo = isset($botao[$field_label]) ? $botao[$field_label] : 'Clique aqui'; | |
$link = isset($botao[$field_link]) ? $botao[$field_link] : '#'; | |
$icone = isset($botao[$field_icon]) ? $botao[$field_icon] : ''; | |
$style_id = $style_prefix . uniqid(); | |
ob_start(); | |
?> | |
<style> | |
.<?php echo $style_id; ?> { | |
display: inline-flex; | |
align-items: center; | |
margin: 5px; | |
padding: 10px 16px; | |
background-color: <?php echo esc_attr($atts['bg_color']); ?>; | |
color: <?php echo esc_attr($atts['text_color']); ?>; | |
text-decoration: none; | |
border-radius: 6px; | |
transition: background-color 0.3s ease; | |
font-weight: 500; | |
} | |
.<?php echo $style_id; ?>:hover { | |
background-color: <?php echo esc_attr($atts['hover_color']); ?>; | |
} | |
.<?php echo $style_id; ?> i { | |
font-size: 1rem; | |
} | |
.<?php echo $style_id; ?> .icon-left { | |
margin-right: 8px; | |
} | |
.<?php echo $style_id; ?> .icon-right { | |
margin-left: 8px; | |
} | |
</style> | |
<a href="<?php echo esc_url($link); ?>" class="<?php echo esc_attr($style_id); ?>"> | |
<?php if ($icone && $atts['icon_position'] === 'left'): ?> | |
<i class="<?php echo esc_attr($icone); ?> icon-left"></i> | |
<?php endif; ?> | |
<?php echo esc_html($titulo); ?> | |
<?php if ($icone && $atts['icon_position'] === 'right'): ?> | |
<i class="<?php echo esc_attr($icone); ?> icon-right"></i> | |
<?php endif; ?> | |
</a> | |
<?php | |
return ob_get_clean(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment