Last active
April 23, 2020 18:30
-
-
Save wpjess/577c4f66c97cbad7fd4931d06beb7427 to your computer and use it in GitHub Desktop.
WP get image ID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php $image_id = jess_get_attachment_id_by_url( $image ); ?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//////////////////////////////////////////////////////////////////////// | |
// GET IMAGE ID | |
//////////////////////////////////////////////////////////////////////// | |
function jess_get_attachment_id_by_url( $url ) { | |
$parsed_url = explode( parse_url( WP_CONTENT_URL, PHP_URL_PATH ), $url ); | |
$this_host = str_ireplace( 'www.', '', parse_url( home_url(), PHP_URL_HOST ) ); | |
$file_host = str_ireplace( 'www.', '', parse_url( $url, PHP_URL_HOST ) ); | |
if ( ! isset( $parsed_url[1] ) || empty( $parsed_url[1] ) || ( $this_host != $file_host ) ) { | |
return; | |
} | |
global $wpdb; | |
$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->prefix}posts WHERE guid RLIKE %s;", $parsed_url[1] ) ); | |
return $attachment[0]; | |
} | |
function prefix_remove_php_test( $tests ) { | |
unset( $tests['direct']['WP_AUTO_UPDATE_CORE'] ); | |
return $tests; | |
} | |
add_filter( 'site_status_tests', 'prefix_remove_php_test' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment