Using OpenAI to write simple plugins
Following on from our first experiment in using OpenAI to write a date and time plugin we thoght we’d have a look at some simple plugins we could ask it to write for us.
This website has 83 published posts
The above example demonstrates how easy it is to add a shortcode to a heading displaying the current number of published posts on this website.
We simply asked OpenAI to: “Write a wordpress plugin and shortcode to display the total number of published posts on a website“
This is the code it generated. Feel free to add use it by adding it to your child theme functions.php file and using the shortcode below:
/* * Plugin Name: WP Total Posts * Description: Display total number of posts published on a website * Version: 1.0 * Author: SD1.co.uk */ // Create a shortcode to display the total number of published posts function wp_total_posts_shortcode() { $total_posts = wp_count_posts(); $published_posts = $total_posts->publish; return $published_posts; } add_shortcode( 'wp_total_posts', 'wp_total_posts_shortcode' );
This is the shortcode:
[wp_total_posts]
It has been added in the heading like this:
This website has [wp_total_posts] published posts
Display a random post with heading, link to the post, image and date published
We asked OpenAI to “Write a plugin to display a random post with title, date published and a thumbnail image, with a link to the post”
This is the result. It’s basic but it works. If you refresh the page you will get a random post. We’ll have a look at refining this in a later post.
How to optimise images in WordPress
February 4, 2023
This is the code that was generated. Simply add it to your child theme functions.php file and using the shortcode below:
/* Plugin Name: Random Post Display Description: Display a random post with title, date published and a thumbnail image, with a link to the post. Version: 1.0 * Author: SD1.co.uk */ // Function for the shortcode function random_post_display_shortcode() { // Arguments for the query $args = array( 'post_type' => 'post', 'posts_per_page' => 1, 'orderby' => 'rand', ); // The Query $random_post_query = new WP_Query( $args ); // Check if the query has posts if ( $random_post_query->have_posts() ) { // Start the markup $output = '<div class="random-post">'; // Loop through the posts while ( $random_post_query->have_posts() ) : $random_post_query->the_post(); // Get the post thumbnail $post_thumbnail = get_the_post_thumbnail( get_the_ID(), 'thumbnail' ); // Add the title to the markup $output .= '<h3><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></h3>'; // Add the thumbnail to the markup $output .= '<p>' . $post_thumbnail . '</p>'; // Add the date to the markup $output .= '<p>' . get_the_date() . '</p>'; endwhile; // Reset post data wp_reset_postdata(); // Close the markup $output .= '</div>'; // Return the markup return $output; } } // Hook the shortcode function to the init action add_action( 'init', 'random_post_display_shortcode' ); // Register the shortcode add_shortcode( 'random_post', 'random_post_display_shortcode' );
This is the shortcode used to genrate the random post.
[random_post]
List all categories with a post count in a table
Taking things a little further we asked OpenAI to “Write a wordpress plugin to link to and display a list of all the categories on this website with a count of all the posts in each category in a table with headers”
This is the result:
This is the code that was generated. Simply add it to your child theme functions.php file and using the shortcode below:
/* Plugin Name: Display All Categories Description: Display all categories with a post count Version: 1.0 Author: SD1.co.uk */ // Create the shortcode function display_all_categories_shortcode() { // Get all categories $categories = get_categories(); // Create the table $output = '<table>'; $output .= '<thead>'; $output .= '<tr>'; $output .= '<th>Category</th>'; $output .= '<th>Post Count</th>'; $output .= '</tr>'; $output .= '</thead>'; $output .= '<tbody>'; // Loop through all categories foreach ( $categories as $category ) { // Get the post count $post_count = $category->category_count; // Output the row $output .= '<tr>'; $output .= '<td><a href="' . get_category_link( $category->term_id ) . '">' . $category->name . '</a></td>'; $output .= '<td>' . $post_count . '</td>'; $output .= '</tr>'; } $output .= '</tbody>'; $output .= '</table>'; return $output; } add_shortcode( 'display_all_categories', 'display_all_categories_shortcode' );
This is the shortcode you need to display it on your post or page:
[display_all_categories]