Guest User

Untitled

a guest
May 26th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Change teams abbreviations into short names.
  5. */
  6. add_action( 'admin_init', function() {
  7.  
  8. /**
  9. * Admin(s) only.
  10. */
  11. if ( ! current_user_can( 'manage_options' ) ) {
  12. return;
  13. }
  14.  
  15. /**
  16. * Skip AJAX.
  17. */
  18. if ( wp_doing_ajax() ) {
  19. return;
  20. }
  21.  
  22. /**
  23. * Teams with an abbreviation but not a short name.
  24. */
  25. $teams = get_posts( [
  26. 'post_type' => 'sp_team',
  27. 'numberposts' => -1,
  28. 'meta_query' => [
  29. [
  30. 'key' => 'sp_abbreviation',
  31. ],
  32. [
  33. 'key' => 'sp_short_name',
  34. 'value' => '',
  35. ],
  36. ],
  37. ] );
  38.  
  39. if ( ! $teams ) {
  40. return;
  41. }
  42.  
  43. $updated .= '';
  44. $i = 0;
  45. foreach ( $teams as $team ) {
  46. $abbreviation = get_post_meta( $team->ID, 'sp_abbreviation', true );
  47. if ( ! strlen( $abbreviation ) ) {
  48. delete_post_meta( $team->ID, 'sp_abbreviation' );
  49. continue;
  50. }
  51. $i++;
  52. $updated .= sprintf( '%03d. %s &mdash; %s<br>', $i, $team->post_title, $abbreviation );
  53. delete_post_meta( $team->ID, 'sp_abbreviation' );
  54. delete_post_meta( $team->ID, 'sp_short_name' );
  55. add_post_meta( $team->ID, 'sp_short_name', $abbreviation, true );
  56. }
  57.  
  58. $title = 'teams abbreviations changed into short names';
  59.  
  60. $output = '';
  61. $output .= sprintf( '<h2>%d %s</h2>', $i, $title );
  62. $output .= sprintf( '<p>%s</p>', $updated );
  63. $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>';
  64. $output .= sprintf( '<p><a href="%s">Go to teams page</a>', admin_url( 'edit.php?post_type=sp_team' ) );
  65.  
  66. wp_die( $output, $title );
  67. } );
Add Comment
Please, Sign In to add comment