How do I add syntax highlighting to a WordPress blog hosted on WordPress.com?

12,282

Solution 1

On wordpress.com, you can't install plugins, so you have to use Wordpress' built-in widget.

Do it like:

[code language='python']

[/code]

Note that those tags go inside your visual editor, not the HTML editor.

You can find more information here.

Solution 2

The best way for syntax highlighting is using prismjs, which is completely free with awesome features.

  1. It has support for almost all programming languages, shell scripts and much more.

  2. It is very light weight and it will not bog down your sites speed like other syntax highlighting plugins.

For configuring prismjs you need to do little code tweaks in your function.php file. Follow the turoails in this blog http://skillslane.com/setup-syntax-highlighter-for-wordpress/

Solution 3

SyntaxHighlighter is a pretty good tool. It uses JavaScript with CSS classes to mark HTML elements that should be highlighted. It also allows code to be copied to the clipboard and printed.

Solution 4

If you don't want to, or can't install stuff on your blog, I suggest you use pygments to highlight your code. I described this here.

Solution 5

Here is an offline option (I know you are asking about online tool but just in case ...)

I use offline tools to process the blog entry with source code examples into html format and then just post the html.

It's really just:

  • offset source code by at least 4 spaces and specify language on the first line like :::html
  • run your file through:

python script:

import markdown
html = markdown.markdown(text,['codehilite'])

I setup markdown with pygments probably you could just use pygments for source code. You run the file thought the script and then copy paste the html to your blog.

You can dump css style, to highlight syntax, from pygments and then either link to it or include it in your page. I'm not sure if wordpress supports that.

Here is how to setup markdown with pygments to do syntax highlighting (for Blogger - the only specifics for Blogger is that it updates the Blogger css with the css to highlight code).

Share:
12,282

Related videos on Youtube

codingfloor
Author by

codingfloor

Updated on March 06, 2020

Comments

  • codingfloor
    codingfloor about 4 years

    I'd like to have a good syntax highlighter for my WordPress blog. What options do I have available? I often see one which has well-colored syntax highlighting and options to copy to the clipboard. Does anyone have an idea what tool this is?

    • Sean Bright
      Sean Bright almost 15 years
    • Sean Bright
      Sean Bright almost 15 years
      OK. I did that. Marking as a dupe.
    • codingfloor
      codingfloor almost 15 years
      I often see one which has well-colored syntax-highlighting and options to copy to the clipboard - does anyone have an idea what tool this is?
    • Thomas Owens
      Thomas Owens almost 15 years
      Is this a self-hosted WordPress installation or a hosted WordPress.com blog?
    • Lucky
      Lucky over 11 years
  • Jorge Israel Peña
    Jorge Israel Peña almost 15 years
    This is the only correct answer so far. Everyone missed the important little detail that his blog is hosted on wordpress.com, which does not allow the installation of plugins.
  • samoz
    samoz almost 14 years
    This seems like a great question for the new WebApps on Stack Exchange
  • phoxis
    phoxis about 13 years
    you can write the sourcecode tags inside the HTML editor as well. That is what i do all the time.
  • user776686
    user776686 over 10 years
    So, how does one install plugins on a WordPress blog hosted on WordPress.com? as OP wants
  • vjain419
    vjain419 over 9 years
    @samoz What if my language s not there in "supported lang" section! I am looking for system verilog lang support but i dont see any option :(
  • Lucky
    Lucky over 8 years
    This doesn't answer the question correctly since the OP ask for wordpress.com and not wordpress.org where the former doesn't support for plugin installations.
  • don bright
    don bright over 7 years
    yeah this doesn't always work. don't know if it is my old old 2010 theme or what. but it basically destroyed my indenting and gave me zero syntax highlighting.
  • Romeo Sierra
    Romeo Sierra about 7 years
    Does this support indentations? How do you put those?
  • Romeo Sierra
    Romeo Sierra about 7 years
    Found the answer to my question. This code tag needs not to be in the visual editor. You can put those in the HTML editor as well. When you put there, you can preserve indentation as well. I have suggested an edit. Please check it on...
  • Arman Ortega
    Arman Ortega about 7 years
    But how can I install Mivhak since I'm using a wordpress blog hosted on wordpress.com ?
  • Yoav Kadosh
    Yoav Kadosh about 7 years
    @ArmanOrtega when I wrote this answer I was not aware of the fact that you can't install plugins on WordPress.com
  • Ravikumar
    Ravikumar almost 7 years
    Thank you so much for solution.
  • user2023370
    user2023370 over 5 years
    This isn't working for me. I'm using C++, and I find with either "HTML" or "Visual", the text between angle brackets (and the angle brackets) is removed. As I switch between the different modes I even find that this has affected my original text - i.e. the angle bracket sections are removed there too! Somehow my older posts (from a few years ago) seem ok - though I'm very nervous that they too will become corrupted the more I struggle here. Is the obvious solution to switch blogging platform?
  • user2023370
    user2023370 over 5 years
    Does this really work for Python? C++ will not work through "Preview". Perhaps it works when you publish, but I don't see a way to even publish privately. This seems the most helpful official response: en.forums.wordpress.com/topic/…. Alas, that forum entry is from 2013 - and closed :(