Adding C/C++ code to a WordPress post

Adding code neatly to a post on your WordPress blog is easy when you know how. However, if you don’t know what you’re doing then WordPress makes it really easy to turn your code into a complete mess.

Most of the confusion stems from the different ways you can edit a post in WordPress. Over the last few years, WordPress has introduced newer versions of the editor that you use to write your posts. However, when I’m posting code I always always always use the Text view in the classic WordPress editor. Here, I’m going to show you how to

  1. Open your post in the classic editor.
  2. Display your post using the classic editor’s Text view.

Begin by navigating to the Dashboard of your blog. The Dashboard URL is the domain name of your blog followed by “/wp-admin“. For example, my Dashboard is located at

https://robosumo.wordpress.com/wp-admin

You need to replace “robosumo.wordpress.com” with the address of your blog. If you’re not already logged in then you’ll be prompted to enter your username and password.

If your post doesn’t already exist, you can create it from the Dashboard and open it for editing in the classic editor by clicking on “Add New” in the “Posts” sub-menu in the side bar on the left side of the Dashboard, as shown below:

If you want to add code to a post that already exists, click on “Posts” on the sidebar at the left side of the dashboard to list your existing posts. Locate the post you want to edit and click on the “edit” link that appears when you hover over it, as shown below:

Before you insert your code into the post, make sure you’re in “Text” view by clicking the tab at the top right corner of the edit box, as shown below:

The code itself can be copied and pasted in directly from the Arduino development environment (or wherever you’re editing your text), but it should be wrapped in a pair of “code” shortcodes:

[code language="cpp"]

*** PASTE YOUR CODE IN HERE ***

[/code]

Before copying and pasting your code from the Arduino editor, make sure it’s neat and tidy. In particular,

  • Include your name in a comment at the top.
  • Include a short description of the program in a comment at the top.
  • Include the date it was last updated in a comment at the top.
  • All comments throughout the program should be clear and accurate.
  • The indentation should be perfect.
  • The variable names should be self-explanatory.

If you’ve inserted the code correctly, it should appear similar to the following (this is the same example code as shown in the editor screenshot above):

//
// RoboSumo code example: Flash an LED
// Written by Ted Burke, 18-4-2019
//
 
void setup()
{
  pinMode(2, OUTPUT); // LED pin
}
 
void loop()
{
  digitalWrite(2, HIGH); // LED on
  delay(250);
  digitalWrite(2, LOW);  // LED off
  delay(250);
}
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s