WordPress Shortcodes

Permalink Dynamic

Blog URL


Basic Usage

[permalink id=49 text=“My Posts Dynamic Permalink”]


function do_permalink($atts)
{ extract(shortcode_atts(array ( ‘id’ => 1, ‘text’ => ‘’ // default value if none supplied ), $atts));

if($text) { $url = get_permalink($id); return ‘‘.$text.’‘; } else return get_permalink($id);
}
add_shortcode(‘permalink’, ‘do_permalink’);

Conditional HTTP GET Vars


Canada

[get key=‘title’ if_return=‘If title isn’t defined’ else_return=‘The value of title is {$key}’]

/** * Allow content to access GET variables * * param $atts Shortcode attributes. * return Processed */
function ayzHttpGetVars($atts)
{ $operators = array(‘==’, ‘!=’, ‘>’, ‘>=’, ‘<’, ‘<=’); $result = ‘’; if(array_key_exists(‘key’, $atts)) { $key = $atts[‘key’]; $keyValue = !empty($_GET[$key]) ? $_GET[$key] : ‘’;

$if_key = !empty($atts[‘if_key’]) ? $atts[‘if_key’] : ‘’;

$if_return = !empty($atts[‘if_return’]) ? str_replace(‘{$key}’, $keyValue, $atts[‘if_return’]) : ‘’; $else_return = !empty($atts[‘else_return’]) ? str_replace(‘{$key}’, $keyValue, $atts[‘else_return’]) : ‘’;

// Return Values Sanity Check // $test = $if_return.$else_return; if(empty($atts)) throw new Exception(‘Shortcode [get] requires that at least one of if_return=“value” or else_return=“value” be set when if_key is defined.’);

// Are we testing if the GET var is empty or not set? // if(empty($if_key)) $result = empty($keyValue) ? $if_return : $else_return; else { // Explode the if_key value to test for operators // $ifValues = explode(’ ‘, $if_key); if(count($ifValues) > 1 && in_array($ifValues0, $operators)) { $operator = array_shift($ifValues); $ifValues = implode(’ ‘, $ifValues); $result = eval(“$key $operator ‘$ifValues’”) ? $if_return : $else_return; } else $result = $keyValue == $if_key ? $if_return : $else_return; } } else { if(array_key_exists(‘key_callback’, $atts) && function_exists($atts[‘key_callback’])) $callback = $atts[‘key_callback’];

foreach($atts as $key) { if(isset($callback)) $result .= call_user_func($callback, $key, $_GET[$key]); else if(!empty($_GET[$key]) && $key != ‘key_callback’) $result .= $key.’=’.$_GET[$key].’&’; } }

return $result;
}
add_shortcode(‘get’, ‘ayzHttpGetVars’);

RAW Input


// Allow for RAW input into posts //
function raw_formatter($content)
{ $content = do_shortcode($content); $new_content = ‘’; $pattern_full = ‘{(\[raw\].*?\[/raw\])}is’; $pattern_contents = ‘{\[raw\](.*?)\[/raw\]}is’; $pieces = preg_split($pattern_full, $content, -1, PREG_SPLIT_DELIM_CAPTURE);

foreach ($pieces as $piece) { if (preg_match($pattern_contents, $piece, $matches)) $new_content .= $matches1; else $new_content .= wptexturize(wpautop($piece)); }

return $new_content;
}
remove_filter(‘the_content’, ‘wpautop’);
remove_filter(‘the_content’, ‘wptexturize’);
add_filter(‘the_content’, ‘raw_formatter’, 99);