WordPress plugin to update values of custom fields from CSV input
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

115 строки
2.8 KiB

  1. <?php
  2. /*
  3. Plugin Name: Meta Updater
  4. Plugin URI:
  5. Description: Bulk insert and update custom fields
  6. Version: 0.1
  7. Author: Adrian Short
  8. Author URI: https://adrianshort.org/
  9. License: CC0/public domain
  10. */
  11. add_action( 'admin_menu', 'as_metaupdater_menu' );
  12. // Add submenu to Tools menu
  13. function as_metaupdater_menu() {
  14. add_submenu_page(
  15. 'tools.php', // top-level handle
  16. 'Meta Updater', // page title
  17. 'Meta Updater', // submenu title
  18. 'manage_options', // capabilities
  19. 'as_metaupdater', // submenu handle
  20. 'as_metaupdater_page' //function
  21. );
  22. }
  23. function as_metaupdater_page() {
  24. if ( ! current_user_can( 'manage_options' ) ) {
  25. wp_die("You do not have sufficient permissions to access this page.");
  26. }
  27. ?>
  28. <div class="wrap">
  29. <h2>Meta Updater</h2>
  30. <?php
  31. if ( 'POST' == $_SERVER['REQUEST_METHOD'] ) {
  32. foreach( explode( PHP_EOL, $_POST['data'] ) as $row ) {
  33. $data[] = str_getcsv( $row );
  34. }
  35. echo "<pre>";
  36. print_r( $data );
  37. echo "</pre>";
  38. $header_row = array_shift( $data );
  39. $search_field = $header_row[0];
  40. $replace_field = $header_row[1];
  41. print_r( $header_row );
  42. print_r( $data );
  43. echo "Search: $search_field, replace: $replace_field";
  44. echo "<h3>Results</h3>";
  45. $success = 0;
  46. foreach( $data as $row ) {
  47. $args = array(
  48. 'meta_key' => $search_field,
  49. 'post_type' => 'site'
  50. );
  51. if ( is_string( $row[0] ) ) {
  52. $args['meta_value'] = $row[0];
  53. } elseif ( is_numeric( $row[0] ) ) {
  54. $args['meta_value_num'] = $row[0];
  55. }
  56. echo "<p>";
  57. print_r( $args );
  58. $query = new WP_Query( $args );
  59. if ( $query->have_posts() ) {
  60. while ( $query->have_posts() ) {
  61. $query->the_post();
  62. echo get_the_title();
  63. if ( $_POST['dry_run'] != '1' ) {
  64. echo "Updating meta for real";
  65. if ( update_post_meta( get_the_ID(), $replace_field, $row[1] ) ) {
  66. $success++;
  67. }
  68. }
  69. }
  70. } else {
  71. echo "No results<br>";
  72. }
  73. }
  74. echo "<h3>Saved $success terms.</h3>";
  75. }
  76. ?>
  77. </div>
  78. <form method="POST" action="">
  79. <p>
  80. Paste in CSV data: column 1 holds the key field and column 2 holds the value. The first row must be a header row. Every post with the field set to the value in column 1 will have a field added or updated with the value in column 2.
  81. </p>
  82. <textarea name="data" rows="15" cols="60">
  83. openlylocal_id,area_covered
  84. 2192,"All of London"
  85. 2191,"Greater Manchester"
  86. </textarea>
  87. <p><input type="checkbox" name="dry_run" value="1" checked="checked" /> <label for="dry_run">Dry run - leave the database unchanged</label></p>
  88. <p><input class="button-primary" type="submit" value="Update Custom Fields" /></p>
  89. </form>
  90. <?php
  91. }