Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. function save_url_link( $post_id, $post ){
  2. // only works if the post is an audio post format
  3. if ( has_post_format('audio', $post_id)) {
  4. // look for either http or https mp3, m4a, ogg, wav or wma files
  5. $pattern = "/(http|https)://.*/(.*).(mp3|m4a|ogg|wav|wma)/";
  6. // php the removes all except the mp3 link
  7. preg_match_all($pattern,$post->post_content,$matches);
  8. // saves the mp3 link to a string
  9. $audio_raw = $matches[0][0];
  10. // check if meta field is empty
  11. if (get_post_meta( $post_id, 'audio_url', true ) == '') {
  12. // update the meta field
  13. update_post_meta( $post_id, 'audio_url', $audio_raw );
  14. // removes the save action
  15. remove_action('save_post', 'save_url_link', 10, 2);
  16. // update the content without the mp3 link
  17. wp_update_post(array('ID' => $post_id,'post_excerpt'=>$audio_raw,'post_content' => preg_replace('/(http|https)://.*/(.*).(mp3|m4a|ogg|wav|wma)/', "", $post->post_content)));
  18. // close and save
  19. };}}
  20. add_action( 'save_post', 'save_url_link', 10, 2 );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement