Hi there, I will teach how to create your own shortcode in wordpress in a secure way and very clean code. Well it was easy to create your own shortcode but did you consider it safe and clean? Well old traditional way we code it in the function.php as example like this right?

function client_shortcode( $atts, $content = "" ) {
      $content = '<div class="shortcode">this is my shortocde it was coded like this</div>';      
return $content; 
add_shortcode( 'client_shortcode', 'client_shortcode' );

As you can see the element code was inline in that function called client_shortcode which hook to the add_shortcode. What if you are building it to show up the list of the posts so it will be more code inside the client_shortcode function. Well I am not saying that this not right thing to do but, it will lower any security in your website.

So I came up a solution for this problem and make it safe and clean. Here is the code complete code

/************* POSTS SHORTCODE ***************/
function hybrid_post_shortcode( $atts, $content = "" ) { 
	return load_template_part('content/post', 'shortcode', $atts);
add_shortcode( 'hybrid_post_shortcode', 'hybrid_post_shortcode' );

function load_template_part($template_name, $part_name=null, $param=null) {
	$param_pass = $param; 
	include( locate_template( $template_name . '-' . $part_name . '.php', false, false ) ); 
    $var = ob_get_contents();
    return $var;

As you can see in that code it much simplier and clean. This code was based to call a template file so that path that I put it in here /wp-content/Your-Themes/content/post-shortcode.php

This is the code for post-shortcode.php you can download it here

Then you can call it [hybrid_post_shortcode cat_id=”4″ mode=”light”]. So imagine if you put all that code post-shortcode.php inline in the function hybrid_post_shortcode it will be disaster. This just an easy tips to call posts anywhere in your website using shortcode much clean and safe. You can construct your own shortcode just follow that easy steps :).