Using ChatGPT to write simple plugins

My challenge: Both volunteer and entrepreneur plugin programmers often, perhaps for quite understandable reasons, abandon their creations, leaving their users scrambling for replacements if and when the particular plugin stops working or gets suspiciously not-updated-for-several-years old.

My solution: I have started to use ChatGPT to write simple plugins to replace some of my ancient ones. Many plugins have a large toolkit of features that I don’t need, so I am focusing on writing plugins that just do the one thing I need. (I also opened up a private account on OPENAI.COM so I can run many experiments for an estimated low cost.) ChatGPT might turn out to be a significant resilience resource for the ClassicPress community as some plugins from the WP4.9 & prior era stop working. I have experienced two big site reconstructions forced on me by plugin conk-outs.

My most successful ChatGPT experiment to date is a simple shortcode to display the content of a custom field. (my abbreviation for Display Custom Field = dcf )

The shortcode use looks like this and so far it works fine:

[dcf-by-id postid=“123” fieldname=“myfield”]

The code looks like this:

<?php /* Plugin Name: DCF by ID Description: Shortcode to display contents of any custom field on any page of a wordpress site, using the Wordpress postid and custom field name as the way to locate the specific custom field content desired. Version: 1.0 Author: Dennis Rivers with help of ChatGPT License: Creative Commons CC0 1.0 Universal Public Domain Dedication Disclaimer of All Warranties: The authors are not responsible for any damages or losses that may be incurred as a result of using this code. User accepts full responsibility for use. Shortcode Style of Use: [dcf-by-id postid="123" fieldname="myfield"] */ ``` function dcf_by_id_func($atts) { $atts = shortcode_atts( array( 'postid' => '', 'fieldname' => '', ), $atts); $postid = $atts['postid']; $fieldname = $atts['fieldname']; $content = get_post_meta($postid, $fieldname, true); return $content; } add_shortcode('dcf-by-id', 'dcf_by_id_func'); ?>

[details="Summary"]
This text will be hidden
[/details]

-----------------------------------------
more discussion:

I included the post id number as a parameter because it makes the shortcode usable in a wider variety of circumstances and locations on a site's pages and other plugins.

Right now I am not a savvy enough php programmer to be able to check the ChatGPT-generated code for sleeping errors or security vulnerabilities. So that means I am going to just use them on my own several websites. 

But I can`t help imagining that in the hands of people with more programming skill than I, this would help the ClassicPress community develop the thousands of plugins that make for a full-fledged CMS.  It could be a step in solving the "Not compatible with ClassicPress" problem that arises when scrolling down the list searching for a plugin to perform a specific function.

What do you think?  Please send comments and suggestions!

Thanks,  Dennis Rivers

If ChatGPT can help solve the compatibility problems (PHP version, security issues, deprecations) between CP and WP, it could be a good thing.
But having it write simple code and then not knowing if it’s secure seems like a waste of time.
There are thousands of plugins in the WP repo that have been reviewed for security. I think it would be better to start with those and change the things you need to change. (There are several that provide shortcodes for all sorts of data.)
I suggest you don’t reinvent the wheel. Just smooth the rough edges off the old ones.

(A very old plugin gives the bare bones to start new plugins: Pluginception)

2 Likes

Dear Joyously, I hope 2023 is a great year for you. ~~~~~ Thanks so much for exploring this topic with me. About 2/3 of what ChatGPT writes for me doesn’t work. The remaining third is very interesting. I am getting better at carefully specifying what I want and as much as I know about how to create it. Also you can ask it to try several time and pick the best effort. ~~~~ In my case, I absolutely want to “reinvent the wheel” because I have had several important wheels (plugins) yanked out from underneath me. So it is a control and organizational security issue for me, working for nonprofits. Also, your answer points to one of my problems: Yes, there are thousands of plugins out there, but I don’t have time to sort through them in the hope that what I need might be out there. So I am really interested in other options. ~~~~ I will experiment with the security issue with ChatGPT itself. It just wrote me a little shortcode to change the background image of a given page. I will go back and say something like “rewrite this plugin, applying all recognized and applicable techniques to improve its operational security. note and explain the improvements you made.” We will see what happens. All the best, Dennis Rivers

We don’t know what everyone’s situation is. Not everyone can afford to pay a developer, even single digits. Not everyone has the skills to write or even modify plugins/code. To each is their own.

A healthy, friendly discussion and disagreement are good, let’s keep it that way and on topic. Let’s not stray into personal attacks.

3 Likes

The idea of an IA writing code is not new.
When I use VSCode several things like autocomplete and code formatting help me write my code in a better way.
Using ChatGPT to completely write a plugin might be “wrong” for the reasons @joyously explained before (like, what if the code it outputs it’s not clean or secure?) but certainly using it to have multiple iterations to compare and combine together as an inspiration can be good. I mean, I myself look up on google when I need something and I end up reading many coding tutorials and sometimes writing code that is the result of carefully mixing things together and this can be done also starting from ChatGPT answers.
The important thing IMHO is to take the bot’s answers and discern what they are and improve on them and eventually come up with a solution derived/inspired from them and not use them as they come out of the bot.
That way the code is yours, you have only used the automation to speed up and polish your process.

1 Like

ChatGPT is useful for things like functions.php snippets. Most simple WP plugins can be reduced to a functions.php code snippet anyway - - its only when the code snippet starts exceeding around 100 lines of code that it starts making sense to split it up into include files, add media assets etc…

IMO just having a forum thread dedicated to “functions.php snippets” would be useful, a place where people can contribute their own scripts.

1 Like

Me thinks this is going to be handled by the new plugin/the directory. There were discussions about a section for snippets. This section ensures snippets are checked before uploading it to the dir so they are safe to use. I don’t know if it will be included from the start or after some time but it was in the works.

1 Like

This actually might be the solution we will adopt. We’ve had discussions about the snippet library, but the end result was getting too complex with too many variables to be implemented at this time.

We thought we will include it, but the implementation complexities made us put it on indefinite hold for now. Our priority is getting plugin/theme directory fully functional and creating integration with the core, which is critical to our ecosystem. With not enough developers to get everything done in a timely manner, we had to put things on the back burner to avoid holding up the entire project.

I’m thinking to bring more snippets to the documentation site, so they are solutions to user problems. Such as “How do I disable lazy loading?”. But a forum category for sharing snippets would be a good option, we can pull good snippets into the documentation site as we go.

4 Likes

This seems a viable solution also. We could have a section of the docs where snippets are searchable in the future also like a sort of knowledge base where one can find them by taxonomy.

3 Likes

Sounds great! Looking forward to this :slight_smile:

2 Likes

ChatGPT has proven to be a good partner for debugging and checking code, of course you shouldn’t blindly trust the results, but ChatGPT also explains very clearly and also points out that it’s not perfect, the code is a possibility and you still are should look over it.
A great and instructive support for newcomers, I also turn to ChatGPT from time to time and have to say it saves me a lot of time when I have lost the overview myself.

2 Likes