You may know it (or not), we am also responsible for creating a free WordPress website tutorials, tips and computer help. The purpose of this site (also in WordPress) is to provide free resources for less advanced computer users in helping them better understand digital tools (computer but also smartphone or tablet touch).
We recently encountered a problem with one of our articles that allows displaying the IP address of the visitor. We have recently been using an ad optimization service that serves as a sort of proxy between the user and the visitor. The article that previously displayed the visitor’s IP address via a WordPress extension displayed the relay’s IP address, hosted by Amazon Web Services (Amazon’s cloud services and products). With the WordPress Text Effects Plugin you can have the best option now.
We finally managed to find a technical solution (described below for those interested) to display the visitor’s IP. But new problem! We had to do without the plugin used so far and use a custom PHP code that we had to insert in my article. It was the first time that we wanted to inject PHP code directly into my article. And we did not know how to do it. Fortunately; we found a simple solution to implement using a WordPress snippet and a short code for WordPress.
Insert PHP code into a page, widget or WordPress article
We first came across old articles that dated 5-6 years ago and explained that you could easily use PHP code in a page or a WordPress article by putting the PHP code via the appropriate short code: PHP] [/ PHP] . Unfortunately, it did not work. We just had on the screen the short code displayed in the article without displaying the IP address retrieved by my script.
We also saw that there were extensions to use PHP code in WordPress pages / articles / widgets like Woody ad snippets, PHP Everywhere or Insert PHP Code Snippet. But go through a plugin to use PHP did not like me, especially because many of these modules were not up to date and incompatible with my version (up to date, it) of WordPress.
So we found an alternative solution by rummaging through the web: create a snippet in the functions.php file of my WordPress template and display the result via a custom short code.
Easily insert PHP into your WordPress pages
Here’s how to insert PHP code into your widgets / articles / WordPress pages:
- Create your custom function in the functions.php file of your WordPress theme.
- Assign the result of your function to a short code via the add_shortcode WordPress function. For English documentation on this reference function: https://codex.wordpress.org/Function_Reference/add_shortcode.
- Use your custom short code in your page / widget or WordPress article.
- Finished the theory, now let’s go to practice with the example of creating a snippet to display the IP of a visitor and its associated short code.