MyBB Community Forums

Full Version: Copy PHP & CODE in post
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
As per title, I would like to know how I can insert the possibility of copying code into PHP or Code within the post.
You can try using some code formatter like Prism which has this feature in-built.
I did that on one of my forums.
In the mycode_code template (and also in mycode_php), add <button class="copy">copy</button> just after {$lang->code}
In a css (I created one, you can do the same or add to global.css) added:
.copy {
	float: right;
	font-variant: small-caps;
	color: green;
}
Then, add a js file containing:
$('.codeblock').find('.title').find('button').click(function() {
  blk = $(this).parent();
  $('.codeblock').each(function(i) {
    $(this).find('.title').find('button').css('color', 'green');
  });
  var range = document.createRange();
  var code = blk.next('.body')[0];
  range.selectNodeContents(code);
  window.getSelection().removeAllRanges();
  window.getSelection().addRange(range);
  document.execCommand("copy");
  blk.find('button').css('color', 'blue');
})
and load it in footer template
"Then, add a js file containing:"

shall I create a js file and place it in the jscprits folder?
Yes, js files must be uploaded, they can't be write from the ACP
Thank you very much for your help. I succeeded and it works perfectly!
+1 Smile