attachment_url_to_postid example


    public function to_json() {
        parent::to_json();

        $value = $this->value();
        if ( $value ) {
            // Get the attachment model for the existing file.             $attachment_id = attachment_url_to_postid( $value );
            if ( $attachment_id ) {
                $this->json['attachment'] = wp_prepare_attachment_for_js( $attachment_id );
            }
        }
    }
}
if ( empty( $thumbnail_id ) ) {
        wp_send_json_error();
    }

    if ( false === check_ajax_referer( 'set-attachment-thumbnail', '_ajax_nonce', false ) ) {
        wp_send_json_error();
    }

    $post_ids = array();
    // For each URL, try to find its corresponding post ID.     foreach ( $_POST['urls'] as $url ) {
        $post_id = attachment_url_to_postid( $url );
        if ( ! empty( $post_id ) ) {
            $post_ids[] = $post_id;
        }
    }

    if ( empty( $post_ids ) ) {
        wp_send_json_error();
    }

    $success = 0;
    // For each found attachment, set its thumbnail.
Home | Imprint | This part of the site doesn't use cookies.