[ad_1]
perform attachment_url_to_postid( $url ) {
international $wpdb;
$path = $url;
$site_url = parse_url( $dir[‘url’] );
$image_path = parse_url( $path );
//pressure the protocols to match if wanted
if ( isset( $image_path[‘scheme’] ) && ( $image_path[‘scheme’] !== $site_url[‘scheme’] ) ) {
$path = str_replace( $image_path[‘scheme’], $site_url[‘scheme’], $path );
}
if ( 0 === strpos( $path, $dir[‘baseurl’] . ‘/’ ) ) {
$path = substr( $path, strlen( $dir[‘baseurl’] . ‘/’ ) );
}
$sql = $wpdb->put together(
“SELECT post_id FROM $wpdb->postmeta WHERE meta_key = ‘_wp_attached_file’ AND meta_value = %s”,
$path
);
$post_id = $wpdb->get_var( $sql );
/**
* Filters an attachment id discovered by URL.
*
* @since 4.2.0
*
* @param int|null $post_id The post_id (if any) discovered by the perform.
* @param string $url The URL being regarded up.
*/
}
[ad_2]