How to Copy and Paste Code
As strange as it may seem, copying and pasting text is not the same as copying and pasting code. Fortunately, while it is different, it is not very difficult. This supplement will guide you through the process of copying, pasting, and verifying PHP code to ensure it works correctly on your WordPress site.
The Importance of Code Formatting
Properly copying and pasting code is critical because even minor mistakes, like missing a line break or adding an extra space, can cause your site to crash. When you copy code, you’re not just worried about copying words, you also need to copy the formatting. Code relies on precise formatting to function correctly. The line number each line of code is on, the way it is indented, and how it is spaced are all important. Losing any of this formatting can cause errors or make the code invalid.
Using a Reliable Text Editor
It’s always a good idea to use a plain text editor for handling code. Programs like Notepad (Windows), TextEdit (Mac, in plain text mode), or VS Code (Windows or Mac) ensure there is no hidden formatting added to the code. Avoid using word processors like Microsoft Word or Google Docs, as they will add unwanted formatting.
Steps for Copying and Pasting Code
1. Copy the code from the curriculum or resource.
2. Paste it into a plain text editor.
3. Open the resource document of screenshots. Find the screenshot that matches the lesson and page number you are on. The “learn” document has a text box that allows you to copy and paste the text, but it does not contain the correct formatting. For example, the line numbers on the “learn” document where you copy the code from are not the true line numbers that need to be used on your WordPress site. You must refer to the resource document and check the screenshot for the lesson you are on to get the correct formatting.
4. While looking at the screenshot, match the formatting in the image so that it is exactly the same as what is in your text editor. Pay close attention to each line of code, making sure the line number in the screenshot for each line of code is the same as what is in your text editor. Your code should be exactly the same as what you see on the resource document, with every line, indentation, and space the same.
5. Once your formatting is correct, make any necessary changes as instructed in the “learn” document. This includes changing the items that are highlighted yellow or green.
6. Navigate to the correct file in your WordPress file manager. Before you alter the code, it is a good idea to make a backup of the original code in case your new code causes an error. Right click on the file you need to edit and select “duplicate”. This will make a copy of the file so you have a backup of the original code in case your new code causes an error.
7. Copy the code from the plain text editor and paste it into the correct WordPress file. When pasting code, make sure to paste it exactly where it belongs. The TurnKey Tech Ed curriculum will tell you where in the file to paste the code, and some editable WordPress files include a comment telling you where you can enter custom code. For example, wp-config.php includes the following comments:
/* Add any custom values between this line and the “stop editing” line. */
and
/* That’s all, stop editing! Happy publishing. */
Any code that is inserted between these comment lines will not break other functions already in the code.
If you’re not sure where to insert your code, and there aren’t specific instructions, you can usually add the code to the end of the file, before ?>. This should be alone on the last line of the file. ?> tells PHP that it has reached the end, so any code you add after it will not be read by WordPress.
8. If the code crashes your site or causes other problems, you probably had a formatting error. Open the “copy” file in the file manager that you made as a backup. Copy and paste that original code into the correct file. Go back to the curriculum to copy the snippet you need to add, and then consult the resource document of screenshots to repeat the above steps to ensure your code is formatted correctly.