Support » Developing with WordPress » How to PHP include a theme file

  • Resolved WatcherJoshua


    I am having trouble with what I assumed was an everyday PHP use case.

    I have some SVG files that I want to insert into the index.php template. The files are saved in one of the theme directories.

    I assumed this would be a simple include statement:

    <?php include('/images/icon.svg'); ?>

    This worked great in my local development environment, but the file did not display when I put it on a live server for testing. So, I thought I would try to be more precise and use get_template_directory_uri();, but I could not figure out how to make it work as part of the include statement. I also tried bloginfo('template_url');, but I could not get it to work inside the include statement either.

    So, I decided to just hard code the full path.

    <?php include('/wp-content/themes/theme-name/images/icon.svg'); ?>

    Once again, this worked locally but now it does not display on the testing server. The file definitely exists; if I type the full URL into the browser it loads the file. I can also insert the file using an <img> tag without any problems – but that will not work for my use case.

    Can anyone provide advice for what I am getting wrong?

    • This topic was modified 1 year, 9 months ago by WatcherJoshua. Reason: Spelling
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello!

    I’m not sure if you are using include correctly here because thats usually used for ‘importing’ php files.

    What is your use-case like? Will echo-ing the svg work –
    echo get_template_directory_uri() . '/images/icon.svg'; ?

    In case you are working with a child theme, then you would want to use get_stylesheet_directory_uri() instead.

    Also, if you want to include a php file, you can try –
    include get_template_directory() . '/inc/yourfile.php';

    Thread Starter WatcherJoshua


    Thank you. I tried all of the ways that you suggested, and they did not work – but you did help me get pointed in the right direction. I was not certain if concatenation would work at all with an include statement, and you confirmed that it did.

    I was also using the wrong function. I was not familiar with get_template_directory().

    To elaborate on the use case: I need to include the raw SVG code in the document so that it can be styled with CSS (this would also be necessary for accessing it with JS). I could just copy/paste all the SVG into the document, but that clutter the code and make it hard to change it later. Including the separate document means I can change the icon in Illustrator without needing to change the PHP file as well.

    With a little more research, I found that this is a known issue with using include to insert an SVG with PHP. The easiest work-around is to use file_get_contents() instead.

    The code that worked:
    <?php echo file_get_contents(get_template_directory_uri() . '/images/icon.svg'); ?>

    I found this information about the PHP and SVG issue at:

    Thanks again!

    • This reply was modified 1 year, 9 months ago by WatcherJoshua. Reason: More details
    • This reply was modified 1 year, 9 months ago by WatcherJoshua.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to PHP include a theme file’ is closed to new replies.