Function as Shortcode

Function as Shortcode Technique

Run functions in "Shortcode Mode" - If functions used for shortcode are output to the browser they will display above whatever content you place in your pages or posts. Not good. This method eliminates the need to write "mirrored" funcitons of your main ones just for shortcode.

//Main plugin function
function main_function($shortcode = false){
if($shortcode) ob_start(); //If shortcode halt browser output

$testvar = 'This is an output test on: ' . $_SERVER['HTTP_HOST'];

<div class="test">
<p><?php echo $testvar; ?></p>

//If shortcode store output in variable and return
$main_function_output = ob_get_contents();
return $main_function_output;

//Run Shortcode - [my_shortcode]
function run_my_shortcode($atts){
'arg1' => 'Not Set',
'arg2' => 'Not Set'
), $atts));

return main_function(true);


Delete Post Link

<a href="<?php echo wp_nonce_url(get_bloginfo('url') . "/wp-admin/post.php?action=delete&amp;post=" . get_the_ID() , 'delete-post_' . get_the_ID())?>">X</a>

Plugin Constants


//Set Constants
function my_plugin_set_constants(){
define('MYPLUGIN_INCLUDES',str_replace('my-plugin.php','',__FILE__) . 'includes/');
define('MYPLUGIN_WP_ADMIN',get_bloginfo('wpurl') . '/wp-admin/');
define('MYPLUGIN_URL',WP_PLUGIN_URL . '/my-plugin/');


Thickbox path fix for enqueue

function wpvp_enqueue_js(){
<script type="text/javascript">
var tb_pathToImage = '<?php echo bloginfo('wpurl') ?>/wp-includes/js/thickbox/loadingAnimation.gif';
var tb_closeImage = '<?php echo bloginfo('wpurl') ?>/wp-includes/js/thickbox/tb-close.png';

add_action('wp_print_scripts', 'wpvp_enqueue_js');


Change url to html link


$str = 'My post with links in it';
preg_replace('!((?:www|http://)[^r ]+)!', '<a href="1">1</a>', $str);


//Here is another method
function addhttp($url) {     if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {         $url = "http://" . $url;     }     return $url; }

Style Text Input Field

//The code below acts upon a variable named titleInput which is an instance of a text input component, which you can find in the flash ide library.

var mainTextFormat:TextFormat = new TextFormat();
mainTextFormat.font = 'Arial';

titleInput.setStyle("textFormat", mainTextFormat);

Enqueue Thickbox


//Enqueue thickbox - for plugin file

//Thickbox example - for display
<a href="images/single.jpg" title="add a caption to title attribute / or leave blank" class="thickbox"><img src="images/single_t.jpg" alt="Single Image"/></a>