Is there a more elagant way to check shortcode atts?
-
Hi there,
My aim is to build shortcodes with optional attributes. The following code works only for 2 attributes and isnt really good in performance, because I have to check if the single attributes exists and if both exists – thats messy.
My first idea was to count the atts array, but without an attribute and with one atrribute the count is 1. Any other Ideas?
<?php function shortcode( $atts, $content = null ) { // temp output variable $output = '<div class="class01">' . do_shortcode($content) . '</div>'; // check att1 exists if (isset($atts['att1'])) { $att1 = $atts['att1']; $output = '<div class="class1 '.$att1.'">' . do_shortcode($content) . '</div>'; // check att2 exists } if (isset($atts['att2'])) { $att2 = $atts['att2']; $output = '<div class="class1 '.$att2.'">' . do_shortcode($content) . '</div>'; // check att1 and att2 exists } if ( (isset($atts['att2'])) && (isset($atts['att1'])) ) { $att2 = $atts['att1']; $att2 = $atts['att2']; $output = '<div class="class1 '.$att1.' '. $att2.'">' . do_shortcode($content) . '</div>'; } return $output; } ?>
thanks in advance
christian
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘Is there a more elagant way to check shortcode atts?’ is closed to new replies.