How to add programming code to blog

  • There is a wordpress.com help page on adding code to a blog (at http://en.support.wordpress.com/code/posting-source-code/) but it does not work. I add the directive to my blog text and it displays as just that – text. Some examples I have seen of use use different codes, which I have tried, and none of them work. Specifically:

    [code language="cpp"]
    int main()
    {
    printf("Hello world!\n");
    }
    [/code]

    [sourcecode language="cpp"]
    int main()
    {
    printf(“Hello world!\n”);
    }
    [/sourcecode]

    <code language=”cpp”>
    int main()
    {
    printf(“Hello world!\n”);
    }

    None of these work. The last one (with angle brackets) is the closest but it does not have language coloration as described in the article, and reformats the code in objectionable ways. The best choice I currently have is to use the

     tag but that is completely different from your article.
    
    Could you please show me how to use this or point me at a blog that currently uses this feature.

    The blog I need help with is: (visible only to logged in users)

  • Yeah that is exactly the information I mentioned above WHICH DOESN’T WORK. Please see the first line of my message.

    Is there a way to get it to work or is there a known working blog article that can be identified?

    P.S. the second last paragraph in my original message got mangled. Attempt #2:

    The best choice I currently have is to use the

     tag but that is completely different from your article.
  • Hi there,
    Sorry my reply was not helpful. I’ll tag this thread for Staff assistance. Please subscribe to the thread so you are notified when they respond and please be patient while waiting.

  • Thanks very much; I appreciate your time on this.

    P.P.S. Attempt #3 on second last paragraph:
    The best choice I currently have is to use the “pre” tag but that is completely different from the article.

  • Hi,

    Can you provide me a link to where you’ve tried the code and failed and a screenshot?

    I tried your first example (using the code shortcode) and it produced this: https://cloudup.com/cPN7J2zClPr

    Thanks!

  • I generated 2 screenshots of Text and Visual modes:

    Text: http://www.brydon.net/images/CodeTagsNFG_Text.jpg
    Visual: http://www.brydon.net/images/CodeTagsNFG_Visual.jpg

    As you can see, the square bracket version of the directive shows up as text instead of doing what is requested. I didn’t show it, but attempted it with tags of “code” and “sourcecode” with square brackets, angle brackets and “pre” angle bracket code. It does different things with each, but none is the right thing.

  • Hi,

    Within the editors themselves, it won’t render as nicely as it does when previewing the page itself. For example, looking at your draft post, it appears like this for me when previewing it:

    https://cloudup.com/cgJ5gFTuewR

    The second code in your draft pose is causing some errors (the brush attribute, at least), but the initial one is working.

    In short, I think using the code or sourcecode shortcodes would work, though not when you’re in the editor itself.

  • Ohhhh…. alright so what you are saying is that I have to publish the page to see what it looks like (which is a “ha ha” point by itself and somewhat negates the idea of the “visual” tab).

    That is an important point that should really be reflected in your documentation. Also, in my travels, there seem to be a number of other options as well (displaying line numbers, highlighting lines etc.). It would be good to document this info as well.

    Thanks for your help. I haven’t verified that it works the way I want but I at least made progress.

  • The visual tab is on the list for a fair amount of work. It is WYSIWYG to a point, but especially with shortcodes, those simply can’t be processed for display in the visual tab as it stands now.

    Thanks for the suggestions. I’ll talk it over with the folks here to make sure the documentation covers it correctly.

  • The topic ‘How to add programming code to blog’ is closed to new replies.