Global WordPress IMG directory function

I am using Bones theme to develop wordpress themes. Images are stored in ‘theme/lib/images/site/’. To load an image in a template I a currently using:

<?php get_template_directory_uri().'/library/images/'; ?>

I would like to add this directory as a global variable in functions.php so I can use a shortcode to load images, for example.

Read More
<?php echo imgDir(); ?>

My first thought would be to use something like

function imgDir() {
     $path_to_image = get_template_directory_uri() . '/library/images/';
}

But does not seem to be working, am I missing something simple here? Shouldn’t be to hard to add a short code like this.

Related posts

2 comments

  1. You need to return your value:

    function imgDir() {
        return get_template_directory_uri() . '/library/images/';
    }
    
  2. The proper way to add a shortcode hook according to the WordPress Documentation is this.

    Inside your functions.php

    function imgDir( $atts ){
        return get_template_directory_uri() . '/library/images/';
    }
    add_shortcode( 'imgdir', 'imgDir' );
    

    So the shortcode [imgdir] will return the desired path.

    To call a shortcode inside your PHP-File just use the do_shortcode() function like this –

    echo do_shortcode('[imgdir]'); (Documentation)

Comments are closed.