Add a shortcode to every post or page

I’ve been asked a few times on how to integrated a single shortcode to every page. Here is how you can do this:My Rich Snippets WordPress Plugin allows you to built up your own WordPress shortcodes. Once saved you can use them anywhere in your posts, pages or custom post types. At current it’s not possible to integrate them automatically to every post because this doesn’t really make much sense. Shortcodes are made to use it only on certain places and not everywhere.

But it depends on what you want to do. In fact it can be useful to add a shortcode. For this there are some possibilities on how to achieve this. You can either add a code to your theme’s functions.php or you can write a small plugin. The best way is to write your own plugin because when your theme gets updated automatically it will overwrite the functions.php and you have to add it again. Manually written plugins don’t get updated and so you can use is a long as it’s compatible with WordPress :-)

1. Possibility: Add it to your themes functions.php

  1. Login-in to your FTP account
  2. Go to your WordPress installation and open up the /wp-content/themes/[your-theme-name]/ folder.
  3. Download the functions.php to your desktop.
  4. Optional: If there is no functions.php you can create one on your own.
  5. Open the functions.php with your favourite text editor and add the code below to the end of the file.
  6. Upload it and be happy! :-)

[box type=”info”]Note: The following will work with WordPress >= 3.5[/box]

<?php

function my_shortcode_to_a_post( $content ) {
  global $post;
  if( ! $post instanceof WP_Post ) return $content;

  switch( $post->post_type ) {
    case 'post':
      return $content . '[my_post_shortcode_goes_here]My content[/my_post_shortcode_goes_here]';
    case 'page':
      return $content . '[my_page_shortcode_goes_here]My content[/my_page_shortcode_goes_here]';
    default:
      return $content;
  }
}

add_filter( 'the_content', 'my_shortcode_to_a_post' );
?>

2. Possibility: Write your own shortcode-plugin

  1. Create a file called maybe my-own-shortcodes.php
  2. Open the file with your favourite text editor and add the code below.
  3. Save it and upload it to your wp-content/plugins/ folder.
  4. Log-in to your WordPress administration panel.
  5. Go to “Plugins” search for your plugin and activate it.
  6. Be happy! :-)

[box type=”info”]Note: The following will work with WordPress >= 3.5[/box]

<?php
/*
Plugin Name: My added shortcodes
Plugin URI: http://your-url.com
Description: Just a simple shortcode plugin that adds a special shortcode to a post, page or custom post type.
Version: 0.1
Author: Your-Name
Author URI: http://your-url.com
*/
function my_shortcode_to_a_post( $content ) {
  global $post;
  if( ! $post instanceof WP_Post ) return $content;

  switch( $post->post_type ) {
    case 'post':
      return $content . '[my_post_shortcode_goes_here]My content[/my_post_shortcode_goes_here]';

    case 'page':
      return $content . '[my_page_shortcode_goes_here]My content[/my_page_shortcode_goes_here]';

    default:
      return $content;
  }
}

add_filter( 'the_content', 'my_shortcode_to_a_post' );
?>
Activate the plugin to use it
Activate the plugin to use it

Hope this helps you out, guys.