Custom Development Without Child Theming Or Custom Plugins

Let’s say you want to use a simple WordPress theme like Blocksy, yet you want to add a bit of custom functionality. What are your options? Well the most logical thing to do would be to create a child theme with custom layouts. Another option would be to build a custom WordPress plugin. Both of those options make sense if your building out something complex. For something more basic let’s look at another option that doesn’t require a child theme or building your own WordPress plugin.

Introducing Code Snippets combined with shortcodes.

Code Snippets is a free plugin which allows you to inject custom PHP code via a graphical interface within the WordPress backend.

These code snippets can be any arbitrary PHP code. When building a basic site I’ll often just want a few custom post types and maybe a few ACF fields. Then a few custom layouts to display these custom post types and custom fields. When using code snippets, this is all possible with a bit of reliance on WordPress shortcodes.

Shortcodes are a great way to inject custom functionality.

You would start by created a code snippet the a custom post type. I recommend using GenerateWP to create the code block. In my example I created a post type called captaincore_recipes. Next create a code snippet to handle the listing of the custom post type within a shortcode. The following code creates a shortcode called captaincore_cookbook_shortcode which does just that.


function captaincore_cookbook_shortcode() {
  
    $html = "";

    // WP_Query arguments
    $args = [
        'post_type'      => [ 'captaincore_recipes' ],
        'posts_per_page' => -1
    ];

    // The Query
    $query = new WP_Query( $args );

    // The Loop
    if ( $query->have_posts() ) {
        $html .= "<div>";
        while ( $query->have_posts() ) {
            $query->the_post();
		    $post_id     = get_the_ID();
		    $content     = get_the_content();
		    $description = get_field( 'description' );
		    $title       = get_the_title();
		    $permalink   = get_the_permalink();
		    $html       .= "<p><a href='{$permalink}'>{$title}</a><br />{$description}</p>";
		}
        $html .= "</div>";
    }

    // Restore original Post Data
    wp_reset_postdata();
    return $html;

}
add_shortcode( 'captaincore_cookbook', 'captaincore_cookbook_shortcode' );

Now place this new shortcode within a WordPress page.

That’s pretty much it. You now have a custom post type which is listing results on as shown here: https://captaincore.io/cookbook/.

Shortcodes within widgets can add custom functionality site-wide

Most themes support widgets. You can likewise create a shortcode which is used within a widget. This allows you to put conditional code on every page. The following shortcode captaincore_post_nav_shortcode will add a simple back button when viewing the single page template of various custom post types.

function captaincore_post_nav_shortcode() {
	$html = "";
 	if ( get_post_type( get_the_ID() ) == 'captaincore_recipes' ) {
    		$html = "<span><a href='/cookbook/' class='button'>Back to Cookbook</a></span>";
	}
   	if ( get_post_type( get_the_ID() ) == 'captaincore_emails' ) {
    		$html = "<span><a href='/email-updates/' class='button'>Back to All Updates</a></span>";
	}
	return $html;
}
add_shortcode( 'captaincore_post_nav', 'captaincore_post_nav_shortcode' );

// Add shortcode to widgets
add_filter( 'widget_text', 'do_shortcode' );

This shortcode can be attached to any widget section using a simple text field as shown here.

Works great for a quick and dirty solution however it can easily break your site.

This method comes with a big warning. Any PHP typo within a code snippet and your website will break. So code cautiously. Coding for shortcodes is a bit different then coding WordPress layouts. Within shortcodes, all output needs to be returned. That means you can’t just loop through a bunch of posts and echo things out. You’ll need to first build up the output and then return it back. That said there really aren’t any limitations. This method comes with a big advantage over using a child theme as it’s theme independent. That means custom functionality will continue to work no matter which WordPress theme is used.