WOW! I made a properly “magic method” WordPress plugin that allows you to just write a function name, and have that function created for you by an AI. It just works! (Some of the time).
I’ve been trying really hard not to do this because literally everyone else has. But this is a post about “AI” generated content.
To start with, I’m skeptical about all the AI hype, which is why I’m reluctant to use it and post about it. More on this another time.
But… Caleb Porzio – maker of my beloved Alpine.js and Wizard number 2 in my personal Laravel community rankings – posted this:
If you don’t want to watch the video (or if Twitter has, in the meantime, died) Caleb makes a Laravel “Ai” helper class that lets him write things like:
echo Ai::beforeFirstDot('caleb.porzio');
and it generates the function from the name, stores the function in a file so that it doesn’t have to be regenerated in future, and runs it!
Caleb himself notes that “this is just a random toy I made for fun.” – and I get that.
But for some reason it lodged itself in my brain. And when I finally decided that I wanted to play with the OpenAI API myself, I had the idea: “What if we did this in WordPress?”
A WordPress AI function generator!
A few hours hacking later, and this exists.
I can type:
WPAI::add_is_awesome_to_all_these_strings(
strings: ['WordPress', 'Ross', 'AI']
);
and the AI will create me the function and run it:
[
"WordPress is awesome",
"Ross is awesome",
"AI is awesome"
]
It made this function and added it to a file in a mu-plugin:
function add_is_awesome_to_all_these_strings ( array $strings ) {
foreach ( $strings as &$string ) {
$string .= ' is awesome';
}
return $strings;
}
That’s not how I’d have written that function, but it works.
It knows about WordPress. You can write:
WPAI::get_an_array_of_all_post_types();
And it will return something like:
[
'post' => "post",
'page' => "page",
'attachment' => "attachment"
]
Because it created:
function get_an_array_of_all_post_types ( ) {
$post_types = get_post_types(
array( 'public' => true ),
'names'
);
return $post_types;
}
I mean… this is pretty impressive.
It can write WP queries too. I wrote a special generator for that. You can type:
WPAI::query(
'3 latest posts from the news post type that
have the metadata test=true and that do not
have a meta key called "ignore"'
);
and it will run:
get_posts([
'post_type'=>'news',
'posts_per_page'=>3,
'meta_query'=>[
[
'key'=>'test',
'value'=>'true',
'compare'=>'='
],
[
'key'=>'ignore',
'compare'=>'NOT EXISTS'
]
]
]);
So you can kind-of sorta build a website just by writing what you want. Like this:
WPAI::register_recipe_post_type();
WPAI::register_recipe_type_taxonomy_for_recipe_type();
WPAI::register_acf_repeater_field_for_ingredients_for_recipe_type();
Does this work? Absolutely it does! With a few glitches here and there (that I think I can fix). Here’s the three minutes of me building this code:
The Big Buts
This really, totally does work. Here’s the code. About 200 lines of PHP.
(It’s currently pretty rough, my “prompt engineering” is probably not very good, and it needs a README to tell you how to use it. But that’s it.)
But…
DON’T DO THIS!!!!!
“What?” I hear you cry.
DON’T DO THIS!!! THIS IS REALLY DANGEROUS AND STUPID!!!
You run this code ENTIRELY AT YOUR OWN RISK!!!
I take no responsibility for you website being broken beyond repair; your laptop or server being wiped of all files; or ANYTHING that might happen as a result of you using this code.
This code tells an AI of dubious trustworthiness to generate more code and then runs the more code on your computer.
You should not do this.
And if you DO do it, do it on a temporary VPS, or a walled-off temporary site, or an instawp.com site or something. And you do that – and I can’t say this strongly enough – entirely at your own risk.
Some thoughts on all this
This seems very cool. But it is a fun toy or experiment. Nothing more. Don’t use this in production.
Will we write code like this one day? Maybe.
I’m not convinced the AIs are coming for us yet. These are fun toys to play with the large language models and cool tech. People will use them. And they seem helpful.
We are at the early stages of having these things around. I suspect they have the power to transform how we do certain kinds of work. They will inevitably impact society – in my opinion. But we don’t understand them well enough yet.
For me…
I’ve now tinkered with generative code AI APIs.
And I find myself in the position of thinking: “I’m not entirely happy with this technology existing, but I’ve played with it, and it’s really fun!”
And the scary thing is: this is where most people started with crypto and NFT’s. And I didn’t like where that went AT ALL.
I will wait and see.