Skip to content

Instantly share code, notes, and snippets.

@johnregan3
Last active December 28, 2015 01:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnregan3/7420441 to your computer and use it in GitHub Desktop.
Save johnregan3/7420441 to your computer and use it in GitHub Desktop.
import_videos.php
<?php
// Untested Code!
//Generate New Page from Video
function copy_videos_to_pages( $post_id ) {
// Only if we're saving a flowplayer5 video
if ( 'flowplayer5' !== get_post_type( $post_id ) )
return $post_id;
// Get $post object for video currently being saved
$video = get_post( $post_id );
$page_title = get_page_by_title( $video->post_title, '', 'page' );
//Prevent Infinite Loop - http://codex.wordpress.org/Plugin_API/Action_Reference/save_post#Avoiding_infinite_loops
remove_action( 'save_post', 'copy_videos_to_pages' );
/**
* Check to see if a Page with the same title as this Video exists.
* If not, create a new Page, if so, update existing Page
* Not totally foolproof, but a simple check.
*/
if ( is_null( $page_title ) ) {
//Create new Page that includes content from Video
wp_insert_post(
array(
'post_title' => $video->post_title,
'post_date' => $video->post_date,
'post_content' => '[flowplayer id="' . $video->ID . '"]',
'post_type' => 'page',
'post_status' => 'publish',
)
);
} else {
wp_update_post(
array(
'post_id' => $page->ID,
'post_title' => $video->post_title,
'post_date' => $video->post_date,
'post_content' => '[flowplayer id="' . $video->ID . '"]',
'post_type' => 'page',
'post_status' => 'publish',
)
);
}
add_action( 'save_post', 'copy_videos_to_pages', 10, 1 );
return $post_id;
}
add_action( 'save_post', 'copy_videos_to_pages', 10, 1 );
//Generate New Page from Video on admin_init
function import_videos_to_pages() {
//Get all flowplayer videos, could probably be modified.
$videos = new WP_Query(
array(
'post_type' => 'flowplayer5',
'posts_per_page' => -1,
)
);
foreach ( $videos as $video ) {
$video = get_post( $video->ID );
$page_title = get_page_by_title( $video->post_title, '', 'page' );
/**
* Check to see if a Page with the same title as this Video exists.
* If not, create a new Page, if so, update existing Page
* Not totally foolproof, but a simple check.
*/
if ( is_null( $page_title ) ) {
//Create new Page that includes content from Video
wp_insert_post(
array(
'post_title' => $video->post_title,
'post_date' => $video->post_date,
'post_content' => '[flowplayer id="' . $video->ID . '"]',
'post_type' => 'page',
'post_status' => 'publish',
)
);
} else {
wp_update_post(
array(
'post_id' => $page->ID,
'post_title' => $video->post_title,
'post_date' => $video->post_date,
'post_content' => '[flowplayer id="' . $video->ID . '"]',
'post_type' => 'page',
'post_status' => 'publish',
)
);
} // End if is_null
} // End foreach $video
}
add_action( 'admin_init', 'import_videos_to_pages' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment