{"id":987,"date":"2019-12-23T00:02:43","date_gmt":"2019-12-22T17:02:43","guid":{"rendered":"https:\/\/lab.wptips.dev\/?p=987"},"modified":"2020-06-21T09:45:55","modified_gmt":"2020-06-21T02:45:55","slug":"composer-to-manage-plugins","status":"publish","type":"post","link":"https:\/\/pixelstudio.id\/blog\/composer-to-manage-plugins\/","title":{"rendered":"Using Composer to Manage Plugins (Fast and Tidy)"},"content":{"rendered":"\n<p>Composer is a command-line tool to manage 3rd party PHP libraries. But not many people know that it can be used for WordPress plugins and themes. <\/p>\n\n\n\n<p>Follow along this tutorial to learn how:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p><strong>NOTE<\/strong>: This guide is for Windows using WAMP Server. If you use Mac or another server, the installation part varies a bit.<\/p><\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">1. Installation<\/h2>\n\n\n\n<p>First, install WAMP Server and Composer:<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-3\">\n<div class=\"wp-block-column is-layout-flow\">\n<div class=\"wp-block-button aligncenter\"><a class=\"wp-block-button__link has-text-color has-light-blue-color has-background has-blue-background-color\" href=\"https:\/\/pixelstudio.id\/blog\/wamp-for-wordpress\/\" target=\"_blank\" rel=\"noreferrer noopener\">WAMP Installation Guide<\/a><\/div>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow\">\n<div class=\"wp-block-button aligncenter\"><a class=\"wp-block-button__link has-text-color has-light-blue-color has-background has-blue-background-color\" href=\"https:\/\/getcomposer.org\/\" target=\"_blank\" rel=\"noreferrer noopener\">Download Composer<\/a><\/div>\n<\/div>\n<\/div>\n\n\n\n<p>Check if the Composer has been successfully installed by running the command <code>composer -V<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Update PHP Version<\/h2>\n\n\n\n<p>Check your PHP version by running the command <code>php -v<\/code>. If it says 7.0 or above, you can <strong>skip this step.<\/strong><\/p>\n\n\n\n<p>If it&#8217;s below 7.0, update the PHP version by following these steps:<\/p>\n\n\n\n<ol><li>Go to <code>C:\\wamp\\bin\\php<\/code> and copy the folder path of the PHP versions that you want. For example <code>C:\\wamp\\bin\\php\\php7.2.14<\/code>.<\/li><li>Open <strong>Environment variables<\/strong> dialog.<br>How? Press Windows button &gt; type &#8220;env&#8221; &gt; click &#8220;Edit environment variables for your account&#8221;<\/li><li>Double click &#8220;Path&#8221; under User Variables.<\/li><li>Replace the path to PHP folder with your new one.<\/li><li>Done! Check the version again by running&nbsp;<code>php -v<\/code>. <\/li><\/ol>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"418\" src=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2019\/12\/edit-env-var.jpg\" alt=\"\" class=\"wp-image-1162\" srcset=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2019\/12\/edit-env-var.jpg 750w, https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2019\/12\/edit-env-var-480x268.jpg 480w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><figcaption>Changing PHP version in Environment Variables<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">3. Create Composer Config File<\/h2>\n\n\n\n<p>Create <code>composer.json<\/code> file in the root directory of your WordPress site. It contains a list of plugins and themes.<\/p>\n\n\n\n<p>Example:<\/p>\n\n\n\n<pre title=\"composer.json\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json line-numbers\">{\n  \"name\": \"my\/demo\",\n  \"require\": {\n    \"wpackagist-plugin\/timber-library\": \"~1.13.0\",\n    \"wpackagist-plugin\/autodescription\": \"~4.0\",\n    \"wpackagist-plugin\/contact-form-7\": \"~5.1\",\n    \"wpackagist-plugin\/w3-total-cache\": \"*\",\n    \"wpackagist-theme\/twentytwenty\": \"*\"\n  },\n  \"repositories\": [\n    { \"type\": \"composer\", \"url\":\"https:\/\/wpackagist.org\" }\n  ],\n  \"autoload\": { \"psr-0\": { \"Acme\": \"src\/\" } }\n}<\/code><\/pre>\n\n\n\n<ul><li>You define all plugins and themes inside <code>require<\/code>.<\/li><li>The plugin is prefixed with <code>wpackagist-plugin\/<\/code> followed by its slug.<\/li><li>The slug can be seen in its folder name or in the URL of <a rel=\"noreferrer noopener\" aria-label=\"Wordpress.org repo (opens in a new tab)\" href=\"https:\/\/wordpress.org\/plugins\/\" target=\"_blank\">WordPress.org repo<\/a>.<\/li><\/ul>\n\n\n\n<p>Versioning cheatsheet:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><code>*<\/code><\/td><td>Get latest version<\/td><\/tr><tr><td><code>1.2.0<\/code><\/td><td>Get version 1.2.0 <\/td><\/tr><tr><td> <code>~1.2.0<\/code><\/td><td>The last digit can be updated.<br>For example if there is version <code>1.2.4<\/code>, it will download that. But when <code>1.3.0<\/code> is released, it will stay at <code>1.2.4<\/code>. <\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">4. Download the Plugins &amp; Themes<\/h2>\n\n\n\n<p>Run this command in the root directory of your site:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">composer update<\/code><\/pre>\n\n\n\n<p style=\"font-size:14px\" class=\"has-text-color has-gray-color\"><strong>HINT<\/strong>: You can type &#8220;cmd&#8221; in the File Explorer&#8217;s address bar to open command prompt in that directory.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"400\" src=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2019\/12\/composer-update.jpg\" alt=\"\" class=\"wp-image-1161\" srcset=\"https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2019\/12\/composer-update.jpg 750w, https:\/\/pixelstudio.id\/blog\/wp-content\/uploads\/2019\/12\/composer-update-480x256.jpg 480w\" sizes=\"auto, (max-width: 750px) 100vw, 750px\" \/><figcaption>Downloading WP plugins with Composer<\/figcaption><\/figure>\n\n\n\n<p>Done! If you want to update the plugins, simply run the same command.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"custom-plugins\">5. Custom Plugins<\/h2>\n\n\n\n<p>For plugins that are not in the official repository, there are 2 ways:<\/p>\n\n\n\n<ol><li>If you are the creator, put it on Github and register it in <a href=\"https:\/\/packagist.org\/\">Packagist<\/a> with type <code>wordpress-plugin<\/code>.<\/li><li>If you are not the creator, zip the plugin (should only contain 1 folder), upload it somewhere, and link it in the config like shown below:<\/li><\/ol>\n\n\n\n<pre title=\"composer.json\" class=\"wp-block-code\"><code lang=\"json\" class=\"language-json\">{\n  \"name\": \"my\/demo\",\n  \"require\": {\n    \"custom\/my-plugin\",\n    ...\n  },\n  \"repositories\": [\n    { \"type\": \"composer\", \"url\":\"https:\/\/wpackagist.org\" },\n    { \"type\": \"package\", \"package\": {\n      \"name\": \"custom\/my-plugin\",\n      \"version\": \"1.0.0\",\n      \"type\": \"wordpress-plugin\",\n      \"dist\": {\n        \"type\": \"zip\",\n        \"url\": \"https:\/\/yoursite.com\/files\/my-plugin.zip\"\n      },\n      \"require\" : { \"composer\/installers\": \"~1.0\" }\n    } }\n  ],\n  \"autoload\": { \"psr-0\": { \"Acme\": \"src\/\" } }\n}<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator is-style-dots\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>There are many benefits of using Composer:<\/p>\n\n\n\n<ul><li><strong>Convenient<\/strong> &#8211; One command to get everything.<\/li><li><strong>Synchronize<\/strong> &#8211; Making sure your Live and Local site uses the same plugins and versions. (You need SSH access to install Composer in your live server).<\/li><li><strong>Git Friendly<\/strong> &#8211; Commit <code>composer.json<\/code> and ignore <code>wp-content\/plugins<\/code>.<\/li><\/ul>\n\n\n\n<p>For more power, you can combine Composer with <a href=\"https:\/\/wp-cli.org\/\">WP-CLI<\/a> so you can download the WP core files too.<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>Let me know in the comment below if you have trouble in the comment below.  \ud83d\ude42<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Composer is a command-line tool to manage 3rd party PHP libraries. But not many people know that it can be used for WordPress plugins and themes. <\/p>\n","protected":false},"author":1,"featured_media":1165,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[],"class_list":["post-987","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general"],"blocksy_meta":{"page_structure_type":"default","page_enable_vertical_spacing":"yes","has_hero_section":"default","hero_section":"type-1","hero_alignment1":"left","hero_alignment2":"center","hero_height":"230px","page_title_bg_type":"color","0a1f0bf5188eb81abe4c826ee0cc3e08":"","custom_hero_background":{"attachment_id":null},"parallax":{"desktop":false,"tablet":false,"mobile":false},"1dbe75f3b5bbacb86c50e6cafb76e0e4":"","single_meta_elements":{"author":true,"date":true,"categories":true,"comments":true,"updated":false,"tags":false},"has_meta_label":"yes","date_format_source":"custom","single_meta_date_format":"M j, Y","2eea201296988c12211eaccb94b49692":"","page_excerpt_visibility":{"desktop":true,"tablet":true,"mobile":false},"pageTitleFont":{"family":"Default","variation":"Default","size":{"desktop":"32px","tablet":"30px","mobile":"25px"},"line-height":"1.3","letter-spacing":"CT_CSS_SKIP_RULE","text-transform":"CT_CSS_SKIP_RULE","text-decoration":"CT_CSS_SKIP_RULE"},"pageTitleFontColor":{"default":{"color":"var(--paletteColor4)"}},"pageMetaFont":{"family":"Default","variation":"n6","size":{"desktop":"12px","tablet":"12px","mobile":"12px"},"line-height":"1.3","letter-spacing":"CT_CSS_SKIP_RULE","text-transform":"uppercase","text-decoration":"CT_CSS_SKIP_RULE"},"pageMetaFontColor":{"default":{"color":"CT_CSS_SKIP_RULEDEFAULT"},"hover":{"color":"CT_CSS_SKIP_RULEDEFAULT"}},"pageExcerptFont":{"family":"Default","variation":"n5","size":"CT_CSS_SKIP_RULE","line-height":"CT_CSS_SKIP_RULE.65","letter-spacing":"CT_CSS_SKIP_RULE","text-transform":"CT_CSS_SKIP_RULE","text-decoration":"CT_CSS_SKIP_RULE"},"pageExcerptColor":{"default":{"color":"CT_CSS_SKIP_RULEDEFAULT"}},"pageTitleOverlay":{"default":{"color":"rgba(41, 51, 60, 0.2)"}},"pageTitleBackground":{"default":{"color":"#EDEFF2"}},"6075da353229edb228fdfd630ba8078b":"","96b55b0631c2bd34a0db10f718b47cd7":"","6e7cb600b03deddfdf9e173f904fb609":"","f6c0af44c72b561dcb7843742f3644b9":"","eaaac8a7420f3207e9ddbebc9646a8f9":"","cf9d4c0a81b1254840c3a3110e5397de":""},"_links":{"self":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/987","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/comments?post=987"}],"version-history":[{"count":10,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/987\/revisions"}],"predecessor-version":[{"id":1652,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/posts\/987\/revisions\/1652"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media\/1165"}],"wp:attachment":[{"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/media?parent=987"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/categories?post=987"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pixelstudio.id\/blog\/wp-json\/wp\/v2\/tags?post=987"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}