Get the pen from codepen before inserting it in a WordPress article
If the pen already exits go directly to the Get your codepen pen in an iframe in order to insert it in a WordPress article section.
Create a pen on codepen
- Create a codepen account on the codepen platform: it is free! or log in your account if you already have one.
- Click on “Pen” button on the left side. A pen named “untitled” will appear.
- Give it a title and save.
- Click on the “Settings” button to add more… settings like a description and tags. Save.
- Code your example.
That is very simple to create a pen, isn’t it? Now, let’s see how to get this codepen pen as an iframe to be able to insert it in a WordPress article.
Get your codepen pen in an iframe in order to insert it in a WordPress article
Note: The below steps are available whether you are connected to your codepen account.
- Go to the requested pen on codepen.
- At the bottom of the webpage, on the right side, there buttons like “Delete”, “Add to collection”, “Fork”… Click on the “Embed” button.
- Then, click on the “HTML (Recommended)” tab on the modal box which has appeared.
- Click on the “Copy Code” button on the top right side, above the HTML code.
Okay, we have copied the code to be inserted. Now, we have to paste it in the right place.
Insert the codepen pen in the WordPress article your are writing
Note: We suppose that you are already connected to the WordPress interface. In case, you are not, log in your blog and go to your article (in the dashboard, Posts > All > select the specific article > Edit).
- In the right menu bar, click on the three dots.
- Then, click on “Code Editor“.
- In the article, in HTML version which has appeared, look for the exact place your pen should be displayed. Click there.
- Paste.
- Save by clicking on the “Save Draft” ou “Update” or “Schedule” button on the top right of the screen, depending on the article status.
- You can click on the “preview article” or “view article” link to control how the pen is displayed. Note: if the iframe is not displayed go directly to the last section of this article.
- Do not forget to click on Exit Code Editor at the top of the article or Visual Editor on the right menu bar to go back to the classic editor to end the writing of your article.
The code has been inserted and the iframe is displayed in the article but is the result exactly the one you expected? Some adjustments can be applied; discover in the next section how to modify the height or the position of the iframe in the WordPress article.
Modify the aspect of the codepen iframe in a WordPress article
Modify the height of the codepen iframe in a WordPress article
The height of the codepen iframe is 300px by default. sometimes we could want to modify it to show more of the result (see how to modify the codepen iframe height before copying its code) or to respect a specific height (see how to modify the codepen iframe height after having inserted it in the article code).
Modify the height of the codepen iframe before copying the iframe code
- In the modal box which appears after clicking on the “Embed” button (step 2 of the Get your codepen pen in an iframe in order to insert it in a WordPress article), move (click and move the mouse) the bottom of the displayed result via the anchor until you obtain the correct : the height measurement is modified to correspond the new preview.
- Go back to the step 3 of the Get your codepen pen in an iframe in order to insert it in a WordPress article.
This solution gives the possibility to choose what to show, the entire result for example. But, what if we want to get exactly the same height on all the codepen iframes and that height is not the standard one written in the given code (I mean 300px)? Let’s find a second solution below.
Give a specific height measurement to a codepen iframe
Giving every time the same height measurement to a codepen iframe is not difficult. As indicated in the introduction, it is possible to modify the codepen iframe height directly in its code.
Note: You have already pasted the code in the article when being in the Code Editor mode.
- In the code Editor mode, look for the iframe code.
- In the iframe code, look for the “data-height” attribute.
- Modify the “300” value to anyone you prefer. Note: the value is in pixels.
- Save the article.
- Go to the preview if you want to control the final aspect.
- Go back to the classic mode by clicking on “Exit Code Editor” or “Visual Editor”.
One example of the iframe code proposed by codepen after data-height modification.
<span>See the Pen <a href=”https://codepen.io/coding-tricks/pen/qBJzQLV”>
HTML elements with a negative CSS order value are displayed first</a> by Coding-Tricks (<a href=”https://codepen.io/coding-tricks”>@coding-tricks</a>)
on <a href=”https://codepen.io”>CodePen</a>.</span>
</p>
<script async src=”https://cpwebassets.codepen.io/assets/embed/ei.js”></script>
Modify the aspect of the displayed codepen iframe in a WordPress article
You have already fixed the codepen iframe height problem, to modify the width or the position (left, center, right, margin, padding…) of the iframe you will have to write a little bit of CSS code.
A trick to be sure of the element to select to apply the CSS rules:
- Preview the article with the inserted iframe.
- Click right on the iframe and select Inspect.
- In the Inspector, have a look at the HTML code and choose the best selector depending on which element you need to modify. As you can see the iframe is inserted in a div.
Then, write CSS rules:
- in the theme customizer if you are not the one who have written the code of the theme,
- in the custom theme CSS file if you have created that custom theme!
Damn! The codepen iframe result is not displayed in the WordPress article
This is the last trick to display a codepen pen in a WordPress article.
If the pen is not displayed but a link to it is, the cause is that the <script>
element at the end of the code has not been copied and paste. Have a look to the article in the Code Editor mode and control if there is a script included in the iframe code you have pasted. If there is not, just re-do all the steps and control that the script has been pasted this time.
Maybe there are some other bugs but I had to face that one and found that cause.
In conclusion
Finally, insert a codepen pen as an iframe in WordPress articles is very easy. So do not hesitate to use pens as examples in your next articles.
In the next episode of this series “Insert elements when writing a WordPress article”, I will give some tricks to insert code examples in your WordPress articles.