⏲️ 2 minutes

How To Style The WordPress <>Code Block

Lubble Team
[lmt-post-modified-info]
I love WordPress for its customizations. Styling code snippets enhances user perceptions. Copy and paste the code below to style your WordPress code blocks.
Image of code

I've personally struggled to find this bit of code online, so I've documented the fix here.

The default styling for the <>code block is not ideal; by default, WordPress does not style your code block text, so the code block matches the rest of your webpage. See the examples below.

I don't know about you, but I want my code block to have a copy-and-paste button and a black background. This is better for users as well as visually. For speed reasons, I'm also averse to having a bunch of plugins that only do one single thing. So I solve this issue by styling the code block with plugins that I'm already using.

Here is how you can style the default WordPress code block to have a black background and white text. Which also has a simple copy-paste button at the top, all while using the free Code Snippets plugin:

  1. Install and activate the Code Snippets plugin.
  2. Go to Snippets > Add New.
  3. Give your snippet a title and paste the following code:
// Code block styling
function custom_code_block_style() {
   echo '<style>
      .wp-block-code {
         background-color: #000;
         color: #fff;
         font-family: "Courier New", Courier, monospace;
         padding: 10px;
      }
   </style>';
}
add_action('wp_head', 'custom_code_block_style');

// Add copy button to code block
function add_copy_button_to_code_block( $code_block_html, $block ) {
   if ( 'core/code' === $block['blockName'] ) {
      $copy_button = '<button class="copy-code-button">Copy</button>';
      $code_block_html = $copy_button . $code_block_html;
   }
   return $code_block_html;
}
add_filter( 'render_block', 'add_copy_button_to_code_block', 10, 2 );

// Copy button functionality
function add_copy_button_functionality() {
   echo '<script>
      document.querySelectorAll(".copy-code-button").forEach(function(button) {
         button.addEventListener("click", function() {
            const codeBlock = button.nextElementSibling;
            const range = document.createRange();
            range.selectNode(codeBlock);
            window.getSelection().removeAllRanges();
            window.getSelection().addRange(range);
            document.execCommand("copy");
            window.getSelection().removeAllRanges();
         });
      });
   </script>';
}
add_action('wp_footer', 'add_copy_button_functionality');
  1. Save the snippet and activate it.
  2. In the WordPress block editor, add the Code Block element to your page or post.
  3. Enter your code in the Code Block element and publish your page or post.

The code block in the WordPress block editor should now have a black background, text that looks like coding, and a simple copy-paste button that works at the top on the frontend.

About the author

January 29, 2023
Lubble Team
January 29, 2023
Lubble Digital Marketing is a boutique, Denver-based paid search agency specializing in SEM and SEO for high-intent, service-driven businesses.
OUR BLOG

Resources

April 6, 2026
The 8 Best AI Video Generator Models in 2026: A Hands-On Comparison

If you had told me a couple of years ago that I could type "a cyberpunk hedgehog making a latte" and get a photorealistic 4K video back in seconds, I would have laughed. But here we are in 2026, and AI video generation isn't just a novelty anymore, it's a massive part of my daily workflow.

Read More
February 16, 2026
The Ultimate Guide to Writing a White Paper

This guide leverages my experience to break down how to write, structure, and publish a document that earns trust rather than just demanding attention.

Read More
February 1, 2026
The Core Principles of Design With Examples

Depending on who you ask, there are anywhere from five to twenty "essential" rules out there. But in my experience, there are really only a dozen “laws” of visual design that matter across every medium. Here’s a guide I’ve created with the elements I find to be the most important, no matter your platform.

Read More
December 23, 2023
How does Quality Score relate to CPC across match types and filters?

The results are surprising, Quality Score and Cost Per Click are more clearly correlated when you look at Exact Match keywords.

Read More
January 29, 2023
How To Style The WordPress <>Code Block

I love WordPress for its customizations. Styling code snippets enhances user perceptions. Copy and paste the code below to style your WordPress code blocks.

Read More
January 29, 2023
How to Add A Title Field To User Profiles In WordPress

The code snippet plugin changed my life. I no longer edit the raw PHP files to add simple functions like titles to users' profiles.

Read More
OUR BLOG
April 6, 2026
The 8 Best AI Video Generator Models in 2026: A Hands-On Comparison

If you had told me a couple of years ago that I could type "a cyberpunk hedgehog making a latte" and get a photorealistic 4K video back in seconds, I would have laughed. But here we are in 2026, and AI video generation isn't just a novelty anymore, it's a massive part of my daily workflow.

Read More
February 16, 2026
The Ultimate Guide to Writing a White Paper

This guide leverages my experience to break down how to write, structure, and publish a document that earns trust rather than just demanding attention.

Read More
February 1, 2026
The Core Principles of Design With Examples

Depending on who you ask, there are anywhere from five to twenty "essential" rules out there. But in my experience, there are really only a dozen “laws” of visual design that matter across every medium. Here’s a guide I’ve created with the elements I find to be the most important, no matter your platform.

Read More
December 23, 2023
How does Quality Score relate to CPC across match types and filters?

The results are surprising, Quality Score and Cost Per Click are more clearly correlated when you look at Exact Match keywords.

Read More
January 29, 2023
How To Style The WordPress <>Code Block

I love WordPress for its customizations. Styling code snippets enhances user perceptions. Copy and paste the code below to style your WordPress code blocks.

Read More
January 29, 2023
How to Add A Title Field To User Profiles In WordPress

The code snippet plugin changed my life. I no longer edit the raw PHP files to add simple functions like titles to users' profiles.

Read More
Lubble Digital Marketing is a boutique, Denver-based paid search agency specializing in SEM and SEO for high-intent, service-driven businesses.
Insights