Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active August 29, 2015 14:21
Show Gist options
  • Save kurozumi/eed112bd964deaf1f32a to your computer and use it in GitHub Desktop.
Save kurozumi/eed112bd964deaf1f32a to your computer and use it in GitHub Desktop.
【ワードプレス】投稿済みのoEmbed対応メディアかチェックする
/**
* 投稿済みメディアかチェックする
* @global type $post
* @global type $wp_embed
* @return type
*/
add_action('wp_ajax_parse-embed', function() {
global $post, $wp_embed, $wpdb;
if ( ! $post = get_post( (int) $_POST['post_ID'] ) )
return;
if ( empty( $_POST['shortcode'] ) || ! current_user_can( 'edit_post', $post->ID ) )
return;
$shortcode = wp_unslash( $_POST['shortcode'] );
$url = str_replace( '[embed]', '', str_replace( '[/embed]', '', $shortcode ) );
$sql =<<< __EOS__
SELECT
ID
FROM
{$wpdb->posts}
WHERE
post_content LIKE '%%%s%%' AND
post_status = %s
__EOS__;
$post = $wpdb->get_row($wpdb->prepare($sql, array($url, 'publish')));
if(isset($post->ID) && $post->ID != $_POST['post_ID']){
wp_send_json_success( array(
'body' => sprintf("<script>alert('%s');</script>%s", __("投稿済みです。"), $url)
) );
}
}, 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment