Reputation: 45
I have a theme wordpress development folder (theme/resources/assets) for development and theme/assets for production. When I create header.php
and add:
img src=”?php echo get_template_directory_uri();
?>/assets/images/code.png”
In development mode, images don't show up. It works only after build. In dev mode, the path must be /resources/assets/images/code.png
How I can fix that? How I can dynamically change paths in Wordpress PHP files? Is it possible?
Upvotes: 2
Views: 636
Reputation: 1661
You could use site URL to check whether it's production or development site and return assets URL based on that:
<?php
$site_url = get_site_url();
if($site_url == 'http://http://stage.example.com'){
// Staging site assets url
$assets_url = get_template_directory_uri() . '/resources/assets';
}else{
// Live site assets url
$assets_url = get_template_directory_uri() . '/assets';
}
?>
<img src="<?php echo $assets_url; ?>/images/code.png" />
Same code can be used in different ways by changing the condition, if you have a better way to compare instead of site URL you can change the if statement and the ourput would be the same.
Upvotes: 0
Reputation: 13719
You probably should fix the code from
img src=”?php echo get_template_directory_uri();
?>/assets/images/code.png”
To:
<img src="<?php echo get_template_directory_uri();?>/assets/images/code.png" />
For detecting the environment, you may use the built-in WP_DEBUG
constant and adjust the path as necessary:
<img src="<?php
echo get_template_directory_uri() . (WP_DEBUG ? '/resources' : '');
?>/assets/images/code.png" />
Upvotes: 1