Coding Internet

How to Write Ruby Code in WordPress

Writing Ruby code in WordPress is primarily for display purposes in posts or pages, as WordPress is PHP-based and does not execute Ruby code. Here’s how you can add Ruby code snippets to your WordPress content:

1. Use the Code Block

WordPress provides a ‘Code’ block that can be used to display code snippets.

  • While editing a post or page, click on the “+” button to add a new block.
  • Search for and select the ‘Code’ block.
  • Paste your Ruby code into this block.

The ‘Code’ block will display your code using a monospace font, which is appropriate for code.

2. Use a Syntax Highlighter Plugin

For syntax highlighting, which adds color to your code to make it more readable, you can use a plugin. Popular syntax highlighter plugins include “Crayon Syntax Highlighter” and “SyntaxHighlighter Evolved.”

  • Install the plugin through the WordPress admin panel by going to Plugins > Add New and searching for the syntax highlighter plugin.
  • Once installed and activated, these plugins usually provide a shortcode or a Gutenberg block that you can use to wrap around your Ruby code.

Here’s an example of what the shortcode might look like:

# Your Ruby code here
def say_hello(name)
puts "Hello, #{name}!"


3. Embed a Gist

Another method for displaying code is to use GitHub Gists.

  • Create a Gist at with your Ruby code.
  • Copy the URL of the Gist.
  • Paste the URL directly into your WordPress post editor. WordPress will automatically turn the URL into an embedded Gist with proper formatting and syntax highlighting.

4. Custom CSS

If you’d rather not use a plugin, you can add custom CSS to your theme.

  • Go to Appearance > Customize > Additional CSS.
  • Add custom CSS rules for styling <pre> or <code> tags.

Example CSS:

pre.ruby-code {
background-color: #f9f9f9;
border: 1px solid #ccc;
padding: 10px;
overflow: auto;
font-family: Consolas, "Courier New", monospace;

You would then wrap your Ruby code in <pre class="ruby-code">...</pre> tags within your post.

Important Notes

  • Always preview your post to ensure the code is displayed correctly.
  • Changing themes or plugins may affect how your code is displayed.
  • The code you add to your posts is for display purposes only and will not be executed.
  • Properly escape your code if not using a block or shortcode to prevent WordPress or browsers from rendering it as HTML.

Leave a Reply

Your email address will not be published. Required fields are marked *