Improved Shortcode

<?php

/* ~~~~~~~~~~~~ Test Routine ~~~~~~~~~~~~*/

//Default Style
hgm_map();

//Array Style
$test_args = array('width' => 500, 'height' => 300);
hgm_map($test_args);

//Query String Style
hgm_map('width=200&height=100');

//Shortcode = [hgm_map width="600" height="400"]

/* ~~~~~~~~~~~~ Functions ~~~~~~~~~~~~*/

/* Main Function */
function hgm_map($args,$shortcode = false){
if($shortcode) ob_start(); //Halt output if shortcode

//Apply defauilts to argumentns and extract
$defaults = array(
'width' => '600',
'height' => '400'
);
$final_args = wp_parse_args($args,$defaults);
extract($final_args,EXTR_SKIP);
?>

<p>Width Value = <?php echo $width ?></p>
<p>Height Value = <?php echo $height ?></p>

<?php
if($shortcode){
$main_function_output = ob_get_contents();
ob_end_clean();
return $main_function_output;
}
}

/* Shortcode - [hgm_map width="600" height="400"] */
function hgm_shortcode($args){ return hgm_map($args,true); }
add_shortcode('hgm_map','hgm_shortcode');

?>

0 Comments

You must be logged in to post a comment.