Search code examples
phpincludeapostropherelative-url

PHP - Calling Relative URL within = 'xx' apostrophe's


I'm trying to insert a relative URL in the code below before "/images" in $thumb_directory and $orig_directory however I'm not sure how to do so within 'apostrophe's'. Also, this is a WordPress website, so if anyone knows how to call the function within '/images', like so: '/images' please let me know (I realize that what I just typed is incorrect syntax).

<?php
/* Configuration Start */
$thumb_directory = '/images';
$orig_directory = '/images';
$stage_width=600;
$stage_height=400;
/* Configuration end */

Solution

  • Assuming that the relative path is in the variable $rel_path, use double quotes:

    $thumb_directory = "$rel_path/images";
    $orig_directory = "$rel_path/images";
    

    With double quotes variables (starting with $) are replaced by their value, but this doesn't happen with single quotes.

    Or use string concatenation:

    $thumb_directory = $rel_path . '/images';
    $orig_directory = $rel_path . '/images';
    

    EDIT

    If your relative path comes from bloginfo('template_directory'), simply add the following line before using $rel_path (so before the lines above):

    $rel_path = bloginfo('template_directory');
    

    This sets the variable with the path you want. Ensure all these lines are within the PHP processing tags <?php and ?>, and don't forget the semicolon ; at the end of the assignment line ;-)