WordPress plugin to update values of custom fields from CSV input
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

metaupdater.php 3.2 KiB

9 år sedan
9 år sedan
9 år sedan
9 år sedan
9 år sedan
9 år sedan
9 år sedan
9 år sedan
9 år sedan
9 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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, stripslashes( $_POST['data'] ) ) as $row ) {
  33. $data[] = str_getcsv( $row, ",", '"' );
  34. }
  35. // Get header row
  36. list( $search_field, $replace_field ) = array_shift( $data );
  37. echo "<h3>Results</h3>";
  38. $success = 0;
  39. $not_found = [];
  40. foreach( $data as $row ) {
  41. list( $key, $value ) = $row;
  42. if ( $key == '' ) continue; // skip blank lines
  43. $args = array(
  44. 'meta_key' => $search_field,
  45. 'post_type' => 'site'
  46. );
  47. // if ( is_string( $key ) ) {
  48. $args['meta_value'] = $key;
  49. // } elseif ( is_numeric( $key ) ) {
  50. // $args['meta_value_num'] = $key;
  51. // }
  52. echo "<p>";
  53. $query = new WP_Query( $args );
  54. if ( $query->have_posts() ) {
  55. while ( $query->have_posts() ) {
  56. $query->the_post();
  57. if ( $_POST['dry_run'] != '1' ) {
  58. if ( update_post_meta( get_the_ID(), $replace_field, trim( $value ) ) === true ) {
  59. echo "<pre>";
  60. print_r( $args );
  61. echo sprintf( "ID: %d<br>%s: %s<br>Title: %s<br>%s: %s", get_the_ID(), $search_field, $key, get_the_title(), $replace_field, $value );
  62. $success++;
  63. }
  64. }
  65. }
  66. } else {
  67. $not_found[]= $key;
  68. echo "No results<br>";
  69. }
  70. }
  71. echo "<h3>Updated $success custom fields OK.</h3><hr>";
  72. echo "<h3>Errors</h3>";
  73. if ( count( $not_found) > 0 ) {
  74. echo "<p>Posts with the custom field <strong>$search_field</strong> with these values could not be found:</p>\n<ul>";
  75. foreach( $not_found as $error ) {
  76. echo "<li>$error</li>\n";
  77. }
  78. echo "</ul>";
  79. } else {
  80. echo "<p>None.</p>";
  81. }
  82. }
  83. ?>
  84. </div>
  85. <form method="POST" action="">
  86. <p>
  87. 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.
  88. </p>
  89. <textarea name="data" rows="15" cols="60">
  90. openlylocal_id,area_covered
  91. 2192,"All of London"
  92. 2191,"Greater Manchester"
  93. 99999,dummy
  94. </textarea>
  95. <p><input type="checkbox" name="dry_run" value="1" checked="checked" /> <label for="dry_run">Dry run - leave the database unchanged</label></p>
  96. <p><input class="button-primary" type="submit" value="Update Custom Fields" /></p>
  97. </form>
  98. <?php
  99. }