Echo Custom Fields in Post
-
How can i display custom fields like “tube_source_external_url” inside my post.
I tried: <?php echo get_post_meta($post->ID, ‘tube_source_external_url’, true); ?>
But nothing shows up
Thanks in advance!
-
For better understandig: I want to echo the Channel-URL inside the post. I can display the Channel Name with “tube_video_creator_name” but cant find a way to display the link
NOTE: Assuming YouTube…
In part because of the way the plugin stores data, and also because YouTube has a variety of “creator” URLs, this isn’t going to be very easy, or at least not as simple as spitting out a custom field.
You MIGHT be able to get away with this for some…
<?php $creator = get_post_meta($post->ID, 'tube_video_creator_name', true); $url = 'https://www.youtube.com/user/' . $creator; ?>If you wanted to be really ambitious, you could use the video ID to hit the YouTube API and get the creator’s Channel URL.
Below is sample code that gets the duration and stores it in a custom field for the video, which I’d imagine would be trivial to update to get the creator by revising the stuff starting after the “// extract the duration” comment.
add_action('added_post_meta', 'tube_maybe_get_youtube_duration', 10, 4); add_action('updated_post_meta', 'tube_maybe_get_youtube_duration', 10, 4); function tube_maybe_get_youtube_duration( $meta_id, $post_id, $meta_key='', $meta_value='' ){ // Do nothing unless correct key if ( $meta_key != 'tube_video_site' ) return; // only applied to youtube if ( $meta_value != 'youtube' ) return; // make sure it doesn't have duration already $has_duration = get_post_meta( $post_id, 'tube_video_duration_hms', true ); if ( $has_duration ) return; tube_get_youtube_duration( $post_id ); } function tube_get_youtube_duration( $post_id ){ // get the video ID $video_id = get_post_meta( $post_id, 'tube_video_id', true ); if ( ! $video_id ) return; // get the API key global $tube_video_curator; $youtube_api_key = $tube_video_curator :: $tube_youtube_videos -> get_youtube_api_key(); if ( ! $youtube_api_key ) return; // generate the endpoint $endpoint = add_query_arg( array( 'part' => 'contentDetails', 'id' => $video_id, 'key' => $youtube_api_key ), 'https://www.googleapis.com/youtube/v3/videos' ); // get the raw results $results_json = file_get_contents( $endpoint ); // decode the results $results = json_decode( $results_json, true ); // extract the duration $duration = $results['items'][0]['contentDetails']['duration']; // get the duraton parts preg_match_all( '/PT(\d+H)?(\d+M)?(\d+S)?/', $duration, $duration_parts ); // create HMS array $time_hms = array( 'h' => (int) str_replace( 'H', '', $duration_parts[1][0] ), 'm' => (int) str_replace( 'M', '', $duration_parts[2][0] ), 's' => (int) str_replace( 'S', '', $duration_parts[3][0] ) ); // calculate total seconds $time_total_seconds = ( $time_hms['h'] * 3600 ) + ( $time_hms['m'] * 60 ) + $time_hms['s']; // update the post meta add_post_meta( $post_id, 'tube_video_duration_hms', $time_hms ); add_post_meta( $post_id, 'tube_video_duration_seconds', $time_total_seconds ); // all done return; }Thank You! This was very helpful 🙂
It seems that the pagination doesnt work in the plugin. I cant look through Videos. If i click on “next” to see more Videos of the specific channel it just schows the same first 10 videos. Also when i try to show 20 30 40 or 50 videos it reloads and shows me always the first 10 videos.
i disabled all plugins, but still not working
On “Next” Click the link changes from:
wp-admin/post.php?post=4260&action=edit
to
wp-admin/post.php?post=4260&action=edit&next=CAoQAA
The topic ‘Echo Custom Fields in Post’ is closed to new replies.