How To Write A WordPress Plugin To Do “Random” Things
How To Write A WordPress Plugin To Do “Random” Things
Let’s face it, sometimes as developers we need to do ‘random’ things to organize data or put things in their correct places. While WordPress provides an easy to use admin interface, it can still be time consuming completing repetitive or bulk tasks. That’s why lots of people create plugins to make these tasks easier, quicker, and sometimes automated.
With many of my projects lately, I’ve found the need to perform small, random tasks on hundreds and sometimes thousands of posts. These tasks are ‘random’ to an extent meaning that there is not an existing plugin out there to do it for me and I will probably only need to do this task once. So what do I do? A while back I wrote just the shell of a plugin to do ‘random tasks’. Basically it’s just a menu item added to the WordPress admin panel and when you press the Process Posts button, it does whatever task you have programmed for it to do. It’s not fancy, it’s technical, but it works and it’s a great starting point if you’re a developer who needs to create a plugin to run a one-time ‘random’ task. So here it is:
<?php /** * Plugin Name: Do Random Stuff - WP Cover * Plugin URI: https://www.wpcover.com * Description: Custom plugin by Cabe to do random repetitive tasks when and where I need it to be done. The "Heavy Lifter" * Version: 1.0 * Author: Cabe Nolan * Author URI: https://www.boldcitydesign.com * License: GPL12 */ /*--------------------------------------------------- add settings page to menu ----------------------------------------------------*/ function add_rstuff_import_page() { add_submenu_page( 'tools.php', 'Do Random Stuff', 'Do Random Stuff', 'manage_options', 'import-rstuff', 'import_rstuff' ); } add_action( 'admin_menu', 'add_rstuff_import_page' ); function import_rstuff() { ?> <div class="wrap import-csv"> <h2>Do Stuff</h2> <p>Click Process button to do whatever is below in your run process.</p> <?php if ( isset( $_POST['_wpnonce-is-iu-import-users-users-page_import_cn'] ) ) { check_admin_referer( 'is-iu-import-users-users-page_import_cn', '_wpnonce-is-iu-import-users-users-page_import_cn' ); $args = array( 'posts_per_page' => -1, 'post_type' => 'weddings', 'post_status' => 'pending' ); $the_query = new WP_Query( $args ); if( $the_query->have_posts() ): while( $the_query->have_posts() ) : $the_query->the_post(); $i++; $parent_post_id = get_the_ID(); $my_post = array( 'ID' => $parent_post_id, 'post_status' => 'publish', 'post_date' => '1970-01-01 01:00:00' ); wp_update_post( $my_post ); endwhile; endif; wp_reset_query(); } ?> <form method="post" action=""> <?php wp_nonce_field( 'is-iu-import-users-users-page_import_cn', '_wpnonce-is-iu-import-users-users-page_import_cn' ); ?> <p class="submit"> <input type="submit" class="button-primary" value="Process Posts" /> </p> </form> <?php } ?>
Looking at the above code, what is it doing? Pretty simple really, when we click the Process Posts button which is on a sub-menu under Tools, it’s looping through all posts created under the ‘weddings’ post type that have a current post status of pending. It’s changing each of those posts to a post status of publish and it’s changing the publish date back to 1970.
There ya have it! Nothing crazy but a great plugin foundation if you need to whip up a quick one time task and run it on your current WordPress install.
Comments:
Share Your Thoughts