Mittwoch, Juli 24, 2024
No menu items!
StartWordpressCode-Highlighting in Wordpress

Code-Highlighting in WordPress

There are several ways to add code highlighting to a WordPress website. One popular method is to use a plugin that provides this functionality.

Here are a few code highlighting plugins that you can use on your WordPress site:

  1. SyntaxHighlighter Evolved: This is a popular plugin that uses the SyntaxHighlighter JavaScript library to add code highlighting to your site. It supports a wide range of programming languages and has a simple, easy-to-use interface. It is the plugin that I use on this site.
  2. WP Code Highlight.js: This plugin uses the Highlight.js library and supports over 150 languages. It is lightweight and very easy to use.
  3. Crayon Syntax Highlighter: This plugin is highly customizable and supports a wide range of languages. It has a number of advanced features, such as line numbering and theme support.
  4. Code Snippets : It is a plugin for adding, managing, and running code snippets. It allows you to add code without modifying your theme or plugin files, also can you schedule and export code snippets.

Once you have installed and activated one of these plugins, you can use their shortcodes or a specific button in the visual editor to wrap your code snippet and apply the highlighting.

Another approach is to add the Syntax Highlighting CSS & JavaScript to your Theme. This can be done by adding the CSS and JS in the <head> of the site, usually it is done via the functions.php or by adding the code in a <style> and <script> tag in the header.php of the theme.

Please note that adding custom code changes to your theme directly is not a best practice, since it will be lost on updating the theme. In that case using a child theme is a good way to go on that.

Here are the general steps you can follow to add custom code highlighting to your WordPress site without using a plugin:

  1. Choose a JavaScript library for code highlighting. Some popular options include Highlight.js, Prism.js, and Rainbow.js.
  2. Download the library and its stylesheet. Make sure to select the languages you want to support.
  3. Add the stylesheet to your WordPress theme by including it in the head of your HTML document. This can be done by adding the following code in the header.php of your theme:
<link rel="stylesheet" type="text/css" href="path/to/stylesheet.css">
  1. Add the library’s JavaScript file to your theme. This can be done by adding the following code in the footer.php of your theme or in functions.php and enqueue it.
    function add_code_highlighting_script() {
        wp_enqueue_script('highlighting-js', 'path/to/library.js', array(), '1.0.0', true);
    add_action('wp_enqueue_scripts', 'add_code_highlighting_script');
  1. Add the necessary JavaScript code to initialize the library. This can be done by adding the following code in the footer.php of your theme, in the <script> tag.
    // Initialize the library and select the languages you want to support

  1. Finally, Wrap your code snippet with <pre><code> tags and add the class language-* to indicate the language of the code snippet.
<pre><code class="language-python">def my_function():
    print("Hello, World!")

Note that, using this approach it is possible to customize and fine-tune the highlighting to your needs, but it would require some extra steps and work.

Keep in mind that directly adding custom code changes to your theme is not recommended, since it will be lost on updating the theme. It is recommended to use a child theme, So your custom changes will remain after the theme updates.

Please let me know if you have any further questions.

Vorheriger Artikel
Nächster Artikel
I am a passionate Webdeveloper and Programmer.

Kommentieren Sie den Artikel

Bitte geben Sie Ihren Kommentar ein!
Bitte geben Sie hier Ihren Namen ein

- Advertisment -

Most Popular

Recent Comments