1
votes

We are trying to use composer to deal with dependencies for a wordpress site. I would like to include wordpress itself in these dependencies. I am trying to install wordpress to the root of the project. all the other plugins etc are installing correctly based on the installer-paths..

I have tried using fancyguy/webroot-installer, but that wipes the directory that the package is installed to also tried mnsami/composer-custom-directory-installer, but that also wipes the directory.

I have a number of files in the root which i do not want to delete - such as the composer.json for one, .tfignore and suchlike.

is the only option I have to let it install to /vendor and then run a post-install script to move it where I want it? or is there another option I am missing

here is the one i tried for webroot-installer

{
  "name": "the/name",
  "description": "description",
  "repositories": [
    {
      "type": "composer",
      "url": "https://wpackagist.org"
    },
    {
      "type": "package",
      "package": {
        "name": "wordpress",
        "type": "webroot",
        "version": "4.7.5",
        "dist": {
          "type": "zip",
          "url": "https://github.com/WordPress/WordPress/archive/4.7.5.zip"
        },
        "require": {
          "fancyguy/webroot-installer": "1.0.0"
        }
      }
    }
  ],
  "require-dev": {
    "wpackagist-plugin/query-monitor": "2.13.4",
    "wordpress": "4.7.5",
    "fancyguy/webroot-installer": "1.0.0"
  },
  "require": {
    "wpackagist-plugin/advanced-custom-fields": "4.4.11",
    ...etc
  },
  "extra": {
    "webroot-dir": ".",
    "webroot-package": "wordpress",
    "installer-paths": {
      "library/plugins/{$name}": [ "type:wordpress-plugin" ]
    }
  },
  "scripts": { ...}
}

here is the composer.json using composer-custom-directory-installer.

{
  "name": "the/name",
  "description": "description",
  "repositories": [
    {
      "type": "composer",
      "url": "https://wpackagist.org"
    },
    {
      "type": "package",
      "package": {
        "name": "wordpress",
        "type": "library",
        "version": "4.7.5",
        "dist": {
          "type": "zip",
          "url": "https://github.com/WordPress/WordPress/archive/4.7.5.zip"
        }
      }
    }
  ],
  "require-dev": {
    "mnsami/composer-custom-directory-installer": "1.1.*",
    "wordpress": "4.7.5"
  },
  "require": {
    "wpackagist-plugin/advanced-custom-fields": "4.4.11",
    ...etc
  },
  "extra": {
    "installer-paths": {
      "library/plugins/{$name}": [ "type:wordpress-plugin" ],
      "./": [ "wordpress" ]
    }

  },
  "scripts": { ... }
}
2

2 Answers

1
votes

I believe that thats how composer installers are intended to work. From the reading I've done it erases the old files to ensure that there isn't any cross contamination of versions. Its not like a git checkout/merge that would handle the differences, but instead a clean install if needed (caching aside type thing).

I would recommend the approach of not keeping any files you are authoring inside the same folder you are installing things and instead take the approach of having everything install and compile into a distribution folder.

Leverage tools like web pack or gulp, plus NPM and composer to handle all your compiling, and keep your distribution folder as something that can be blown away and replicated. It adds some overhead, but allows for all the tools to interact without causing file loss.

For example my typical repos look as follows. With all the different tools in place, the dist is entirely gitignored, but composer and build tools compile into it. From there if I want to FTP or use CI/CD I can, but I have a clean folder that is never stored and contains the entire project once I run the tools.

/dist
/src (where any files I'm authoring reside)
composer.json
package.json
gulpfile.js
etc...

QUICK UPDATE TO THIS. Been doing more playing around myself. It appears that the extended versions of composer installers won't install overtop of other directories. I've ended up doing the following which seems to work every time (still need to test with a deployment though)

"extra": {
    "installer-types": ["cms-core"],
    "installer-paths": {
        "public/wp": ["type:cms-core"],
        "public/wp-content/plugins/{$name}/": ["type:wordpress-plugin"],
        "public/wp-content/themes/{$name}/": ["type:wordpress-theme"]
    }
},
"repositories":[
    {
        "type": "composer",
        "url": "https://wpackagist.org"
    },
    {
        "type": "package",
        "package": {
            "name": "wordpress",
            "type": "cms-core",
            "version": "4.7.5",
            "dist": {
                "type": "zip",
                "url": "https://github.com/WordPress/WordPress/archive/4.7.5.zip"
            }
        }
    }
],
"require": {
    "composer/installers": "^1.3",
    "oomphinc/composer-installers-extender": "^1.1",
    "wpackagist-plugin/wordpress-seo": "^4.8",
    "wordpress": "^4.7"
},
"scripts": {
    "post-update-cmd": [
        "cp -R public/wp/ public && rm -rf public/wp"
    ]
}`
0
votes

For any new visitors trying to use Wordpress with Composer check out https://github.com/johnpbloch/wordpress for an implementation using a custom Composer installer. Full details and examples can be found at https://composer.rarst.net/, but the gist of it is:

composer.json:

{
    "name"        : "my/wordpress-project",
    "description" : "Test project for WordPress stack via Composer",
    "type"        : "project",
    "repositories": [
        {
            "type": "composer",
            "url" : "https://wpackagist.org"
        }
    ],
    "config"      : {
        "vendor-dir": "wp-content/vendor"
    },
    "require"     : {
        "johnpbloch/wordpress" : ">=5",
        "wpackagist-plugin/a-fresher-cache" : "*",
        "wpackagist-plugin/core-control" : "*",
        "wpackagist-plugin/monster-widget" : "*",
        "wpackagist-plugin/theme-check" : "*",
        "wpackagist-plugin/user-switching" : "*",
        "wpackagist-plugin/wcm-user-language-switcher" : "*"
    },
    "extra"       : {
        "installer-paths" : {
            "wp-content/plugins/{$name}/": ["type:wordpress-plugin"]
            "wp-content/themes/{$name}/": ["type:wordpress-theme"]
        }

        "wordpress-install-dir": "wp"
    }
}

This will install Wordpress into a /wp folder. You will need to modify your .htaccess to suit, as detailed here: https://wordpress.org/support/article/giving-wordpress-its-own-directory/

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^(www.)?example.com$
    RewriteCond %{REQUEST_URI} !^/wp/
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /wp/$1
    RewriteCond %{HTTP_HOST} ^(www.)?example.com$
    RewriteRule ^(/)?$ wp/index.php [L]
</IfModule>