product data shopify

How To Automatically Add Files To Shopify Theme Using PHP

I will show you how to add a file to Shopify theme using PHP in this article. Before getting started, we will assume that you’ve already installed your app

Customizing Shopify Themes

Before adding a file to theme, you have to customize your scope so that you will have the right to access theme. Let’s change $scope variable look like this:

$scopes = "read_themes,write_themes";

You can also add other scopes to install script files such as read_products, write_products, read_scripttags, write_scripttags,…

After changing the scopes, save your file and install your app on store again

Shopify Theme API

Please use following code to get all active themes on your store

$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$shop = "magepow";
$theme = shopify_call($token, $shop, "/admin/api/2021-01/themes.json", array(), 'GET');
$theme = json_decode($theme['response'], JSON_PRETTY_PRINT);
foreach ($theme as $cur_theme) {
	foreach ($cur_theme as $key => $value) {
		if($value['role'] === 'main') {
			$theme_id = $value['id'];
			$theme_name = $value['name'];

			echo "Theme ID: " . $theme_id . "<br />";
			echo "Theme Name: " . $theme_name . "<br />";

Shopify Asset API

Following these codes to add your file to theme

$array = array(
	'asset' => array(
		'key' => 'templates/home.liquid', 
		'value' => '<h1>Hello World</h1>'

$assets = shopify_call($token, $shop, "/admin/api/2021-01/themes/".$value['id']."/assets.json", $array, 'PUT');
$assets = json_decode($assets['response'], JSON_PRETTY_PRINT);

Load the store admin again, on Online Store, open edit code -> templates, you will see your home.liquid has already been installed on theme


Below is some instruction for auto-adding a file to theme in Shopify using PHP. I hope this guide will help you add a file to Shopify theme successfully. Thanks for reading!