Skip to content

Instantly share code, notes, and snippets.

@kurozumi
Last active Aug 29, 2015
Embed
What would you like to do?
【ワードプレス】投稿済みの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