Beginner's Guide for WordPress - Start your WordPress Blog in minutes.
Choosing the Best
WordPress Hosting
How to Easily
Install WordPress
Recommended
WordPress Plugins
View all Guides

How to Add Reusable Codes in WordPress Posts and Pages

Last updated on by
Elegant Themes
How to Add Reusable Codes in WordPress Posts and Pages

For better security WordPress post editor does not allow you to add executable code into your posts. However, we understand that there are times when you may want to add code to your posts. For example, you may want to add your Adsense Ad code into posts, or a form, or just some javascript. In the past, we have discussed how to allow PHP in posts, or how to add adsense code inside posts. In this post, we will show you how you can better manage these content blocks. The goal is to manage all reusable code snippets at one place, so that they can be used selectively inside your posts. Changing one block would change it every where it is used, and deleting it will remove it from every where.

First thing you need to do is install and activate the Global Content Blocks plugin. Once activated, go to plugin’s settings page under Settings » Global Content Blocks.

create content block

Click on Add a New Content Block button. This will take you to add a new content block page.

Adding content block name and type

Provide a name for your content block and then choose what kind of content block it is. Optionally provide a description for the content block. Finally paste the code the in the content box, or type it in. The content box editor is very similar to WordPress post editor.

Add content block code here

You can add images to your content block. There is also a button to add an existing content block inside a new content block. In most cases you would probably just want to paste some code here. This could be an Adsense code, a pricing table, an iframe embed, custom optin form etc. Switch to text editor and paste your code. Save your changes and your content block is ready to be used.

To add a content box to a post or page. Edit the post where you want to add the content block. Click on the global content block icon among your visual editor buttons. Clicking on it will open a pop-up where you can choose which content block you want to add.

Insert content block in a post

In case you have disabled visual post editor on your site. Then you can add a global content block to your posts by using the shortcode. These shortcodes are listed on Global Content Blocks settings page next to the content blocks you have created.

Adding content blocks using shortcodes

Global content blocks does not just allow you to use executable code inside posts, there are many other ways to do just that. What’s great here is that it allows you to save your code snippets in blocks so that you can reuse, modify, and delete them from one convenient interface.


Editorial Staff at WPBeginner is a team of WordPress lovers led by Syed Balkhi. Page maintained by Syed Balkhi.

WPBeginner's Video Icon
Our HD-Quality tutorial videos for WordPress Beginners will teach you how to use WordPress to create and manage your own website in about an hour. Get started now »

Comments

  1. Carlos says:

    Hi. Thanks for sharing this info. Very Useful.

    One question. How do you do to insert at the beginning of all your posts an image of a hosting or themes companies next to the image of the post. Do you use the same method descried in this post?. Thanks!

    • Editorial Staff says:

      Hey Carlos,

      No we do not use this method. We have this option customized in our theme. We are using a plugin called OIO Publisher to handle our ad inventory and ad rotation.

      http://www.wpbeginner.com/blueprint/oio-publisher/

      But still, you would have to customize it in your theme using HTML/CSS along with the php output function of OIO Publisher and the post thumbnail code.

      • Carlos says:

        Thank you. By the way, if you use OIO Publisher along with a cahce plugin, do the thumbnails rotate evenly? or have you noticed some thumbs / ads “frozen” because of OIO Publisher?.
        Thanks for your help and reply.

Add a Comment

We're glad you have chosen to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and all links are nofollow. Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.