Created
December 29, 2023 18:25
-
-
Save urosevic/4001f130f81f98c19654ac3db31a0c6b to your computer and use it in GitHub Desktop.
Script to make compatible with Head & Footer Code a bulk imported code into posts with CSV/XML file via WP All Import
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Head & Footer Code - Bulk Import Code Into Posts Fix | |
* | |
* Script to make compatible with Head & Footer Code | |
* a bulk imported code into posts with CSV/XML file via WP All Import | |
* | |
* Reference: https://wordpress.org/support/topic/bulk-import-code-into-posts-with-csv-xml-file/ | |
* | |
* @author Aleksandar Urošević <urke.kg@gmail.com> | |
* | |
* HOW TO USE? | |
* 1) Backup your database (or just `wp_postmeta` table). | |
* 2) Put this scipr to WordPress root. | |
* 3) Load this script in browser, wait a bit to conversion get finished. | |
* 4) When all is done, DELETE THIS SCRIPT FROM SERVER!!! | |
*/ | |
// Include WordPress bootstrap | |
require __DIR__ . '/wp-load.php'; | |
global $wpdb; | |
$converted = 0; | |
// Get all available post types (default and custom ones) | |
$post_types = get_post_types( array( 'public' => true ), 'names' ); | |
// Loop through each post type | |
foreach ( $post_types as $post_type ) { | |
// Get all posts with '_auhfc' post meta for the current post type | |
$results = $wpdb->get_results( | |
$wpdb->prepare( | |
" | |
SELECT meta_id, post_id, meta_value | |
FROM {$wpdb->postmeta} | |
WHERE meta_key = %s | |
AND post_id IN ( | |
SELECT ID | |
FROM {$wpdb->posts} | |
WHERE post_type = %s | |
) | |
", | |
'_auhfc', | |
$post_type | |
) | |
); | |
// Loop through each result | |
foreach ( $results as $result ) { | |
$post_id = $result->post_id; | |
$auhfc_data = $result->meta_value; | |
// Check if content is serialized array or plain JS script | |
if ( ! is_serialized( $auhfc_data ) ) { | |
// Print original content | |
echo "<p>Original _auhfc value for $post_type ID $post_id:</p><pre>" | |
. htmlspecialchars( $auhfc_data ) | |
. "</pre>\n"; | |
// Convert to PHP array | |
$plain_code = $auhfc_data; | |
// Convert post meta plain value to Head & Footer Code format | |
$converted_data = array( | |
'behavior' => 'append', // append || replace | |
'head' => $plain_code, // Plain code will be placed as HEAD code | |
'body' => '', | |
'footer' => '', | |
); | |
// Write back to _auhfc post meta | |
update_post_meta( | |
$post_id, | |
'_auhfc', | |
wp_slash( $converted_data ), | |
$auhfc_data | |
); | |
// Print final content | |
echo "<p>Final _auhfc value for $post_type ID $post_id:</p><pre>" | |
. htmlspecialchars( wp_slash( print_r( $converted_data, 1 ) ) ) | |
. "</pre>\n"; | |
// Increase number of converted items | |
++$converted; | |
} | |
} | |
} | |
if ( $converted ) { | |
echo "<p>Converted {$converted} postmetas.</p>"; | |
} else { | |
echo '<p>There was no postmetas for conversion.</p>'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment