• I have a plugin that automatically inserts HTML code into the < a href > of images, in order to populate a Javascript gallery that I have.

    The issue is, the plugin uses a function to automatically find the ‘rel=”attachment wp-att-1234’ in order to get the ID and then grab the caption from the media database. This was working fine until I assume WordPress 4.5, which appears to have changed the get_image_send_to_editor block of code in wp-admin/includes/media.php.

    In other words, the new code is not inserting the ‘rel’ attribute at all into the < a > tags.

    Here is the old block of code, which I have reverted to and is now working again when I insert images:

    /**
     * Retrieves the image HTML to send to the editor.
     *
     * @since 2.5.0
     *
     * @param int          $id      Image attachment id.
     * @param string       $caption Image caption.
     * @param string       $title   Image title attribute.
     * @param string       $align   Image CSS alignment property.
     * @param string       $url     Optional. Image src URL. Default empty.
     * @param string       $rel     Optional. Image rel attribute. Default empty.
     * @param string|array $size    Optional. Image size. Accepts any valid image size, or an array of width
     *                              and height values in pixels (in that order). Default 'medium'.
     * @param string       $alt     Optional. Image alt attribute. Default empty.
     * @return string The HTML output to insert into the editor.
     */
    function get_image_send_to_editor( $id, $caption, $title, $align, $url = '', $rel = '', $size = 'medium', $alt = '' ) {
    
    	$html = get_image_tag($id, $alt, '', $align, $size);
    
    	if ( ! $rel ) {
    		$rel = ' rel="attachment wp-att-' . esc_attr( $id ) . '"';
    	} else {
    		$rel = ' rel="' . esc_attr( $rel ) . '"';
    	}
    
    	if ( $url )
    		$html = '<a href="' . esc_attr($url) . "\"$rel>$html</a>";
    
    	/**
    	 * Filter the image HTML markup to send to the editor.
    	 *
    	 * @since 2.5.0
    	 *
    	 * @param string       $html    The image HTML markup to send.
    	 * @param int          $id      The attachment id.
    	 * @param string       $caption The image caption.
    	 * @param string       $title   The image title.
    	 * @param string       $align   The image alignment.
    	 * @param string       $url     The image source URL.
    	 * @param string|array $size    Size of image. Image size or array of width and height values
    	 *                              (in that order). Default 'medium'.
    	 * @param string       $alt     The image alternative, or alt, text.
    	 */
    	$html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt );
    
    	return $html;
    }

    And then here’s the new code which is not inserting ‘rel=”attachment…”‘:

    /**
     * Retrieves the image HTML to send to the editor.
     *
     * @since 2.5.0
     *
     * @param int          $id      Image attachment id.
     * @param string       $caption Image caption.
     * @param string       $title   Image title attribute.
     * @param string       $align   Image CSS alignment property.
     * @param string       $url     Optional. Image src URL. Default empty.
     * @param bool|string  $rel     Optional. Value for rel attribute or whether to add a dafault value. Default false.
     * @param string|array $size    Optional. Image size. Accepts any valid image size, or an array of width
     *                              and height values in pixels (in that order). Default 'medium'.
     * @param string       $alt     Optional. Image alt attribute. Default empty.
     * @return string The HTML output to insert into the editor.
     */
    function get_image_send_to_editor( $id, $caption, $title, $align, $url = '', $rel = false, $size = 'medium', $alt = '' ) {
    
    	$html = get_image_tag( $id, $alt, '', $align, $size );
    
    	if ( $rel ) {
    		if ( is_string( $rel ) ) {
    			$rel = ' rel="' . esc_attr( $rel ) . '"';
    		} else {
    			$rel = ' rel="attachment wp-att-' . intval( $id ) . '"';
    		}
    	} else {
    		$rel = '';
    	}
    
    	if ( $url )
    		$html = '<a href="' . esc_attr( $url ) . '"' . $rel . '>' . $html . '</a>';
    
    	/**
    	 * Filter the image HTML markup to send to the editor.
    	 *
    	 * @since 2.5.0
    	 *
    	 * @param string       $html    The image HTML markup to send.
    	 * @param int          $id      The attachment id.
    	 * @param string       $caption The image caption.
    	 * @param string       $title   The image title.
    	 * @param string       $align   The image alignment.
    	 * @param string       $url     The image source URL.
    	 * @param string|array $size    Size of image. Image size or array of width and height values
    	 *                              (in that order). Default 'medium'.
    	 * @param string       $alt     The image alternative, or alt, text.
    	 */
    	$html = apply_filters( 'image_send_to_editor', $html, $id, $caption, $title, $align, $url, $size, $alt );
    
    	return $html;
    }

    Anyone have any idea what might be going on, or why this change was made?

    Short version: WordPress 4.5 no longer inserts ‘rel=”attachment wp-att-12345″‘ when adding images, and I’m not sure why, or how to change it back properly without hacking the code in wp-admin/includes/media.php.

    Thanks ?? !

  • The topic ‘get_image_send_to_editor has changed, no more 'rel="attachment"' ?’ is closed to new replies.