{"id":11285105,"date":"2019-03-07T10:11:45","date_gmt":"2019-03-07T10:11:45","guid":{"rendered":"https:\/\/wordpress.org\/support\/?post_type=helphub_article&#038;p=11285105"},"modified":"2026-05-25T06:25:23","modified_gmt":"2026-05-25T06:25:23","slug":"code-block","status":"publish","type":"helphub_article","link":"https:\/\/wordpress.org\/documentation\/article\/code-block\/","title":{"rendered":"Code block"},"content":{"rendered":"\n<p class=\"has-text-align-right has-small-font-size wp-block-paragraph\"><a href=\"https:\/\/wordpress.org\/documentation\/article\/blocks\/\">Go back to the list of <strong>Blocks<\/strong><\/a><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code<\/strong> block lets you add and display code snippets for others to view.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code<\/strong> block example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">if ( condition ) {\n    action1();\n    action2();\n} elseif ( condition2 &amp;&amp; condition3 ) {\n    action3();\n    action4();\n} else {\n    defaultaction();\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Add a Code block<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">To add the <strong>Code<\/strong> block, open the block inserter by clicking the (<strong>+<\/strong>) icon on the upper left corner of the editor. After that, look for <strong>Code<\/strong> using the search bar and click it to insert the block into the editor.<\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"1568\" style=\"aspect-ratio: 3010 \/ 1568;\" width=\"3010\" controls src=\"https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257921530-6fa51096-f087-49c8-841d-231d24914eca.mp4\"><\/video><figcaption class=\"wp-element-caption\">A demonstration to add and use code block<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Alternatively, you can type <code>\/code<\/code> on the editor to insert the <strong>Code<\/strong> block manually.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"542\" height=\"240\" src=\"https:\/\/wordpress.org\/documentation\/files\/2021\/02\/Screenshot-454535.png\" alt=\"Code block keyboard shortcut\" class=\"wp-image-14109493\" style=\"width:271px;height:120px\" srcset=\"https:\/\/wordpress.org\/documentation\/files\/2021\/02\/Screenshot-454535.png 542w, https:\/\/wordpress.org\/documentation\/files\/2021\/02\/Screenshot-454535-300x133.png 300w\" sizes=\"auto, (max-width: 542px) 100vw, 542px\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-left has-background wp-block-paragraph\" style=\"background-color:#e5f5fa\"><a href=\"https:\/\/wordpress.org\/documentation\/article\/adding-a-new-block\/\">Detailed instructions on adding blocks<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Block toolbar<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Most blocks have their own block-specific controls that allow you to manipulate the block right in the editor.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>Code<\/strong><em> <\/em>Block shows the following buttons:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Transform to<\/li>\n\n\n\n<li>Moving handles<\/li>\n\n\n\n<li>Bold<\/li>\n\n\n\n<li>Italic<\/li>\n\n\n\n<li>Link<\/li>\n\n\n\n<li>More Rich Text controls<\/li>\n\n\n\n<li>More options<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"760\" height=\"130\" src=\"https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561204-45bc26bb-f192-4fdd-a870-d3834c91c7f8.png\" alt=\"Code block toolbar\" class=\"wp-image-16359980\" srcset=\"https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561204-45bc26bb-f192-4fdd-a870-d3834c91c7f8.png 760w, https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561204-45bc26bb-f192-4fdd-a870-d3834c91c7f8-300x51.png 300w\" sizes=\"auto, (max-width: 760px) 100vw, 760px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Transform to<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Transform to button lets you change the <strong>Code<\/strong> block into a <em>Paragraph<\/em>, <em>Columns<\/em>, <em>Group<\/em>, <em>Custom HTML<\/em> or <em>Preformatted blocks<\/em>.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"762\" height=\"666\" src=\"https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561888-d7ce127c-9cc4-4396-8a9e-fa443fce8ddf.png\" alt=\"Code block toolbar with the transform option highlighted\" class=\"wp-image-16359981\" srcset=\"https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561888-d7ce127c-9cc4-4396-8a9e-fa443fce8ddf.png 762w, https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561888-d7ce127c-9cc4-4396-8a9e-fa443fce8ddf-300x262.png 300w\" sizes=\"auto, (max-width: 762px) 100vw, 762px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Transforming the <strong>Code<\/strong> block into <em>Group<\/em>&nbsp;will give you the ability to change the background color around the&nbsp;code.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"974\" height=\"416\" src=\"https:\/\/wordpress.org\/documentation\/files\/2019\/03\/Untitled-2676757.png\" alt=\"Code block in a group with a colored background\" class=\"wp-image-13399019\" srcset=\"https:\/\/wordpress.org\/documentation\/files\/2019\/03\/Untitled-2676757.png 974w, https:\/\/wordpress.org\/documentation\/files\/2019\/03\/Untitled-2676757-300x128.png 300w, https:\/\/wordpress.org\/documentation\/files\/2019\/03\/Untitled-2676757-768x328.png 768w\" sizes=\"auto, (max-width: 974px) 100vw, 974px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Moving handles<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The dotted icons can be used to drag and drop a block to the place of your choosing. The up and down arrow icons can be used to shift a block up and down in your document.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"760\" height=\"130\" src=\"https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28.png\" alt=\"Code block toolbar with moving handles highlighted\" class=\"wp-image-16359982\" srcset=\"https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28.png 760w, https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28-300x51.png 300w\" sizes=\"auto, (max-width: 760px) 100vw, 760px\" \/><\/figure>\n\n\n\n<p class=\"has-background wp-block-paragraph\" style=\"background-color:#e5f5fa\"><a href=\"https:\/\/wordpress.org\/documentation\/article\/moving-blocks\/\">Get more information about moving a block within the editor.<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Change alignment<\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"760\" height=\"356\" src=\"https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28-5.png\" alt=\"Code block toolbar with the alignment option highlighted\" class=\"wp-image-16359987\" srcset=\"https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28-5.png 760w, https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28-5-300x141.png 300w\" sizes=\"auto, (max-width: 760px) 100vw, 760px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The&nbsp;<em>change alignment<\/em>&nbsp;tool lets you align the&nbsp;<strong>Code<\/strong><em> <\/em>block&nbsp;within the content. You can choose one of the following alignment options:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>None:<\/strong>&nbsp;Leaves the block alignment as is.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Wide width:<\/strong>&nbsp;Increase the block&#8217;s width beyond the content size.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Bold<\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"760\" height=\"130\" src=\"https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28-1.png\" alt=\"Code block toolbar with bold option highlighted\" class=\"wp-image-16359983\" srcset=\"https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28-1.png 760w, https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28-1-300x51.png 300w\" sizes=\"auto, (max-width: 760px) 100vw, 760px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You can select the text in the <strong>Code<\/strong> block and use the <em>Bold<\/em> option or <strong>Ctrl+B<\/strong> \/ <strong>Cmd+B<\/strong> on your keyboard to bold it, which is usually heavier than the surrounding text.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Italic<\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"760\" height=\"130\" src=\"https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28-2.png\" alt=\"Code block toolbar with italic option highlighted\" class=\"wp-image-16359984\" srcset=\"https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28-2.png 760w, https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28-2-300x51.png 300w\" sizes=\"auto, (max-width: 760px) 100vw, 760px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You can select the text in the <strong>Code<\/strong> block and use the <em>Italic<\/em> option or <strong>Ctrl+I<\/strong> \/ <strong>Cmd+I<\/strong> on your keyboard to italicize it, which usually appears slanted to the right.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Link<\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"760\" height=\"130\" src=\"https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28-3.png\" alt=\"Code block toolbar with link option highlighted\" class=\"wp-image-16359985\" srcset=\"https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28-3.png 760w, https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28-3-300x51.png 300w\" sizes=\"auto, (max-width: 760px) 100vw, 760px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Use the link icon to hyperlink the highlighted text inside the <strong>Code<\/strong> block.<\/p>\n\n\n\n<p class=\"has-text-align-left has-background wp-block-paragraph\" style=\"background-color:#e5f5fa\"><a href=\"https:\/\/wordpress.org\/documentation\/article\/link-editing\/\">Read about more link editing options.<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">More rich text options<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The drop-down menu to the left of the&nbsp;<a href=\"https:\/\/wordpress.org\/documentation\/article\/more-options\/\">More options menu<\/a>&nbsp;contains a range of additional rich text editing options such as highlighting, inline code, strikethrough, and more.<\/p>\n\n\n\n<p class=\"has-text-align-left has-background wp-block-paragraph\" style=\"background-color:#e5f5fa\"><a href=\"https:\/\/wordpress.org\/documentation\/article\/more-text-editing-overview\/\">Read about more rich text editing options.<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">More options<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Options menu represented by three vertical dots on the far right of the toolbar gives you more features such as the ability to duplicate or remove the block.<\/p>\n\n\n\n<p class=\"has-background wp-block-paragraph\" style=\"background-color:#e5f5fa\"><a href=\"https:\/\/wordpress.org\/documentation\/article\/more-text-editing-overview\/\">Read about these and other settings.<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Block settings<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The block settings panel contains customization options specific to the block. To open it, select the block and click the <strong>Settings <\/strong>button next to the <strong>Publish <\/strong>button.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"600\" height=\"118\" src=\"https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28-4.png\" alt=\"Block settings panel\" class=\"wp-image-16359986\" srcset=\"https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28-4.png 600w, https:\/\/wordpress.org\/documentation\/files\/2024\/03\/257561238-782eee25-933c-4f66-95b7-439ae8022e28-4-300x59.png 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">Color<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>Code<\/strong> block provides color settings options to change the text and background colors.<\/p>\n\n\n\n<p class=\"has-background wp-block-paragraph\" style=\"background-color:#e5f5fa\">For details, refer to this support article: <a href=\"https:\/\/wordpress.org\/support\/article\/colors-settings-overview\/\">Color settings overview<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Typography<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>Code<\/strong> block provides typography settings to change the font family, appearance, line height, letter spacing, decoration, letter case, and font size.<\/p>\n\n\n\n<p class=\"has-background wp-block-paragraph\" style=\"background-color:#e5f5fa\">For details, refer to this support article: <a href=\"https:\/\/wordpress.org\/support\/article\/typography-settings-overview\/\">Typography settings overview<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Dimensions<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>Code<\/strong> block provides dimension settings options to add padding and margin.<\/p>\n\n\n\n<p class=\"has-background wp-block-paragraph\" style=\"background-color:#e5f5fa\">For details, refer to this support article: <a href=\"https:\/\/wordpress.org\/support\/article\/dimension-controls-overview\/\">Dimension settings overview<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Border<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>Code<\/strong> block provides border settings options to add border color, width, and radius.<\/p>\n\n\n\n<p class=\"has-background wp-block-paragraph\" style=\"background-color:#e5f5fa\">For details, refer to this support article: <a href=\"https:\/\/wordpress.org\/documentation\/article\/border-settings-overview\/\">Border settings overview<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Advanced<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The Code block provides the following Advanced settings options: HTML Anchor, Additional CSS Class(es), and Styles.<\/p>\n\n\n\n<p class=\"has-background wp-block-paragraph\" style=\"background-color:#e5f5fa\"><a href=\"https:\/\/wordpress.org\/documentation\/article\/advanced-settings-overview\/\">Learn more about advanced settings<\/a><\/p>\n\n\n\n<div class=\"wp-block-group changelog has-background is-layout-flow wp-block-group-is-layout-flow\" style=\"background-color:#e0e0e0\">\n<h2 id=\"changelog\" class=\"wp-block-heading has-large-font-size\">Changelog <\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Updated 2026-05-25 (props to @kjoyner @awetz583)\n<ul class=\"wp-block-list\">\n<li>Updated &#8220;Advanced&#8221; section to refer to new overview page<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Updated 2026-03-01\n<ul class=\"wp-block-list\">\n<li>Updated block name from italic to bold (props katmoody, rollybueno)<\/li>\n\n\n\n<li>Monospaced when referrencing <code>\/code<\/code> block (props katmoody, rollybueno)<\/li>\n\n\n\n<li>Add a section heading on how to add a Code block (props katmoody, rollybueno)<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Updated 2025-12-08\n<ul class=\"wp-block-list\">\n<li>Changed the capitalization in the title to Code block.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Updated 2024-03-26\n<ul class=\"wp-block-list\">\n<li>Add wide align support<\/li>\n\n\n\n<li>Update videos and screenshots for 6.3<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Updated 2023-08-09\n<ul class=\"wp-block-list\">\n<li>Replaced the link section with short summary linking to new dedicated page.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Updated 2023-06-08\n<ul class=\"wp-block-list\">\n<li>Replaced &#8220;More rich text options&#8221; section with short summary linking to new dedicated page for rich text editing options.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Updated 2023-06-02\n<ul class=\"wp-block-list\">\n<li>Screenshots for 6.2<\/li>\n\n\n\n<li>Revised formatting<\/li>\n\n\n\n<li>Added transform to paragraph support<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Updated 2021-02-27\n<ul class=\"wp-block-list\">\n<li>Added Typography option<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Updated 2020-09-13\n<ul class=\"wp-block-list\">\n<li>Screenshots and video as per WordPress 5.5<\/li>\n\n\n\n<li>Added feature changes in Block Toolbar<\/li>\n\n\n\n<li>Added feature changes in Block Settings<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Created 2019-03-07<\/li>\n<\/ul>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Go back to the list of Blocks Code block lets you add and display code snippets for others to view. Code block example: Add a Code block To add the Code block, open the block inserter by clicking the (+) icon on the upper left corner of the editor. After that, look for Code using [&hellip;]<\/p>\n","protected":false},"author":7298140,"featured_media":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false},"category":[80,85],"class_list":["post-11285105","helphub_article","type-helphub_article","status-publish","hentry","category-customization","category-text-blocks"],"revision_note":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/wordpress.org\/documentation\/wp-json\/wp\/v2\/articles\/11285105","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wordpress.org\/documentation\/wp-json\/wp\/v2\/articles"}],"about":[{"href":"https:\/\/wordpress.org\/documentation\/wp-json\/wp\/v2\/types\/helphub_article"}],"replies":[{"embeddable":true,"href":"https:\/\/wordpress.org\/documentation\/wp-json\/wp\/v2\/comments?post=11285105"}],"version-history":[{"count":21,"href":"https:\/\/wordpress.org\/documentation\/wp-json\/wp\/v2\/articles\/11285105\/revisions"}],"predecessor-version":[{"id":16368452,"href":"https:\/\/wordpress.org\/documentation\/wp-json\/wp\/v2\/articles\/11285105\/revisions\/16368452"}],"author":[{"embeddable":true,"href":"https:\/\/wordpress.org\/documentation\/wp-json\/wporg\/v1\/users\/kriskorn"}],"wp:attachment":[{"href":"https:\/\/wordpress.org\/documentation\/wp-json\/wp\/v2\/media?parent=11285105"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wordpress.org\/documentation\/wp-json\/wp\/v2\/category?post=11285105"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}