Support » Fixing WordPress » Trying to show an image by usning shortcode

  • Almost everything works fine accept to display the image.
    I have the image anna.jpg in the same folder as the php function
    getContactInfo where the code to hande the shortcode is located.

    The text from the switch clause when the case anna match is being displayed correct.

    So what is it that I doing wrong here?

    <?php
    /*
       * Plugin Name: WordPress ShortCode
       * Description: Create a wordpress contactinfo shortcode
       * Version: 1.0
       * Author: Tony Johansson
       * Author URI: xxx
    */
    
    add_shortcode( 'contact', 'getContactInfo' );
    
    function getContactInfo( $atts )
    {
        $result = "";
         extract(shortcode_atts(array(
    	   'person' => 'noName'
    	   ),$atts));
    
    	   switch($person)
    	   {
    	      case 'anna':
    		  $result = "Namn: Anna Persson ";
                      $result .= "Telefon: 08-123465 ";
    		  $result .= "Adress: Stigen 55, 123 45 Småstad ";
    		  $result .= "E-mail: anna.person@telia.com ";
    		  $result .=  "<img src='anna.jpg' alt='Bild på Anna
                                     Persson' />";
    		  break;
                   more case here ...
    	   }
    
    	   return $result;
    }
    ?>

Viewing 1 replies (of 1 total)
  • It’s this line:

    $result .=  "<img src='anna.jpg' alt='Bild på Anna Persson' />";

    You can’t just link to an image using a relative path like that. You never know where the shortcode might be used… so you need an absolute path to the image.

    This should work:

    $result .=  "<img src='".plugin_dir_url(__FILE__).'anna.jpg'."' alt='Bild på Anna Persson' />";

    Take a look at plugin_dir_url().

Viewing 1 replies (of 1 total)
  • The topic ‘Trying to show an image by usning shortcode’ is closed to new replies.