web-dev-qa-db-fra.com

Custom Shortcodes en générant une erreur sur le site de développement

J'ai un site existant qui utilise des codes courts personnalisés. Quand j'ai essayé de les transférer sur un nouveau thème et site de développement, cela me donne une erreur. Le shortcode concerne le contenu tabulé. Vous pouvez voir l'erreur ici: https://dev.brothermartin.com/extracurriculars/academic-games/ et voici:

Remarque: Variable non définie: tab3_button dans /home/brothermartin18/public_html/blog/wp-content/themes/Brother_Martin_2018/lib/admin/shortcodes/shortcodes.php à la ligne 60

Remarque: Variable non définie: tab4_button dans /home/brothermartin18/public_html/blog/wp-content/themes/Brother_Martin_2018/lib/admin/shortcodes/shortcodes.php à la ligne 60

Remarque: Variable non définie: tab5_button dans /home/brothermartin18/public_html/blog/wp-content/themes/Brother_Martin_2018/lib/admin/shortcodes/shortcodes.php à la ligne 60

C'est le code lié au shortcode:

function mbiz_tabbed_box( $atts, $content = null ) {
 extract( shortcode_atts( array(
  'tab1' => '',
  'tab2' => '',
  'tab3' => '',
  'tab4' => '',
  'tab5' => '',
  ), $atts ) );

if ($tab1) $tab1_button = '<li><a href="#" title="tab1">' . esc_attr($tab1) . '</a></li>';
if ($tab2) $tab2_button = '<li><a href="#" title="tab2">' . esc_attr($tab2) . '</a></li>';
if ($tab3) $tab3_button = '<li><a href="#" title="tab3">' . esc_attr($tab3) . '</a></li>';
if ($tab4) $tab4_button = '<li><a href="#" title="tab4">' . esc_attr($tab4) . '</a></li>';
if ($tab5) $tab5_button = '<li><a href="#" title="tab5">' . esc_attr($tab5) . '</a></li>';

return '<div class="tabs-style1"><div class="tabs-navigation"><ul class="tabs-nav">'
        . $tab1_button . $tab2_button . $tab3_button . $tab4_button . $tab5_button .
        '</ul></div><div class="tabs-content">' . do_shortcode($content) . '</div></div>';
}
add_shortcode('tabbed_box', 'mbiz_tabbed_box');

Quelqu'un at-il des solutions simples pour cela? Cela ne se produit que lorsque 5 onglets ne sont pas utilisés. Je ne les ai pas créés, donc j'ai du mal à résoudre les problèmes.

2
Christi Y.

Initialisez les variables en premier. Voir le code ci-dessous:

function mbiz_tabbed_box( $atts, $content = null ) {
 extract( shortcode_atts( array(
  'tab1' => '',
  'tab2' => '',
  'tab3' => '',
  'tab4' => '',
  'tab5' => '',
  ), $atts ) );

$tab1_button = $tab2_button = $tab3_button = $tab4_button = $tab5_button = '';

if ($tab1) $tab1_button = '<li><a href="#" title="tab1">' . esc_attr($tab1) . '</a></li>';
if ($tab2) $tab2_button = '<li><a href="#" title="tab2">' . esc_attr($tab2) . '</a></li>';
if ($tab3) $tab3_button = '<li><a href="#" title="tab3">' . esc_attr($tab3) . '</a></li>';
if ($tab4) $tab4_button = '<li><a href="#" title="tab4">' . esc_attr($tab4) . '</a></li>';
if ($tab5) $tab5_button = '<li><a href="#" title="tab5">' . esc_attr($tab5) . '</a></li>';

return '<div class="tabs-style1"><div class="tabs-navigation"><ul class="tabs-nav">'
        . $tab1_button . $tab2_button . $tab3_button . $tab4_button . $tab5_button .
        '</ul></div><div class="tabs-content">' . do_shortcode($content) . '</div></div>';
}
add_shortcode('tabbed_box', 'mbiz_tabbed_box');

Une autre solution serait de construire une chaîne puis de la renvoyer, sans utiliser de variables séparées.

$out = '<div class="tabs-style1"><div class="tabs-navigation"><ul class="tabs-nav">';
    if ($tab1) $out .= '<li>whatever</li>';
    if ($tab2) $out .= '<li>whatever</li>';
    if ($tab3) $out .= '<li>whatever</li>';
    if ($tab4) $out .= '<li>whatever</li>';
    if ($tab5) $out .= '<li>whatever</li>';
$out .= '</ul></div><div class="tabs-content">' . do_shortcode($content) . '</div></div>';

return $out;
1
Ciprian