WordPress Tips : Writing Code in Your WordPress Blog Posts
How to include or write code in your wordpress blog post? The code can be anything, from HTML and CSS to programming language code like Java or even Mathematical formula. If you put the code as it is in the wordpress post, wordpress will convert that into ugly looking text and will mess up the layout.
There are plenty of wordpress plugins available to solve this issue. Commonly known as syntax highlighter , these plugins will do good job in identifying the keywords and line numbers. Many of these plugins will show keywords in different color and special characters in different color.
But how to achieve this without plugins? Let us discuss about using code inside your post without the help of any of these plugins. If you know bit of HTML and CSS then it is very easy to do this.
Write your own simple syntax code highlighter using HTML and CSS
We will utilize the two HTML Elements to showcases the code exactly in your blog post.
The <code> element</code>
The <code> elemnt represents a fragment of computer code. It can be anything like an XML, HTML, JavaScript or even a documet URL or file name. Generally speaking <code> element can be used where phrasing content is expected.
The <pre> element.
The <pre> element represents a block of preformatted text. Inside the
<pre> element the structure is represented by typographic conventions rather than by elements.
<pre> will preserver the spaces and other formatting.
<pre> element is best suited for including fragments of computer code, with structure indicated according to the conventions of that language. Browsers render preformatted text in a fixed-width font, should not collapse white-space, and should not wrap long lines.
A combination of <pre> and <code> elemnts can be used to display the code fragments. To display HTML entities you can use the following HTML characters.
1 2 3 4 5 | < = < > = > / = / " = " ' = ' |
etc..
The <pre> and <code> tag can be stylized using CSS based on your need.
An example CSS styles given below. This will display your code inside a box with font color based on your style class selection.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | pre {
border: solid 1px blue;
font-size: .95 em;
margin: 10px;
padding:10px;
background: #E6E3E6
width: 85%;
}
.java {
color: #000099;
}
.php {
color: #FF6699;
}
.html {
color: #32AC20;
} |
Here we have defined special CSS classes for languages like java ,php, html etc. If you know your wordpress theme template structure you can add the above CSS styles to your style-sheet file. Or you can add the styles in your header.php files after the element.
Using the above CSS we can display three different programming language code, Java code, PHP code and HTML code.Please not the usage of class attribute of <code> element. For each language we are using appropriate style class.
1 2 3 4 5 6 7 8 | <pre>
<code class="java">
class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
</code> |
This will display as below
class HelloWorld {
public static void main(String[] args) {
System.out.println(“Hello World!”);
}
}
You can use the above example to create more attractive styles. When you want to include a code fragment to your post, just add
<pre><code class=”xxxx”> Your Code Fragment </code></pre>
replace “xxxx” wih the style class (java,php,html etc as in our example above) and “Your Code Fragment” with the actual code.
Join Globinch on Social Media
Follow Globinch
Popular Posts
- Turn your Windows 7 Computer Into a Personal Wi-Fi Hotspot 20 comment(s) | 26,469 view(s)
- Search Engines for File Sharing Sites : Rapidshare Search, Megaupload and More 2 comment(s) | 18,455 view(s)
- Google Translate Now supports Bengali, Gujarati, Kannada, Tamil and Telugu 2 comment(s) | 17,344 view(s)
- Best PDF OCR Tools to Convert Scanned images to Text / Word Documents 4 comment(s) | 13,626 view(s)
- Google Top Searches Today – Hot Google Search Words today 8 comment(s) | 13,498 view(s)
- Globinch Search 0 comment(s) | 10,388 view(s)
- Access Blocked sites, Unblock Restricted Websites 16 comment(s) | 9,453 view(s)
- Password Revealer – How To Reveal- Show Password Behind Asterisks? 1 comment(s) | 8,517 view(s)
- Play Angry Birds Game Free and Offline in Chrome Browser 1 comment(s) | 8,244 view(s)
- Free Online OCR Software to Convert Images Into Searchable PDF, Doc, HTML or Text 2 comment(s) | 8,083 view(s)
Recent Articles
- How to Use Wi-Fi the Secure Way
- 5 Apps to Boost Android Performance
- Building a Smarter Site Using a WordPress Platform
- Samsung Galaxy S IV Smartphone :Summary of All the Radical Innovations
- 5 Killer Tips to Increase Traffic to Your Blog
- Domain Parking to Make Genuine Money Online
- SEO Is Not Dead, But You Need to Upgrade The Process
- A Complete SEO Guide on Using Social Media for Better SERPs
- When Should You Use Geotargeting or Setting Geographic Target in Google Webmaster Tools?
- How To Optimize Your Google+ Page And Profile For Search Engines








