WP2Static Documentation

repository·develop·Indexed 23 days ago

https://github.com/elementor/wp2static

A WordPress plugin for static site generation and deployment that transforms dynamic WordPress sites into static versions. Includes documentation on CLI workflow commands (detect, crawl, post_process, deploy), integration testing with Nix and direnv, and developer APIs such as SiteInfo for environment paths and SitemapParser for recursive URL discovery.

Tokens
2.9K
Snippets
5
Records
22
Agent score
80%

What's inside WP2Static

  1. Overview of the Integration Test System

    develop

    The integration test suite validates WP2Static's behavior using a real web server and database environment. The system spins up the following services:

    • Web Servers: NGINX running on localhost:7000, localhost:7001, and localhost:7002.
    • Database: MariaDB using the socket at mariadb/data/mysql.sock.
    • PHP: PHP-FPM using the socket at php/fpm.sock.

    Test Scenarios/Environments:

    • Standard WordPress: Accessible at localhost:7000.
    • Bedrock: Accessible at localhost:7001.
    • WordPress with Basic Auth: Accessible at localhost:7002.
  2. Install WP2Static

    develop

    You can install WP2Static using several methods depending on your workflow:

    • From source code: Clone the repository and run Composer to install dependencies.
    • Via Composer: Add it to your project using the standard Composer require command.
    • Installer Zip: Download a pre-built installer zip from the official website.
    • Custom Build: Compile your own installer zip from the source code.
  3. Configure SitemapParser via constructor

    develop

    When instantiating SitemapParser, you can provide configuration options to control its behavior, specifically regarding HTTP requests and parsing strictness.

    Constructor Signature

    public function __construct( $user_agent = self::DEFAULT_USER_AGENT, array $config = [] )

    Configuration Keys

    • guzzle: An associative array used to pass configuration to the underlying `WP2StaticGuzzleHttp

    ` client. This is primarily used to set HTTP headers.

    • guzzle['headers']['User-Agent']: Overrides the default User-Agent if not already set.
    • strict: A boolean flag. If set to true, the parser will only attempt to parse line-separated text strings if they are valid XML sitemaps. If false (default), it may treat lines as individual URLs.

    Note: The parser uses WP2StaticGuzzleHttp for requests and sets 'verify' => false by default for SSL verification.

  4. Retrieve discovered sitemaps and URLs

    develop

    After running the parser, you can extract the discovered data using the following methods:

    • getSitemaps(): Returns an associative array of discovered sitemaps. The keys are the sitemap URLs, and the values are arrays containing metadata like loc and lastmod.
    • getURLs(): Returns an associative array of all discovered URLs. The keys are the URL strings, and the values are arrays containing metadata such as loc, lastmod, changefreq, and priority.
  5. Customize site information via the `wp2static_siteinfo` filter

    develop

    You can modify or extend the core site information (paths and URLs) used by WP2Static by applying a filter to wp2static_siteinfo. This is useful if you need to override default paths for content, uploads, themes, or plugins during the plugin execution.

    The filter provides an associative array containing keys for both paths and URLs. Note that paths are automatically standardized to use forward slashes (/) when retrieved via getPath().

  6. Retrieve site paths and URLs using `SiteInfo`

    develop

    The WP2Static\SiteInfo class provides static methods to access the WordPress environment's filesystem paths and URLs. These methods ensure that paths are standardized (using / even on Windows) and URLs are correctly formatted.

    Available Keys

    When calling getPath($name) or getUrl($name), use the following base names:

    Base NamePath KeyURL Key
    Coresite_pathsite_url
    home_pathhome_url
    includes_pathincludes_url
    Contentcontent_pathcontent_url
    uploads_pathuploads_url
    Pluginsplugins_pathplugins_url
    Themesthemes_root_paththemes_root_url
    parent_theme_pathparent_theme_url
    child_theme_pathchild_theme_url

    Methods

    • SiteInfo::getPath(string $name): string: Returns the filesystem path for the given name. Throws WP2StaticException if the key does not exist.
    • SiteInfo::getUrl(string $name): string: Returns the URL for the given name. Throws WP2StaticException if the key does not exist.
    • SiteInfo::getAllInfo(): array: Returns the entire site information array.
  7. Check environment compatibility with `SiteInfo`

    develop

    Use the SiteInfo class to verify if the current WordPress environment meets the requirements for static generation:

    • SiteInfo::isUploadsWritable(): bool: Checks if the uploads_path exists and is writable.
    • SiteInfo::permallinksAreCompatible(): bool: Checks if the current WordPress permalink structure is compatible (specifically checking if it ends with a trailing slash).
    • SiteInfo::hasCURLSupport(): bool: Checks if the curl PHP extension is loaded.
  8. Rewrite host and protocol of a URL

    develop
    The rewriteHostAndProtocol(string $destination_url) method mutates the existing URL object in place. It updates the current object's host and scheme (protocol) to match those found in the provided $destination_url. This is useful when preparing URLs to point to a new destination site during the static generation process.