Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to add <code> tag to my post, easily. For example, Adding code is very easily in Stackoverflow and other sites. Basically, we can add codes between apostrophes (``) I need something like that for Wordpress .

Maybe i can use shortcode function for this.

For example : [code="<b>Hello</b>"] will show exactly like this : <b>Hello</b>

In summary, i'm sharing a lot of HTML and PHP codes with my readers on my blog. I need an easily way to share them. Is it possible, like stackoverflow's apostrophe method ? Or maybe shortcodes ?

Thanks...

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

I'd suggest using Syntax Highlighter Evolved. It's by Viper007Bond. You can use it like this:

[sourcecode lang="php"]
<?php
echo 'Hello World!';
?>
[/sourcecode]

Or even like this:

[php]
<?php
echo 'Hello World!';
?>
[/php]

A screenshot:

enter image description here

EDIT

An example of how you could do the backticks method that SE uses:

function wpse13653_content( $content ){
  return preg_replace( '@`([^`]+)`@', '<code>$1</code>', $content );
}

foreach( array( 'the_content', 'comment_text' ) as $hook )
  add_filter( $hook, 'wpse13653_content', 1 );

function wpse13653_excerpt( $content ){
  $content = preg_replace( '@`([^`]+)`@', '<code>$1</code>', $content );
  return str_replace( '`', '', $content );
}

add_filter( 'the_excerpt', 'wpse13653_excerpt', 1 );

This would let you use backticks in comments and post text, but would also prevent the backticks from showing up in excerpts after the code's been stripped in automatic excerpts.

share|improve this answer
    
You misunderstood me, i'm recently using a plugin like this, but i don't want to use it just a <br> code for example. Actually, problem solved, i wrote a shortcode for me :) –  Eray Mar 31 '11 at 18:12
    
now showing codes like this --> &lt;!&#8211; &#8211;&gt; –  Eray Apr 2 '11 at 16:11
add comment

OK, i'm using this :

function codefunc( $atts ) {
extract( shortcode_atts( array(
    'c' => ''
), $atts ) );

return "<code>".htmlentities($c)."</code>";
}
add_shortcode( 'code', 'codefunc' );

Usage :

[code c="<br><b>Hello</b>"]
share|improve this answer
1  
Personally, I'd suggest you use the $content argument instead of passing the code as an argument. e.g. function codefunc( $atts, $content ){ return "<code>$content</code>"; }. Usage would be like this: [code]<br><b>Hello</b>[/code]. Doing it that way would allow you to use both kinds of quotes without needing to escape them. –  John P Bloch Mar 31 '11 at 18:19
    
Hello, John, thanks! You mean this function? : pastebin.com/CdbPRACA –  Eray Mar 31 '11 at 18:25
    
I think this is OK : function codefunc( $atts, $content ) { return "<code>".htmlentities($content)."</code>"; } –  Eray Mar 31 '11 at 18:34
    
That last one looks great. You shouldn't need the shortcode_atts function anymore, though, since you're not using the attributes. Upvoted! –  John P Bloch Mar 31 '11 at 18:45
1  
You could, but you'd have to add a filter to 'the_content' and 'the_excerpt' on priority 9 or lower and run some sort of preg_replace[_callback] type function to handle the code swap. I'll edit my answer with an example. –  John P Bloch Mar 31 '11 at 19:07
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.