WordPress.org

Ideas

Image size as suffix instead of image dimensions

  1. Pat Hawks
    Member

    12345

    We've gotten so far from where we started, I don't think this will be resolved in this thread.

    Another thread seems pretty close to what we are now discussing, but it has been closed.

    I think we can all at least agree that this is plugin territory.

    Posted: 3 years ago #
  2. Mdkart
    Member

    12345

    Just an update to tell you I have personnally written a plugin to insert images by shortcode :

    Pros :
    - Can be used by via media uploader or entering manually the shortcodes.
    - You can regenerate images (for example, when you change default size images) without breaking your posts.
    - Image is inserted using default function wp_get_attachment_image() and so fully compatible with latest changement to enable responsive images.
    - I have put some variables at the beginning of the plugin, to choose default parameters of the shortcodes.
    - title and alt attributes come from media information you write when you upload or edit the media.

    Two shortcodes :
    1) [img_disp id=X size=X link_to=X]
    Insert a pic and a link with a link to the picture.

    Parameters : id is the only required parameter
    - id=ID_OF_THE_PICTURE
    - size=SIZE_OF_THE_DISPLAYED_IMG . Ex thumbnail, medium, large, or full or a 2-item array representing width and height in pixels, e.g., array(32,32). Default is medium
    - link_to=SIZE_OF_THE_LINKED_IMG As above, or type 0 if you don't want to link Default is large

    2) [img_link id=X link_to=X]CONTENT[/img_link]
    Just a link to a picture.

    Parameters : id is the only required parameter
    - id=ID_OF_THE_PICTURE
    - link_to=SIZE_OF_THE_LINKED_IMG Default is large

    Here's the code :

    /*
    Plugin Name: MD Image Shortcode
    Plugin URI: http://mdkart.fr
    Description: Insert picture just using it's id
    Version: 1.0
    Author: Mdkart
    Author URI: http://mdkart.fr
    License: CC-BY-NC-SA
    */
    
    //Default parameters. Easy to change so!
    $md_default_size_disp = 'medium';
    $md_default_size_link = 'large';
    $md_default_align = 'none';
    
    function md_img_link( $content, $id = 1, $link_to = null ) {
    	$link_to = isset($link_to ) ? $link_to  : $GLOBALS["$md_default_size_link"];
    	$content=  '<a href="' . wp_get_attachment_image_src($id, $link_to)[0] . '" title="'.get_the_title($id).'">' . $content . '</a>';
    	return $content;
    }
    
    function md_img_disp_shortcode( $atts, $content = null ) {
    	extract( shortcode_atts( array(
    		'id'    => 1,
    		'size' => $GLOBALS["md_default_size_disp"],
    		'link_to' => $GLOBALS["md_default_size_link"],
    		'align' => $GLOBALS["md_default_align"],
    	), $atts ) );
    	$class = "attachment-$size size-$size wp-image-$id"; // Définition des attributs de class (pour les css)
    	if ( $align != 'none' )
    		$class .= " align$align";
    	$attr = array(
    		'class'	=> $class,
    	);
    	$output = wp_get_attachment_image( $id, $size, 0, $attr );
    	if ( $link_to != "0" )
    		$output = md_img_link( $output, $id, $link_to );
    	return $output;
    }
    
    function md_img_link_shortcode( $atts, $content = null ) {
    	extract( shortcode_atts( array(
    		'id'    => 1,
    		'link_to' => $GLOBALS["md_default_size_link"],
    	), $atts ) );
    	$content =  md_img_link( $content, $id, $link_to );
    	return $content;
    }
    
    //SHORTCODE [img_disp id=X size=X link_to=X]
    add_shortcode( 'img_disp', 'md_img_disp_shortcode' );
    /* Parameters :
    id=ID_OF_THE_PICTURE
    size=SIZE_OF_THE_DISPLAYED_IMG . Ex thumbnail, medium, large, or full or a 2-item array representing width and height in pixels, e.g., array(32,32).
    link_to=SIZE_OF_THE_LINKED_IMG As above, or type 0 if you don't want to link*/
    
    //SHORTCODE [img_link id=X link_to=X]CONTENT[/img_link]
    add_shortcode( 'img_link', 'md_img_link_shortcode' );
    /* Parameters :
    id=ID_OF_THE_PICTURE
    link_to=SIZE_OF_THE_LINKED_IMG As above*/
    
    // Alter Media Uploader output to output shortcode instead
    function md_insert_image( $html, $id, $caption, $title, $align, $url, $size ) {
    	$content = "[img_disp id=$id";
    	if ($size != $GLOBALS["md_default_size_disp"]) //Ajoute attribut size si besoin
    		$content .= " size=$size";
    	if ($align != $GLOBALS["md_default_align"]) //Ajoute align size si besoin
    		$content .= " align=$align";
    	$content .= "]";
    	return $content;
    }
    add_filter( 'image_send_to_editor', 'md_insert_image', 10, 9 );
    Posted: 9 months ago #

RSS feed for this topic

Reply

You must log in to post.

  • Rating

    12345
    10 Votes
  • Status

    Sorry, not right now