public function attachment_link( $link, $post_id ) {
		/**
		 * WP_Rewrite.
		 *
		 * @var WP_Rewrite $wp_rewrite
		 */
		global $wp_rewrite;

		if ( ! $wp_rewrite->permalink_structure ) {
			return $link;
		}

		$post = get_post( $post_id );
		if ( ! $post->post_parent ) {
			return $link;
		}

		$post_parent = get_post( $post->post_parent );
		if ( ! $post_parent ) {
			return $link;
		}

		$pt_object = get_post_type_object( $post_parent->post_type );

		if ( empty( $pt_object->rewrite ) ) {
			return $link;
		}

		if ( ! in_array( $post->post_type, CPTP_Util::get_post_types(), true ) ) {
			return $link;
		}

		$permalink   = CPTP_Util::get_permalink_structure( $post_parent->post_type );
		$post_type   = get_post_type_object( $post_parent->post_type );

		if ( empty( $post_type->_builtin ) ) {
			if ( strpos( $permalink, '%postname%' ) < strrpos( $permalink, '%post_id%' ) && false === strrpos( $link, 'attachment/' ) ) {

				$link = str_replace( $post->post_name, 'attachment/' . $post->post_name, $link );
			}
		}

		return $link;
	}
public function attachment_link( $link, $post_id ) {
		/**
		 * WP_Rewrite.
		 *
		 * @var WP_Rewrite $wp_rewrite
		 */
		global $wp_rewrite;

		if ( ! $wp_rewrite->permalink_structure ) {
			return $link;
		}

		$post = get_post( $post_id );
		if ( ! $post->post_parent ) {
			return $link;
		}

		$post_parent = get_post( $post->post_parent );
		if ( ! $post_parent ) {
			return $link;
		}

		$pt_object = get_post_type_object( $post_parent->post_type );

		if ( empty( $pt_object->rewrite ) ) {
			return $link;
		}

		if ( ! in_array( $post->post_type, CPTP_Util::get_post_types(), true ) ) {
			return $link;
		}

		$permalink   = CPTP_Util::get_permalink_structure( $post_parent->post_type );
		$post_type   = get_post_type_object( $post_parent->post_type );

		if ( empty( $post_type->_builtin ) ) {
			if ( strpos( $permalink, '%postname%' ) < strrpos( $permalink, '%post_id%' ) && false === strrpos( $link, 'attachment/' ) ) {

				$link = str_replace( $post->post_name, 'attachment/' . $post->post_name, $link );
			}
		}

		return $link;
	}
public function attachment_link ( $ link , $ post_id ) { /**
		 * WP_Rewrite.
		 *
		 * @var WP_Rewrite $wp_rewrite
		 */ global $ wp_rewrite ; if ( ! $ wp_rewrite -> permalink_structure ) { return $ link ; } $ post = get_post ( $ post_id ) ; if ( ! $ post -> post_parent ) { return $ link ; } $ post_parent = get_post ( $ post -> post_parent ) ; if ( ! $ post_parent ) { return $ link ; } $ pt_object = get_post_type_object ( $ post_parent -> post_type ) ; if ( empty ( $ pt_object -> rewrite ) ) { return $ link ; } if ( ! in_array ( $ post -> post_type , CPTP_Util :: get_post_types ( ) , true ) ) { return $ link ; } $ permalink = CPTP_Util :: get_permalink_structure ( $ post_parent -> post_type ) ; $ post_type = get_post_type_object ( $ post_parent -> post_type ) ; if ( empty ( $ post_type -> _builtin ) ) { if ( strpos ( $ permalink , '%postname%' ) < strrpos ( $ permalink , '%post_id%' ) && false === strrpos ( $ link , 'attachment/' ) ) { $ link = str_replace ( $ post -> post_name , 'attachment/' . $ post -> post_name , $ link ) ; } } return $ link ; }
Fix attachment output

@version 1.0
@since 0.8.2

@param string $link permalink URI.
@param int    $post_id Post ID.

@return string
Fix attachment output