0

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...

2 Answers 2

2

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.

2
  • 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, 2011 at 18:12
  • now showing codes like this --> &lt;!&#8211; &#8211;&gt;
    – Eray
    Apr 2, 2011 at 16:11
1

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>"]
6
  • 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. Mar 31, 2011 at 18:19
  • Hello, John, thanks! You mean this function? : pastebin.com/CdbPRACA
    – Eray
    Mar 31, 2011 at 18:25
  • I think this is OK : function codefunc( $atts, $content ) { return "<code>".htmlentities($content)."</code>"; }
    – Eray
    Mar 31, 2011 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! Mar 31, 2011 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. Mar 31, 2011 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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