jeroenroodnl
Forum Replies Created
-
Second update
I managed to get it to work. In fact, it was quite simpler than I imagined. This sequence is valid, but it has two errors:
$pods_post_name = "showcase"; // error #1 // ... $pod = pods( $pods_post_name, $post_id); $pod_id = $pod->id(); $field = $pod->fields($pods_field_name); $field_id = $field->id(); // error #2
- I used the wrong Pod name.
- Obtaining the field ID was not by means of a method called
id()
, but rather the propertyid
of the field object, returned by thefields()
method of a Pod.
The updated code block would look like this:
<?php function wpai_pods_image_imported( $post_id, $att_id, $filepath, $is_keep_existing_images = '' ) { global $wpdb; $pods_field_name = 'fotogallerij'; $pods_post_name = "project"; // updated $existing = get_post_meta($post_id, '_pods_' . $pods_field_name, true ); if (strpos($existing, ":".$att_id.";" ) !== false) return; $pod = pods( $pods_post_name, $post_id); $pod_id = $pod->id(); $field = $pod->fields($pods_field_name); $field_id = $field->id; // updated $table_name = $wpdb->prefix . "podsrel"; $wpdb->insert( $table_name, array( "pod_id" => $pod_id, "field_id" => $field_id, "item_id" => $post_id, "related_item_id" => $att_id ) ); preg_match_all("/i:(\d+);i:(\d+);/mi", $existing, $matches); $images = $matches[0]; array_push($images,"i:".count($images).";i:".$att_id.";"); $meta = "a:".count($images).":{".implode("",$images)."}"; update_post_meta($post_id, '_pods_' . $pods_field_name,$meta, $existing ); } add_action( 'pmxi_gallery_image', 'wpai_pods_image_imported', 10, 4 ); ?>
…this works quite well. Admittedly, I don’t understand the Pods internals to fully make use of it. Maybe there is a method for more easily adding content to a Pods field programmatically. But this is an import-step, ideally only run once.
- This reply was modified 6 months, 1 week ago by jeroenroodnl.
FWIW: here’s my progress so far! I am getting close, but I am not there yet.
Someone else, in the community of WP All Import, has had a similar problem: trying to import an exported Pods Gallery (which is referred to as the “File / Image / Video” field). Here’s the link:
https://www.ads-software.com/support/topic/wp-all-import-pods-gallery-field/I set out to try and reverse-engineer it a little, so I could use approach that the WP All Import support admin suggested.
Step 1: interpreting what’s in a gallery field’s value.
I was diving into the database, and learned the following:A Pod’s gallery field is stored in two ways:
- In
wp_postmeta
under the record with key_pods_<YOUR_FIELD_NAME>
, as a collection of images - In
wp_podsrel
, stored per image.
The actual data looks as follows:
- In
wp_postmeta
, the data is formatted as:a:X:{i:0;i:YYYY;i:1;i:ZZZZ}
. What does this mean?a:X
means that the data is to be interpreted as an array, whereX
is the amount of items in the array.- The preceeding
i:0
andi:1
are the keys of the array items (zero-based counting). - The succeeding
i:YYYY
andi:ZZZZ
are the array values, whereYYYY
andZZZZ
are the IDs of the actual uploads.
- In
wp_podsrel
, you have a few columns:pod_id
, which is the ID of the Pod you’ve created to include a custom field into.field_id
, which is the ID of the field you’ve created.item_id
, which is the ID of the specific Post.related_item_id
, which is the ID of the actual upload.
Step 2: trying to hook into the lifecycle of the WP All Import process
How do I hook into the WP All Import lifecycle? I learned through the aforementioned post that there is a function that will be called after every image import. This could be useful for “enhancing” the database once an image is imported:wpai_pods_image_imported( $post_id, $att_id, $filepath, $keep_existing_images)
What does this function receive?
$post_id
, which is the ID of the Post that WP All Import is currently importing.$att_id
, which is the ID of the upload which has been created.$filepath
hasn’t been used in the example, but I reckon that it is the actual file path to the upload.$is_keep_existing_images
hasn’t been used either, but I think it reflects the settingkeep existing media files in Media Library
.
Step 3: trying to replicate the data stored by Pods
So, in order to get the data correctly in Pods so that images are actually put into the Gallery field, the WP All Import admin suggested a piece of code, that did not actually work for me. I adapted it slightly:function wpai_pods_image_imported( $post_id, $att_id, $filepath, $is_keep_existing_images = '' ) { $pods_field_name = 'fotogallerij'; $pods_post_name = "showcase"; $existing = get_post_meta($post_id, '_pods_' . $pods_field_name, true ); if (strpos($existing, ":".$att_id.";" ) !== false) return; preg_match_all("/i:(\d+);i:(\d+);/mi", $existing, $matches); $images = $matches[0]; array_push($images,"i:".count($images).";i:".$att_id.";"); $meta = "a:".count($images).":{".implode("",$images)."}"; update_post_meta($post_id, '_pods_' . $pods_field_name,$meta, $existing ); } add_action( 'pmxi_gallery_image', 'wpai_pods_image_imported', 10, 4 );
This only imports the data into the
wp_postmeta
table. The result is that there the gallery is still empty in the admin UI, and neither in the instances where the Pod field is displayed. Onwards to the next step, which isn’t yet successful.Step 4: getting the data into
wp_podsrel
I set out to navigate my way through the Pods documentation, but some pages are blank. Also, I can’t really deduce what data is given through most of the method calls, or what some of the calls require.
The code below is still a bit problematic.<?php function wpai_pods_image_imported( $post_id, $att_id, $filepath, $is_keep_existing_images = '' ) { global $wpdb; $pods_field_name = 'fotogallerij'; $pods_post_name = "showcase"; $existing = get_post_meta($post_id, '_pods_' . $pods_field_name, true ); if (strpos($existing, ":".$att_id.";" ) !== false) return; $pod = pods( $pods_post_name, $post_id); $pod_id = $pod->id(); $field = $pod->fields($pods_field_name); $field_id = $field->id(); $table_name = $wpdb->prefix . "podsrel"; $wpdb->insert( $table_name, array( "pod_id" => $pod_id, "field_id" => $field_id, "item_id" => $post_id, "related_item_id" => $att_id ) ); preg_match_all("/i:(\d+);i:(\d+);/mi", $existing, $matches); $images = $matches[0]; array_push($images,"i:".count($images).";i:".$att_id.";"); $meta = "a:".count($images).":{".implode("",$images)."}"; update_post_meta($post_id, '_pods_' . $pods_field_name,$meta, $existing ); } add_action( 'pmxi_gallery_image', 'wpai_pods_image_imported', 10, 4 ); ?>
When executing WP All Import, I get a PHP runtime error:
Uncaught Error: Call to a member function id() on null in C:\\wamp64\\www\\wordpress\\wp-content\\uploads\\wpallimport\\functions.php:11 Stack trace: #0 C:\\wamp64\\www\\wordpress\\wp-includes\\class-wp-hook.php(324): wpai_pods_image_imported(26721, 26722, 'C:\\\\wamp64\\\\www\\\\w...', 'update_images') #1 C:\\wamp64\\www\\wordpress\\wp-includes\\class-wp-hook.php(348): WP_Hook->apply_filters('', Array) #2 C:\\wamp64\\www\\wordpress\\wp-includes\\plugin.php(517): WP_Hook->do_action(Array) #3 C:\\wamp64\\www\\wordpress\\wp-content\\plugins\\wp-all-import-pro\\models\\import\\record.php(4403): do_action('pmxi_gallery_im...', 26721, 26722, 'C:\\\\wamp64\\\\www\\\\w...', 'update_images') #4 C:\\wamp64\\www\\wordpress\\wp-content\\plugins\\wp-all-import-pro\\controllers\\admin\\import.php(2805): PMXI_Import_Record->process('process() #6 C:\\wamp64\\www\\wordpress\\wp-includes\\class-wp-hook.php(324): PMXI_Plugin->adminInit('') #7 C:\\wamp64\\www\\wordpress\\wp-includes\\class-wp-hook.php(348): WP_Hook->apply_filters(NULL, Array) #8 C:\\wamp64\\www\\wordpress\\wp-includes\\plugin.php(517): WP_Hook->do_action(Array) #9 C:\\wamp64\\www\\wordpress\\wp-admin\\admin.php(175): do_action('admin_init') #10 {main} thrown
Line 11 is the first offending line of code that PHP crashes on:
$field_id = $field->id();
That leads me to believe that
$field
isnull
. So the line above is not returning a result that I was expecting. Rather than the data of the field, it returnednull
:$field = $pod->fields($pods_field_name);
A quick lookup on Github into the source code of
Pods::fields
, there are a couple of scenarios that could give menull
:- The Pod data is empty (so the call
pods()
was unsuccessful). - The field could not be found.
Currently, I am still investigating. I don’t have an answer to my own question. However, I am still looking into it.
Nonetheless, if there is anyone who could give me a pointer on what I could do to successfully import a series of images into my Pods Gallery field programmatically, I am all ears! ??