<?php echo get_template_directory_uri(); ?>/images/image.jpg not working in sidebar

Hoping someone can answer this!

I’m using this for my header and it is working:

Read More
<img src="<?php echo get_template_directory_uri(); ?>/images/image.jpg">  

But when I try to use it in a sidebar text widget it won’t show the image. Any ideas?

Related posts

Leave a Reply

3 comments

  1. PHP code won’t run inside of a text widget. There are plugins that will let you do that, but this type of thing is strongly discouraged.

    Add this code to your functions.php file, or better yet, make it a simple little plugin:

    // Enable the use of shortcodes within widgets.
    add_filter( 'widget_text', 'do_shortcode' ); 
    
    // Assign the tag for our shortcode and identify the function that will run. 
    add_shortcode( 'template_directory_uri', 'wpse61170_template_directory_uri' );
    
    // Define function 
    function wpse61170_template_directory_uri() {
        return get_template_directory_uri();
    }
    

    To use it in the admin, add the shortcode [template_directory_uri] to your text widget, and the template directory will be output e.g.

    [template_directory_uri]/images/image.jpg