How to use prism to brighten the code when using markdown in wordpress

不言
Release: 2023-04-02 22:40:02
Original
2860 people have browsed it

This article mainly introduces the use of prism for code highlighting when wordpress uses markdown. It has a certain reference value. Now I share it with you. Friends in need can refer to it

Background: I recently switched my blog’s article editor to markdown, but I found that the CSS that comes with the wordpress theme cannot achieve code highlighting. As an obsessive-compulsive disorder, it seems very uncomfortable, so I tried my best to achieve code highlighting in markdown. .

1. Use markdown editor

First of all, you need to switch the article editor that comes with wordpress to markdown. Here you need to install the markdown plug-in. After several comparisons, I chose WP Editor .md plug-in, a markdown editor that can be previewed in real time. Please read the plug-in description in detail for its specific advantages.
Plug-in screenshot:
How to use prism to brighten the code when using markdown in wordpress

The most important thing is that the plug-in code highlighting uses prism, so the code in the editing stage will automatically use prism syntax. , no additional settings are required. If you don’t believe it, you can check the html source code of your article. There will be the following syntax:

<pre class="language-c"><code class="language-c">  code_here </code>
Copy after login

The code highlighting option is switched on in the plug-in settings, "Syntax Highlighting Option-Support Prism Syntax Highlighting"# Turn on in ##.

2. Go to the prism official website to download your own css and js files
Download address: http://prismjs.com/download.htm

Select your commonly used language and configuration, Click
DOWNLOAD JS and DOWNLOAD CSS to download css and js files, that is, prism.js and prism.css files.

3. Modify the wordpress theme
Enter the folder corresponding to the theme, create the prism folder, and put the prism.js and prism.css files into the folder

Modify funcations.php file, the custom path in it is the storage path of prism.js and prism.css files

function add_prism() {        
wp_register_style(            
&#39;prismCSS&#39;, 
            get_stylesheet_directory_uri() . &#39;prism/prism.css&#39; //自定义路径
         );          
wp_register_script(            
&#39;prismJS&#39;,            
get_stylesheet_directory_uri() . &#39;prism/prism.js&#39;   //自定义路径
         );        
wp_enqueue_style(&#39;prismCSS&#39;);        
wp_enqueue_script(&#39;prismJS&#39;);
    }
add_action(&#39;wp_enqueue_scripts&#39;, &#39;add_prism&#39;);
Copy after login
4.Enjoy
Okay, you can refresh your article at this time. You will find that code highlighting has been implemented, it is that simple!

The above is the entire content of this article. I hope it will be helpful to everyone's study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

WordPress realizes that the password is displayed directly on the page after registration in the registration interface

Use PHPstudy in Deploying PHP system under Windows server

The above is the detailed content of How to use prism to brighten the code when using markdown in wordpress. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template