4

I'm aware that one can add a control to the theme customiser to add an image.

This control returns an image URL however, and I need its attachment ID, not the image URL. Where can I intercept or steal the attachment ID of this image in order to do this? Is there a save hook I can intercept? Or a javascript based method?

1 Answer 1

1

The solution, needs a custom control object extending the original image control, and does an SQL query to grab the GUID and associated attachment ID on sanitisation. Not nice, kludgey, but it works

$wp_customize->add_setting( 'customimage', array(
    'default'       => $default,
    'capability'    => 'edit_theme_options',
    'type'          => 'option',
    'sanitize_callback' => array( 'ICIT_Customize_Image_Control_AttID', 'attachment_guid_to_id' ),
    'sanitize_js_callback' => array( 'ICIT_Customize_Image_Control_AttID', 'attachment_guid_to_id' ),
) );

$wp_customize->add_control( new ICIT_Customize_Image_Control_AttID( $wp_customize, "custom_image_attach_id", array(
    'label'      => $label,
    'section'    => "custom_image_attach_id",
    'settings'   => 'customimage'
) ) );

if ( ! class_exists( 'ICIT_Customize_Image_Control_AttID' ) ) {
    class ICIT_Customize_Image_Control_AttID extends WP_Customize_Image_Control {

        public $context = 'custom_image';

        public function __construct( $manager, $id, $args ) {
            $this->get_url = array( $this, 'get_img_url' );
            parent::__construct( $manager, $id, $args );
        }

        // As our default save deals with attachment ids not urls we needs this.
        public function get_img_url( $attachment_id = 0 ) {
            if ( is_numeric( $attachment_id ) && wp_attachment_is_image( $attachment_id ) )
                list( $image, $x, $y ) = wp_get_attachment_image_src( $attachment_id );

            return ! empty( $image ) ? $image : $attachment_id;
        }


        public function attachment_guid_to_id( $value ) {
            global $wpdb;
            if ( ! is_numeric( $value ) ) {
                $attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = 'attachment' AND guid = %s ORDER BY post_date DESC LIMIT 1;", $value ) );
                if ( ! is_wp_error( $attachment_id ) && wp_attachment_is_image( $attachment_id ) )
                    $value = $attachment_id;
            }

            return $value;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.