2 minutes to read - Apr 12, 2023

Generate WordPress plugin

Create a custom WordPress plugin called "Big Daddy Media" that deletes images older than one year from the media library. OOP. Custom admin screen. one button delete. log the full URL of all deleted media files.

Here is an example of how you could create a custom WordPress plugin called "Big Daddy Media" that deletes images older than one year from the media library:

1. Create a new folder in the `wp-content/plugins` directory and name it `big-daddy-media`.

2. Create a new file in this folder and name it `big-daddy-media.php`. This file will contain the main code for your plugin.

3. At the top of the `big-daddy-media.php` file, add the plugin header information:

Copy code

` 'attachment', 'post_status' => 'inherit', 'date_query' => array( array( 'before' => '1 year ago', ), ), ) ); // delete the images from the media library foreach ( $older_than_one_year->posts as $image ) { $deleted = wp_delete_attachment( $image->ID, true ); if ( $deleted ) { // log the full URL of the deleted image error_log( wp_get_attachment_url( $image->ID ) ); } } }`

7. Use the `add_action` function to register the `admin_menu` action, which will be used to add a custom admin screen for your plugin. In the callback function for this action, use the `add_management_page` function to add a new menu item to the "Tools" menu in the WordPress admin dashboard.

Copy code

`add_action('admin_menu',`

loading...