Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Change teams abbreviations into short names.
- */
- add_action( 'admin_init', function() {
- /**
- * Admin(s) only.
- */
- if ( ! current_user_can( 'manage_options' ) ) {
- return;
- }
- /**
- * Skip AJAX.
- */
- if ( wp_doing_ajax() ) {
- return;
- }
- /**
- * Teams with an abbreviation but not a short name.
- */
- $teams = get_posts( [
- 'post_type' => 'sp_team',
- 'numberposts' => -1,
- 'meta_query' => [
- [
- 'key' => 'sp_abbreviation',
- ],
- [
- 'key' => 'sp_short_name',
- 'value' => '',
- ],
- ],
- ] );
- if ( ! $teams ) {
- return;
- }
- $updated .= '';
- $i = 0;
- foreach ( $teams as $team ) {
- $abbreviation = get_post_meta( $team->ID, 'sp_abbreviation', true );
- if ( ! strlen( $abbreviation ) ) {
- delete_post_meta( $team->ID, 'sp_abbreviation' );
- continue;
- }
- $i++;
- $updated .= sprintf( '%03d. %s — %s<br>', $i, $team->post_title, $abbreviation );
- delete_post_meta( $team->ID, 'sp_abbreviation' );
- delete_post_meta( $team->ID, 'sp_short_name' );
- add_post_meta( $team->ID, 'sp_short_name', $abbreviation, true );
- }
- $title = 'teams abbreviations changed into short names';
- $output = '';
- $output .= sprintf( '<h2>%d %s</h2>', $i, $title );
- $output .= sprintf( '<p>%s</p>', $updated );
- $output .= '<p style="color: #dc3232; font-weight: bold">You should now delete the code from your installation, otherwise you won\'t be able to save abbreviations.<p>';
- $output .= sprintf( '<p><a href="%s">Go to teams page</a>', admin_url( 'edit.php?post_type=sp_team' ) );
- wp_die( $output, $title );
- } );
Add Comment
Please, Sign In to add comment