MyBB Community Forums

Full Version: [Tutorial] Improved [code] tags
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone!

I recently migrated my forum from SMF to MyBB, and I quickly discovered the default [ code ] tags are lacking some functionality. Here's a list of things I needed:
  1. Allow [ code=Something ] tags where "Something" would become the title of the code block.
  2. Preserve whitespace inside [ code ] tags
  3. Don't add extra <br>s inside [ code ] tags
  4. Don't convert tabs to spaces inside [ code ] tags

So, if you're in a similar boat as me, and need any or all of this functionality, here's what you do:

1. Allow [ code=Something ] tags where "Something" becomes the title of the code block
  • Go to Admin CP -> Configuration -> MyCode (on the left sidebar).
  • Click "Add new MyCode".
  • Title it whatever you want (I used "Improved code tags"), and set the following:
Regular Expression:
\[code\=(.*?)\](?|\n(.*?)\n|\n(.*?)|(.*?)\n|(.*?))\[\/code\]

Replacement:
<div class="codeblock">
<div class="title">$1:</div>
<div class="body" dir="ltr"><code>$2</code></div>
</div>
  • Under "Enabled" select "Yes" then click Save. You're all done with #1.

2. Preserve whitespace inside [ code ] tags
  • Go to Admin CP -> Templates and Styles and click on your theme.
  • At the top click "Add Stylesheet".
  • Give it a name (I used "custom.css").
  • Select "Globally" under "Attatched to" and click "Write my own content".
  • Paste the following into the box:
.codeblock code {
     white-space: pre;
}
  • Click save.
  • Go back to your theme and make sure that your new css file has the highest order
  • Save the stylesheet ordering. You've now finished #2.

3. Don't add extra <br>s inside [ code ] tags

If you've done #2, you'll see that #3 is a problem. The following info is taken from a post by metulburr (https://community.mybb.com/thread-205148...pid1249392) so thanks to him!!

To fix this, we'll have to edit a core MyBB file, so be careful!
  • Using the FTP client of your choice, go to your main MyBB directory, then to the "inc" subfolder, and edit the file "class_parser.php".
  • At the top of the file you'll see "<?php" and underneath that you'll see a bunch of comments (inside /* */ symbols). After the comments, and above "class PostParser" (or whatever the first bit of code is) paste the following code:


function my_nl2br($string){
    $string = str_replace("\n", "<br />", $string);
    if(preg_match_all('/\<code\>(.*?)\<\/code\>/', $string, $match)){
        foreach($match as $a){
            foreach($a as $b){
                $string = str_replace('<code>'.$b.'</code>', "<code>".str_replace("<br />", "\n", $b)."</code>", $string);
            }
        }
    }
return $string;
}

Now find the line:

$message = nl2br($message);

and replace it with:

$message = my_nl2br($message);

You're all done with #3

If you're not worried about #4, you can just save the file and re-upload it. Otherwise, continue on.


4. Don't convert tabs to spaces inside [ code ] tags

For #4 you'll need to edit class_parser.php again.

Find the lines:

$code = str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;', $code);
$code = str_replace("  ", '&nbsp;&nbsp;', $code);

And comment them out like so (note the double slashes at the beginning of each line):

//$code = str_replace("\t", '&nbsp;&nbsp;&nbsp;&nbsp;', $code);
//$code = str_replace("  ", '&nbsp;&nbsp;', $code);

You're now all done with #4. Just save class_parser.php and re-upload it.

If you have any questions just let me know!
TFs...! Smile
NICCEE!
I like this, and can use for one of my forums, but atm i'm editing a myBB forum and want the 'Code' text above the code box to say 'Links' and also the 'Code' button...
CAN SUM1 PLS TELL ME WHICH FILE THE CODE (& QUOTE) BOX html/css IS IN SO I CAN EDIT THESE 'TITLES' PLS?

Much Thanks

P.S - Also need to change the size of text as atm its too large & bold, I want to remove bold & make italic for the code/quote boxes & make size of text size smaller! :-)
Hello, I find new problem here. When I post it while in  "View Source" active, it will show custom title. Later on if I want to preview the post before posting it. The "Code Title" back to "Code" instead of [code=Custom Title]

any fix for this?