When you’re working in WordPress there will be many times when you need to present code to your viewers. The WordPress editor provides a Code block that makes it easy to add and share code. The main advantage of the block is that it will keep the spacing used in the code without trying to format the text. Follow the steps below to use the Code block for your WordPress posts or pages.
Need a home for your WordPress site? Check out InMotion’s WordPress Hosting solutions for secure, optimized servers that will meet your performance and budget needs.
Using the Code Block
The Code block is very simple to use. However, please note that the appearance of the block will be determined by the theme used in your WordPress installation. As with any WordPress block, you will need to be logged into the WordPress Administrator Dashboard in order to add or edit your posts or pages. Here’s an example of the code block in action:
?php require "vendor/autoload.php"; $robo = '[email protected]'; use PHPMailer\PHPMailer\PHPMailer; use PHPMailer\PHPMailer\Exception; $developmentMode = true; $mailer = new PHPMailer($developmentMode);
There are no options that come with the use of this block. Basically, you add it to your page or post, then paste the code within it. The only option that you have will be the usual Additional CSS option provided in the block properties that is a part of all blocks.
- Open an existing page/post or create a new one.
- Click on the circled + sign in order to add a new block.
- Search for Code, or go to the Formatting section and select the Code block.
- Once the block appears, simply paste in your code when you’re ready.
Congratulations! You now know how to add and use a Code block within the WordPress editor. To learn more about using WordPress, please go to our WordPress Educational Channel.