A static build is a snapshot. StaticScope crawls WordPress, copies each page, rewrites addresses and leaves a folder of files. Your PHP is not there.
A feature survives only if a file, the browser or a service outside WordPress can answer it. Decide which before you write code.
The boundary
Ask one question of every feature: what does it need at the moment someone uses it?
- Nothing beyond the page. Tabs, filters, sorting. Do it in the browser, with the data in the markup or a JSON file.
- A request to somewhere. Form delivery, payments, comments. Send it to a service that owns the state.
- A live WordPress. A members area, a cart, an admin screen. Keep it on WordPress and link to it.
What does not move: nonces, admin-ajax, current_user_can(), the search query, comment submission, anything that reads the database after the build. A hidden field is not a permission check, because anyone can read it and send something else.
What the build changes
The crawler is not a browser. It fetches HTML, follows links, records assets and rewrites addresses. It does not run your JavaScript, submit your forms or wait for a fetch. Content that appears only after a request is not in the build.
That matters most for files loaded later. Pagefind’s index is written after the pages are prepared, so no page refers to it. The add-on registers /pagefind/ through staticscope_runtime_paths. Do the same for anything your script loads later.
Addresses are rewritten, including home_url(). That is why the examples use it rather than a hard-coded path: the same code works on WordPress, in the preview and on the published domain.
The crawler sees HTML, not JavaScript. If the content is not in the response, it is not in the index or the export. Render it in PHP or accept that the page has none.
Mark what gets indexed
Pagefind reads the prepared HTML and writes static index files. The browser downloads only the fragment a query needs.
Put data-pagefind-body on the element that holds the content you want to find. Put data-pagefind-ignore on the parts that repeat on every page.
<header class="site-header" data-pagefind-ignore>
...
</header>
<main id="main" data-pagefind-body>
<article>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
</article>
</main>
<footer class="site-footer" data-pagefind-ignore>
...
</footer>
Once any page has data-pagefind-body, Pagefind indexes only marked pages. A template you forget is invisible to search, with no error.
The Pagefind add-on marks single posts and pages when no page carries the attribute, and skips the search page. Mark your own templates and it leaves them alone.
Indexing the chrome is the usual mistake. A header with “Home, About, Contact” makes every page match “contact”. The add-on excludes comments, post navigation and post lists by default.
Build the index
The add-on runs Pagefind on staticscope_post_process_complete, after the pages are prepared and rewritten, so the index matches what gets published.
wp staticscope pagefind status
wp staticscope pagefind index
If the host forbids proc_open, run Pagefind in your deployment: npx pagefind --site path/to/exported-site. The add-on bundles Pagefind 1.6.0 for Linux, macOS and Windows; check its readme for the current version.
Zero indexed pages usually means every page was excluded, or the folder was empty.
The search page
The search page is an ordinary page with a form and a container. Use GET so the query is in the address and can be shared.
<form role="search" method="get" action="<?php echo esc_url( home_url( '/search/' ) ); ?>">
<label for="ssc-q">Search</label>
<input id="ssc-q" type="search" name="q">
<button type="submit">Search</button>
</form>
<div id="search-results" data-pagefind-bundle="<?php echo esc_url( home_url( '/pagefind/' ) ); ?>">
<p data-search-status>Search runs in your browser.</p>
<ol data-search-list hidden></ol>
</div>
The add-on’s block uses ?s= because WordPress search forms send it, and redirects old /?s= links. This code uses q. Either works if the form and script agree.
Leave the action as home_url() and let StaticScope rewrite it. A hard-coded /search/ breaks under a path, and the preview is always under one.
Query and render
The module imports Pagefind only when the container exists. The import is dynamic so a missing index shows a message instead of a blank page.
const container = document.querySelector( '#search-results' );
if ( container ) {
const bundle = new URL( container.dataset.pagefindBundle, document.baseURI );
const list = container.querySelector( '[data-search-list]' );
const status = container.querySelector( '[data-search-status]' );
let pagefind = null;
function setStatus( message ) {
status.textContent = message;
status.hidden = message === '';
}
function render( results ) {
list.replaceChildren();
results.forEach( ( result ) => {
const item = document.createElement( 'li' );
const link = document.createElement( 'a' );
const excerpt = document.createElement( 'p' );
link.href = result.url;
link.textContent = result.meta && result.meta.title ? result.meta.title : result.url;
// Pagefind marks the matched words in the excerpt it returns.
excerpt.innerHTML = result.excerpt;
item.append( link, excerpt );
list.append( item );
} );
list.hidden = results.length === 0;
}
async function run( query ) {
if ( ! pagefind ) {
try {
pagefind = await import( new URL( 'pagefind.js', bundle ).href );
await pagefind.init();
} catch ( error ) {
pagefind = null;
setStatus( 'Search is not available yet. Build the site, then try again.' );
return;
}
}
const term = query.trim();
if ( term === '' ) {
list.replaceChildren();
setStatus( 'Type a word to search.' );
return;
}
setStatus( 'Searching…' );
try {
const search = await pagefind.search( term );
const results = await Promise.all(
search.results.slice( 0, 10 ).map( ( result ) => result.data() )
);
render( results );
setStatus( results.length === 0 ? 'Nothing matched “' + term + '”.' : '' );
} catch ( error ) {
setStatus( 'Search failed. Try again, or use the menu.' );
}
}
const form = document.querySelector( 'form[role="search"]' );
const initial = new URLSearchParams( window.location.search ).get( 'q' );
if ( initial ) {
run( initial );
}
if ( form ) {
form.addEventListener( 'submit', ( event ) => {
event.preventDefault();
const term = form.elements.q.value;
const url = new URL( window.location.href );
url.searchParams.set( 'q', term );
window.history.replaceState( null, '', url );
run( term );
} );
}
}
Three states matter. No query: say what to do. No matches: repeat the term. No index: send them to the build, because that is a deployment problem.
result.data() returns url, excerpt, content and meta. meta.title is whatever the page’s title element gave Pagefind, so fall back to the URL. The excerpt contains <mark>; set it with innerHTML. Everything else uses textContent.
The add-on ships a different front end: Pagefind Components, loaded from /pagefind/pagefind-component-ui.js. The code above is the ES module API. Both read the same index; pick one.
Forms with Web3Forms, end to end
A form needs a receiver. Web3Forms accepts a POST from any host and forwards it to the inbox tied to a public access key. No WordPress, no nonce, nothing stored.
The markup is a normal form. The key goes in a hidden field, with the subject and from name if you want them fixed.
<form class="ssc-form" action="https://api.web3forms.com/submit" method="post" data-ss-form>
<input type="hidden" name="access_key" value="<?php echo esc_attr( $access_key ); ?>">
<input type="hidden" name="subject" value="New enquiry from the website">
<input type="hidden" name="from_name" value="Example site">
<input type="hidden" name="redirect" value="<?php echo esc_url( home_url( '/thanks/' ) ); ?>">
<p>
<label for="ssc-name">Name</label>
<input id="ssc-name" name="name" type="text" autocomplete="name" required>
</p>
<p>
<label for="ssc-email">Email</label>
<input id="ssc-email" name="email" type="email" autocomplete="email" required>
</p>
<p>
<label for="ssc-message">Message</label>
<textarea id="ssc-message" name="message" rows="6" required></textarea>
</p>
<div class="ssc-honeypot" aria-hidden="true">
<label for="ssc-botcheck">Leave this field empty</label>
<input id="ssc-botcheck" name="botcheck" type="text" tabindex="-1" autocomplete="off">
</div>
<button type="submit">Send</button>
<p role="status" aria-live="polite" data-ss-status hidden></p>
</form>
The honeypot is a real text input inside a wrapper you hide with CSS. A type="hidden" input is ignored by most bots, which defeats the point. Keep it out of the tab order and away from screen readers.
The redirect field is the no-JavaScript path. Web3Forms uses it after a successful POST, so the visitor gets a thank-you page instead of JSON.
const form = document.querySelector( '[data-ss-form]' );
if ( form ) {
const status = form.querySelector( '[data-ss-status]' );
const button = form.querySelector( 'button[type="submit"]' );
form.addEventListener( 'submit', async ( event ) => {
event.preventDefault();
if ( form.dataset.sending === '1' || ! form.reportValidity() ) {
return;
}
form.dataset.sending = '1';
button.setAttribute( 'aria-disabled', 'true' );
status.hidden = false;
status.textContent = 'Sending…';
const controller = new AbortController();
const timer = window.setTimeout( () => controller.abort(), 15000 );
try {
const response = await fetch( form.action, {
method: 'POST',
headers: { Accept: 'application/json' },
body: new FormData( form ),
signal: controller.signal,
} );
const result = await response.json();
if ( ! response.ok || result.success !== true ) {
throw new Error( result.message || 'The form was refused.' );
}
form.reset();
status.textContent = result.message || 'Thank you. Your message has been sent.';
} catch ( error ) {
status.textContent = 'The message did not send. Email hello@example.com instead.';
} finally {
window.clearTimeout( timer );
form.dataset.sending = '';
button.removeAttribute( 'aria-disabled' );
}
} );
}
The code sends FormData, so the provider reads the same field names as the plain form. A 200 with success: false is still a failure, which the explicit check catches. Do not clear the form until the provider confirms.
The access key is public. The other key is not. A key starting w3f_live_ is the Submissions API key and can read submissions. It must never appear in page source, a repository or a hidden field.
On WordPress, store the key in an option only administrators can change, or in STATICSCOPE_FORMS_WEB3FORMS_KEY. The Forms add-on does this and protects the form’s action from URL rewriting.
A block that survives the build
A dynamic block is fine when the dynamic part happens during the build. render_callback runs while WordPress serves the page to the crawler, and that HTML is what ships. The output must depend on saved attributes and published content, not on who is asking.
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 3,
"name": "example/callout",
"title": "Callout",
"category": "design",
"attributes": {
"heading": { "type": "string", "default": "" },
"body": { "type": "string", "default": "" }
},
"render": "file:./render.php",
"style": "file:./style.css"
}
<?php
$heading = isset( $attributes['heading'] ) ? $attributes['heading'] : '';
$body = isset( $attributes['body'] ) ? $attributes['body'] : '';
?>
<aside <?php echo get_block_wrapper_attributes( [ 'class' => 'ssc-callout' ] ); ?>>
<?php if ( $heading !== '' ) : ?>
<h3 class="ssc-callout__heading"><?php echo esc_html( $heading ); ?></h3>
<?php endif; ?>
<p class="ssc-callout__body"><?php echo wp_kses_post( $body ); ?></p>
</aside>
No user, nonce or request context in that render. If you need the latest posts, query them in the render and accept that the list changes only at build time. A REST call in view.js has no API to call.
The same test catches a render that checks is_user_logged_in() or current_user_can(). The crawler is anonymous, so one branch is baked in for everyone.
Enqueuing and passing values
Register scripts the normal way. Pass only public values, as data rather than hand-built JavaScript strings.
add_action( 'wp_enqueue_scripts', function () {
wp_enqueue_script(
'example-search',
plugins_url( 'assets/search.js', __FILE__ ),
[],
'1.0.0',
true
);
wp_localize_script( 'example-search', 'exampleSearch', [
'bundle' => home_url( '/pagefind/' ),
'strings' => [
'empty' => __( 'Nothing matched your search.', 'example' ),
],
] );
} );
wp_localize_script() prints JSON before a classic script. For a module, use data- attributes or wp_add_inline_script() with wp_json_encode(). Escape at output: esc_html for text, esc_attr for attributes, esc_url for addresses and wp_kses_post for rich content.
Enqueue only where the code runs. has_block() and is_page_template() keep it off other pages. Every enqueued file is crawled, rewritten and downloaded.
Capability checks
current_user_can() still matters on WordPress. Use it on settings screens, with a nonce, and check the nonce again on arrival. None of that exists in the static folder.
For public writes, the service is the boundary. The provider checks its key, spam rules and rate limits. If you run the endpoint, validate the method, origin and field lengths yourself.
Test the built output
Test the folder, not the WordPress page.
- Run
wp staticscope check. It reports missing files, references back to the WordPress host and files nothing links to. - View source on a deep page. No
wp-json,admin-ajax.php,localhostor WordPress uploads URL. - Turn JavaScript off. The form should still post; search may say it needs JavaScript, as long as it says so.
- Search for a word that only appears in the footer. If every page matches, the chrome is indexed.
- Throttle the network, then block the endpoint. The error state must appear and the button must work again.
- Open the site from a subfolder. Absolute paths pass on a root domain and fail there.
Where each job runs
| Work | Runs | How |
|---|---|---|
| Page HTML and content | At build time | The crawler fetches and rewrites each page |
| Pagefind index | At build time | Pagefind reads the prepared folder |
| Search query | In the browser | A dynamic import of pagefind.js |
| Form validation | In the browser | Your script, with HTML validation as the fallback |
| Form delivery | At the form service | A POST to Web3Forms |
| Logged-in content | Must stay dynamic | Keep it on WordPress and link to it |
| Nonces and admin-ajax | Must stay dynamic | No PHP is published, so do not call it |
Next
The search add-on covers setup, indexing and styling. The Forms add-on is the supported version of the form code. To decide what to move first, read plugin compatibility.