Doubt with document conf

Hello,

I’m using a document type URL to set the main document, but in the item view, it shows the URL. I know that I can enable the option Render content in iframe, but I have thousands of items with this document. So, I would like to know if there is a snippet or code to enable the ‘Render content in iframe’ by default. :distorted_face:

Regards!

Item view

Hi @erickml!

If you’re reusing an image as the main document you should enable both “Render content in iframe” and “Is link to external image” options.

Sadly we do not have bulk editing features for document options yet. In particular we consider it may be rare that one would put tons of items with the same document, but I believe you might have some good reason for that.

Meanwhile, what you can do with a bit of effort is use the WP CLI for that. Because those settings are stored as Post Meta named document_options in the Item object. They are something like this:

array (
  'forced_iframe' => true,
  'forced_iframe_width' => 600,
  'forced_iframe_height' => 450,
  'is_image' => true,
)

The script would be something like this (not tested, please run first with a single item):

COLLECTION_ID=123
POST_TYPE="tnc_col_${COLLECTION_ID}_item"

META='{"forced_iframe":true,"forced_iframe_width":600,"forced_iframe_height":450,"is_image":false}'

for ID in $(wp post list --post_type="$POST_TYPE" --format=ids); do
    wp post meta update $ID document_options "$META"
done

Depending of which is your collection ID (123) and of course if you don’t want to apply to all items in the collection you may modify this to something like an array of Item IDs

Thanks for your gentle answer @mateus.m.luna!

The reason I’m trying to do this is because I’m working on a kind of harvester that gets items from other repositories, so I get the URL in document type for each item (approx. 50k items), and the URL contains an image that is different for each item. That’s the context. :sweat_smile:

So, I tried with the script in a testing collection and the result message is:

Success: Updated custom field ‘document_options’.
Success: Updated custom field ‘document_options’.

But in the item view, it doesn’t show the forced iframe and in the ‘Edit item’ option doesn’t apply the option yet. Do you have any idea?

Hi @erickml!

In your script, if after updating you do something like this:

wp post meta get $ID document_options 

What is the output? Maybe the way the json is being parsed is breaking something.

This is the output:

Success: Value passed for custom field 'document_options' is unchanged.
{"forced_iframe":true,"forced_iframe_width":600,"forced_iframe_height":450,"is_image":true}
Success: Value passed for custom field 'document_options' is unchanged.
{"forced_iframe":true,"forced_iframe_width":600,"forced_iframe_height":450,"is_image":true}

But still it shows the link URL and not the forced iframe.:melting_face:

That is weird. In that particular case, is the ID of the item 19041? I mean, which is the return of

wp post meta get 19041 document_options 

?

In this case, my special_item_id is 4503631 and the URL image is got from another Tainacan instance.

Output:

wp post meta get 4503631 document_options --allow-root
{"forced_iframe":true,"forced_iframe_width":600,"forced_iframe_height":450,"is_image":false}

Oh ok, my bad @erickml.

Since the document_options is an array we cannot pass it like that, which is a string encoded version of that array. To give an array value to the wp cli command you’ll need wp eval, which runs php function calls. So the updated version:

COLLECTION_ID=123
POST_TYPE="tnc_col_${COLLECTION_ID}_item"

PHP_ARRAY="
[
    'forced_iframe' => true,
    'forced_iframe_width' => 600,
    'forced_iframe_height' => 450,
    'is_image' => false,
]
"

for ID in $(wp post list --post_type="$POST_TYPE" --format=ids); do
    wp eval "update_post_meta($ID, 'your_meta_key', $PHP_ARRAY);"
done

It works!!

Output:

array (
  'forced_iframe' => true,
  'forced_iframe_width' => 600,
  'forced_iframe_height' => 450,
  'is_image' => true,
)

Thanks @mateus.m.luna!

1 curtida