Adding this snippet to your active theme’s (or child theme’s)
functions.php file will automatically convert backtrack wrapped phrase to be wrapped in code block. The word functions.php was written with back ticks wrap. When even there will be matching back ticks within WordPress content area, it will convert to inline code. If we use one back tick, it will not get replaced :
This is a very simple
preg_replace function of PHP. Many actually searches :
# wordpress official forum answers
They are simply asking how to convert the back ticks to code block in an easy way. Even people wrote about more complex methods.
Here is PHP Manual on
It is quite odd, the people who wrote various WordPress Plugins which actually use this function can not provide the answer!
Convert Backticks to Code Block in WordPress : More on BackTicks
These symbols are known as Grave accent. In TeX typeset text, the back tick character is used as a syntax to represent curly opening quotes. It is also used for supplying the numeric ASCII value of an ASCII character wherever a number is expected. Unix shells and the programming languages like Perl, PHP, and Ruby use pairs of this character to indicate substitution of the standard output from one command into a line of text defining another command. Possibly you have seen another closer one :
sudo chown 'root:www-data' /var/www/html
This is also used by the Markdown formatter to indicate code. The old-style backquotes do treat backslashes and nesting a bit different. It is better to AVOID this very simple
preg_replace function of PHP, although on frontend we will possibly never need complex stuffs.
Convert Backticks to Code Block in WordPress : Basic Snippet
This is a basic snippet which can convert your all back ticks to code :
tcw_the_content is just a name, you can change it to yours one. We are asking PHP to replace
$content of WordPress (that is post, pages, everything what we see in the frontend) to get replaced by code. But, this has a big fallacy. If we use backticks within a pre block (like for writing a bash script, commands etc.) – they’ll also get replaced and will be vanished. So, we need to add some condition within this
$content part. Understand this – you will not use the snippet provided above because of the bug.
Convert Backticks to Code Block in WordPress : Final Snippet
So, we need to escape the pre tag wrapped text area, we should include this condition :
So, finally our code is becoming :
However, we will advice to use any advanced syntax highlighter in coding blogs because without a structured if then else return, the logic might not work. PHP is not very powerful language for text with code output after lot of conditional filtering. If it was Python, we could filter many times; if we add more filters for PHP with data on MySQL database, page loading time can get increased. So, for that reason plugin should be used in blogs where there are lot of codes, commands.