Using OpenAI to write a simple WordPress plugin
We wanted to test out the power of OpenAI. So we asked it to write a plugin for WordPress. OpenAI duly obliged with a code snippet to add to the functions.php of our child theme and a shortcode to use on any post or page.
It works by displaying the current date and time of the server this site is on in the United Kingdom. The problem is that we do not have control over where it is displayed. You can see the effect above the title on this post. It;s there, but not where we want it to be disaplyed. We want it to be inline, in a sentence for example.
This is the code that was generated by OpenAI.
/* Plugin Name: Display Date and Time Description: Display the current date and time Version: 1.0 Author: SD1.co.uk */ function wp_display_date_time() { echo date('l, F jS, Y \a\t g:ia'); } add_shortcode('display_date_time','wp_display_date_time');
So it works but we need to adjust the placement. Correcting it was easy, we just asked OpenAI to modify the plugin to display it inline.
Displaying the shortcode inline
The result is this: Saturday, December 21st, 2024 at 11:19am
That’s it, it works and this is the code generated. Feel free to copy and use it for your own projects.
/* Plugin Name: Display Date and Time (Inline) Description: Display the current date and time inline Version: 1.0 Author: SD1.co.uk */ function wp_display_date_time_inline() { return date('l, F jS, Y \a\t g:ia'); } add_shortcode('display_date_time_inline','wp_display_date_time_inline');
Once added to the child theme function.php file, all we have to do is pick out the shortcode ID which is contained in the last line and put it between square brackets, like this to create a shortcode we can use anywhere:
[display_date_time_inline]