In WordPress, you can do a lot with shortcodes, the problem is, some work fine then one day they just break.  It’s best to stick with ones that work reliably, even when you upgrade.  I’m showing parts of a function I wrote for a custom shortcode that should get you started in looking at ways to expand the flexibility of WordPress without bogging your site down with needless plug-ins.  Some plug-ins are great but they include too much and inevitably, they conflict with each other.

I always build shortcodes and encapsulate them in objects to avoid clashing with the system and plug-ins, however, sometimes you need to write a shortcode to do something simple.  I find it ironic that WordPress does not have built in more useful and common functions as shortcodes.  I mean, embedding a page or excerpt of a list of category posts isn’t so common it should be built in?!


//ini_set("display_errors",true); error_reporting(E_ALL);

// Prevent recursion
global $outerPost;
if ($outerPost)
return '';

// Parse parameters
extract(shortcode_atts(array('query' => 'post_type=post','show'=>'','show_excerpt'=>'','show_title'=>'','first_img'=>'N','cols'=>'1','no_msg'=>'N'), $atts));

$query = html_entity_decode($query);  // The flexibility to add any query that wp_query() will accept -- this is the real power and flexibility behind this shortcode
$show_title= html_entity_decode($show_title);  // This will make sure ONLY the title gets shown with the permalink
$show=html_entity_decode($show);  // Show excerpt,title, first_img, or "content"?

// You will need to call on another function that will find the first image of a function and return that image -- not included here
$first_img=html_entity_decode(strtoupper($first_img));

// Allow multiple columns in results
$cols=html_entity_decode(strtoupper($cols));
if ($cols>1) {$class="cols$cols-1col";} else {$class='';}

$no_msg=html_entity_decode($no_msg);  // If empty, what should it say?

// This only includes an extra class that extracts the first image of a post if it is specified in the shortcode by the user to do so
if ($first_img=="Y") {include($_SERVER['DOCUMENT_ROOT'].'/library/functions/image.php');}

// Create new post loop
// Example use: [list_custom query="author_name=fred"]
// More sample query vars: &category_name=news-articles&offset=3
// &posts_per_page=3

global $post;
$outerPost = $post;
$my_query = new WP_Query($query);

$cnt=0;
while ($my_query->have_posts()) {

$cnt++;

$my_query->the_post(); setup_postdata($my_query->post);

        if ($show_title!="hide" && $show!="title"){

$the_title=get_the_title(); echo ''$the_title'';

}

//  You will need to use the "catch_that_image()" function -- Google it if ($first_img=="Y"){ echo $wpImage->catch_that_image();}

if ($show=='excerpt' || $show_excerpt=="yes"){

$content=get_the_excerpt();

}

if ($show=="title"){

$content=get_the_title();

echo "<a href='".get_permalink()."'>$content."</a>"; continue;

}

if ($show==""){

$content=get_the_content();

}

if ($content=='') {continue;} if (stristr($content,'id="more-')){ $content=substr($content,0,stripos($content,'<span id="more-'));

// See if need to close a tag first: $cnt_p=substr_count($content,"<p"); $cnt_p_close=substr_count($content,"");

$num_closers=$cnt_p-$cnt_p_close;

$content.=custom_read_more_link();

for($i=0;$i<$num_closers;$i++){ $content.=''</p>"; }

}

if($cols>1) { if ($cnt%$cols==0) {echo ''<div class='cols$cols-1col'>$content</div>"} } else {

echo str_ireplace("\r\n",'',$content);

}

} if($no_msg=="Y" && $cnt<1){

echo "Sorry, no articles or content were found matching that criteria.";

} $post = $outerPost; $outerPost = null; setup_postdata($post);

Performance of your website, especially when using a CMS or framework, may often tax your server to the point it bogs down or throws fatal “out of memory” errors. It’s often hard to troubleshoot these issues because sometimes the server runs out of memory before it can even report the error, or the error it throws is in a log file that’s not readily accessible. Depending on your type of host, this can waste a lot of your time just hunting down the error, much less actually trying to solve the reason it is erroring out in the first place.

Before you try anything else, increase the WordPress PHP memory allocation, you probably shouldn’t go past ‘256M’ (that’s megabytes), but you may also want to check with your host. Caching and improving the speed of your website will not only improve your SEO rankings, it will also help you retain more traffic. Let’s face it, people are looking for information and they don’t want to wait, staring at that spinning beach ball or Window’s “blue ring of futility” — I’m sure you can relate. We all have better things to do than sit at red lights, right?!


Turning on the built-in WordPress cache can speed things up. I’d suggest loading something more robust, like “WP Super Cache” but some shared hosts will not allow it.

The WP_CACHE setting included in the wp-content/advanced-cache.php script, when executing wp-settings.php:



Adding this to your template file or functions.php in some cases will allow your PHP scripts to access variables passed in the URL query strings.  You know, that ugly text after the Question mark in the website address:  “http://tools.belchamber.us?variable=value&variable2=value”.  Most of you probably already figured out just looking at these type of URLs that some values are being passed from one page to another.  The variables extracted this way are called “Get variables” and aren’t very secure.

Remember to avoid passing any sensitive or personal data inside a URL — anyone can view it on the address bar, or worse yet – in your browser history!


function add_query_vars_filter( $vars ){

$vars= array("var1","var2","var3");

return $vars;

}

add_filter( 'query_vars', 'add_query_vars_filter' );


RSS feeds are handy, but let’s face it, images do say so much more than words.  They stand out and grab attention, they help people identify blocks of information quicker.  They really are worth 1,000 words if you think about it.  Sometimes, a lot more….

Add this to your template or into your functions.php file:


function rss_post_thumbnail($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$content = '<p>' . get_the_post_thumbnail($post->ID) .
'</p>' . get_the_content();
}
return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');


How often do you find yourself making a change to a post or a page, clicking “Update” then the “View Page” link?  Why not just have another button by “Publish” and “Update” that says ” and View” so you can do it all at once?  Just a suggestion, maybe I’ll get around to writing a plug-in just in time for someone else to have written a better one or they include it in the core…. 🙂