WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] I just want to post source code samples in a blog post (22 posts)

  1. Chris_K
    Member
    Posted 8 years ago #

    I apparently am having a blonde moment. Or a bland moment. or something. I want to include some code (html) samples in a post I'm building. using <code> isn't the trick. nor is block-quote. Isn't there a way to do this that won't attempt to render my html?

    Heh - would the "backtick" solution that we use here work? I really don't want to type a billion gt's and lt's if I can avoid it!

  2. Kafkaesqui

    Posted 8 years ago #

    &lt; = <
    &gt; = >

    Replace the characters on the right above in your sample HTML with the character entity codes on the left, and no "rendering" will occur.

  3. Chris_K
    Member
    Posted 8 years ago #

    drat. That hits my last sentence pretty hard!

    I really don't want to type a billion gt's and lt's if I can avoid it!

    Guess I'll pull it into vi first and do some global search and replace lovin'

  4. Chris_K
    Member
    Posted 8 years ago #

    Oh hell, and replace every leading space with a nbsp; too? Dang.. there has GOT to be a better way to not lose all formatting.

    Where's my html books?

  5. Kafkaesqui

    Posted 8 years ago #

  6. Edit: Bah, Kafkaesqui is a faster typer. :(

    There is a better way.

    http://codex.wordpress.org/Plugins/Posts_Formatting

    I use Code Auto Escape personally. There are also ones that will apply syntax highlighting (colors) to your code.

  7. Kafkaesqui

    Posted 8 years ago #

    But CAE is a little cooler then my suggestion. :)

  8. Chris_K
    Member
    Posted 8 years ago #

    Sweet! Thanks guys, I'll give those a shot.

  9. Chris_K
    Member
    Posted 8 years ago #

    Hmm, anyone seen one of these critters that's 2.0 friendly? I'll try some tonight, but with tinyMCE in the mix... I'm not overly hopeful.

  10. Arnan de Gans
    Member
    Posted 8 years ago #

    tinymce keeps raping the snippets i insert. is there a plugin to make this work better?

    i need to be able to insert php/css/html snippets

  11. moshu
    Member
    Posted 8 years ago #

    i need to be able to insert php/css/html snippets

    Then disable the wysiwyg. If you know code why would you need that idiotic tinymce that causes zillion times more problems than it solves?

  12. spencerp
    Member
    Posted 8 years ago #

    I was just wondering about this the other day, what to use or do, to be able to post "html codes" and stuff in the posts.

    I basically was trying what Handy said in the first post, but gave up on it at the time, because I had alot more to worry about getting done. Oh well..I'll try those two plugins or just turn off the tinymce, like Moshu said above here lol.. =)

    spencerp

    I guess I was just hoping to do it, without turning off the tinymce, but..oh well lol!! God damn things... =P

  13. alrescha
    Member
    Posted 8 years ago #

    Well, if anyone's interested, I sneaked into my copy of bbPress and stole their code for using the backtick, then dumped it into a quick plugin. I've tested it to work with my WordPress 2.0.2.

    Here's the working code:
    backtick-code.phps

    Just activate this and you can write code as you would here on this forum (well they're basically the same code). It doesn't touch your database at all, just filters post excerpts, contents, comments and it seems that it does RSS feeds as well.

    I've only made two modifications:

    1) Rather not so elegant (I think) solution to replace leading spaces with &nbsp; (for indentation). Also I just converted tabs into four &nbsp;—sometimes I copy and paste code that indent using tabs, so I wanted those to work as well.

    2) Within backtick code blocks, two backticks in a row will be converted back to one backtick when it is displayed. (Downside is you need to type four backtickes in a row, if you want to display a literal backtick while not inside a backtick code block.)

    I like using backticks just because I'm lazy and don't want to type a million CODE tags...although I do feel that the CAE plugin using the code tags is a "better" solution. After all, once you get settled into using backticks, these backticks will get stored into your database...

    BTW: reminders on mistakes I made or on how to improve this would be greatly appreciated. I'm just a PHP beginner.

    PS: I do not know if this works with the WYSIWIG editor. I've grown to hate that thing. :x

  14. Terry
    Member
    Posted 8 years ago #

    Maybe this suggestion is quite stupid, but this may help a little before you install the plugings.

    Try typing the code into Microsoft Word and then replace all "<" with "<" and replace all ">" with ">". And then paste everything back into your wordpress editing area.

    Sorry if I sidetracked a lot into Microsoft Office. Just a suggestion.

  15. spencerp
    Member
    Posted 8 years ago #

    Thanks for that alrescha. =) I'll give it a try on the localhost blog once....see what happens. ;)

    spencerp

  16. Arnan de Gans
    Member
    Posted 8 years ago #

    yea i tried replacing the < and >. it doesnt exactly get me excited when i post code with loads of tags.

    I also tried the autoescape (and most others i could find) plugin which works nicely, when tinymce is turned off.
    alrescha: your plugin doesnt work if the posted html includes the < li> tag

    i even tried to patch tinymce to make a 'original' plugin (insertcode, using geshi) work but then wp comes in between and makes a mess of it (codewise that is)

    i think im running out of options and stick with the old editor and a highlight plugin for now :)

    thanks anyway for the quick replies!

  17. alrescha
    Member
    Posted 8 years ago #

    SHRIKEE:
    Hmmm. It seems to work find if I'm logged in (<li> will show properly when posting, editing posts, commenting and editing comments). But if I'm not logged in, then <li> codes gets eaten by WordPress (in fact a lot of other tags get eaten, too).

    I suspect it's the hooks not properly written, but I'm at a loss here how to correct this problem. Probably have to find out exactly what goes on when comments get posted.

    PS: temporary test-bed here:
    http://claes.twbbs.org/insane/wordpress/6
    Comments open to all for now. No need to enter email.

  18. Rok
    Member
    Posted 8 years ago #

    Surprised, nobody talked about iG:Syntax Hiliter

  19. Arnan de Gans
    Member
    Posted 8 years ago #

    if i remember correct its not working very well with tinymce ...

    i tried it a while back

  20. Chris_K
    Member
    Posted 8 years ago #

    Lol - wow, one of my old threads!

    I ended up with iG:Syntax Hiliter as well (although CAE was pretty slick too). As I don't use the tinymce thingy, it works out quite well for me.

  21. Arnan de Gans
    Member
    Posted 8 years ago #

    i use code snippet, http://blog.enargi.com/codesnippet/.

    works very well in teh old editor.
    it uses the geshi library.

  22. alrescha
    Member
    Posted 8 years ago #

    Just an update on using backticks. It doesn't work properly in comments because of the kses filter. All one needs to do is to apply the backtick filter to pre_comment_content. This also means that the translated html entities in the code will be stored in the database (for comments only).

    Using the same code, just hook onto:

    add_filter('the_content','backticker_code_trick',1);
    add_filter('the_excerpt','backticker_code_trick',1);
    add_filter('pre_comment_content','backticker_code_trick',1);
    add_filter('comment_text','backticker_code_trick',1);

    This should make it work properly. For posts, code will be stored in its original form in the database; for comments, code will be translated into html entities, then get stored in the database. (Actually don't have to hook comment_text, if you've never used this on comments before.)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags