How do I obtain url from File/Image/Video in php?
-
I have a custom type “document” with a F/I/V field “source”. These are pdf, and I need to create a shortcode to display the pdf with a viewer on user page.
I could create the shortcode which includes the pdf url manually for each new document, but that is cumbersome and not good if other admins have to do it.
So I created another field, “shortcode_document”, and a php snippet that should set that field’s value to the full shortcode, pulling the url from the “source” field when the pod is saved.
I used example code on pods site and wrote this:add_action( 'pods_api_pre_save_pod_item_document', 'document_update_pdf_shortcode_on_save', 10, 3 ); function document_update_pdf_shortcode_on_save( $pieces, $is_new_item ) { // s'assurer que les champs sont actifs $fields = array( 'source', 'shortcode_document' ); foreach( $fields as $field ) { if ( ! isset( $pieces[ 'fields_active' ][ $field ] ) ) { array_push ($pieces[ 'fields_active' ], $field ); } } $source = ''; // get value from source if ( isset( $pieces[ 'fields' ][ 'source' ] ) && isset( $pieces[ 'fields' ][ 'source' ][ 'value' ] ) ) { $source = $pieces[ 'fields' ][ 'source' ][ 'value' ]; } // debug error_log(__FUNCTION__); $count = count($source); error_log($count); for ($i = 0; $i < $count; $i++) { error_log($i.': '.$source[$i].' | '); } // full value for shortcode_document: $shortcode_document_value = '[flipbook pdf=\''.$source[0].'\']'; error_log( $shortcode_document_value ); $pieces[ 'object_fields'][ 'shortcode_document' ][ 'value' ] = $shortcode_document_value; return $pieces; }
If I try to access the value of “source” directly, log says “Array”. But the loop through the array displays only 1 element, and it is empty. I tried
source[‘ID’] as recommended here:
https://stackoverflow.com/questions/12727438/how-to-get-url-of-uploaded-file-in-pods-framework-2
but that only yields a warning: “Illegal string offset ‘ID’…”.
Suggestions?
- The topic ‘How do I obtain url from File/Image/Video in php?’ is closed to new replies.