Skip to content

Commit 396a970

Browse files
committed
add headless theme to wp setup
1 parent 7e61979 commit 396a970

File tree

5 files changed

+141
-0
lines changed

5 files changed

+141
-0
lines changed

wordpress/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ RUN curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli
1313
# Copy the revalidation plugin
1414
COPY next-revalidate /var/www/html/wp-content/plugins/next-revalidate
1515

16+
# Copy the headless redirect theme
17+
COPY theme /var/www/html/wp-content/themes/nextjs-headless
18+
1619
# Copy the setup scripts
1720
COPY setup.sh /usr/local/bin/setup-wordpress.sh
1821
COPY entrypoint.sh /usr/local/bin/custom-entrypoint.sh

wordpress/setup.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ if ! wp plugin is-active next-revalidate --allow-root 2>/dev/null; then
2929
wp plugin activate next-revalidate --allow-root
3030
fi
3131

32+
# Activate the headless theme
33+
current_theme=$(wp theme list --status=active --field=name --allow-root 2>/dev/null)
34+
if [ "$current_theme" != "nextjs-headless" ]; then
35+
echo "Activating Next.js Headless theme..."
36+
wp theme activate nextjs-headless --allow-root
37+
fi
38+
3239
# Configure the plugin if NEXTJS_URL is set
3340
if [ -n "$NEXTJS_URL" ]; then
3441
echo "Configuring Next.js Revalidation plugin..."

wordpress/theme/functions.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
/**
3+
* Next.js Headless Theme
4+
*
5+
* Redirects all frontend requests to the Next.js application.
6+
* Allows admin, login, REST API, and other WordPress internals.
7+
*/
8+
9+
// Redirect frontend requests to Next.js
10+
add_action('template_redirect', function () {
11+
// Allow WordPress admin area
12+
if (is_admin()) {
13+
return;
14+
}
15+
16+
// Allow login/logout pages
17+
if (strpos($_SERVER['REQUEST_URI'], 'wp-login') !== false ||
18+
strpos($_SERVER['REQUEST_URI'], 'wp-signup') !== false ||
19+
strpos($_SERVER['REQUEST_URI'], 'wp-activate') !== false) {
20+
return;
21+
}
22+
23+
// Allow REST API
24+
if (strpos($_SERVER['REQUEST_URI'], 'wp-json') !== false ||
25+
strpos($_SERVER['REQUEST_URI'], rest_get_url_prefix()) !== false) {
26+
return;
27+
}
28+
29+
// Allow cron
30+
if (strpos($_SERVER['REQUEST_URI'], 'wp-cron') !== false) {
31+
return;
32+
}
33+
34+
// Allow AJAX requests
35+
if (defined('DOING_AJAX') && DOING_AJAX) {
36+
return;
37+
}
38+
39+
// Allow XML-RPC (if needed for some integrations)
40+
if (strpos($_SERVER['REQUEST_URI'], 'xmlrpc.php') !== false) {
41+
return;
42+
}
43+
44+
// Get Next.js URL from environment
45+
$nextjs_url = getenv('NEXTJS_URL');
46+
47+
if ($nextjs_url) {
48+
// Redirect to Next.js with 301 (permanent redirect)
49+
wp_redirect($nextjs_url, 301);
50+
exit;
51+
}
52+
});
53+
54+
// Remove unnecessary frontend features for headless
55+
add_action('after_setup_theme', function () {
56+
// Remove emoji scripts
57+
remove_action('wp_head', 'print_emoji_detection_script', 7);
58+
remove_action('wp_print_styles', 'print_emoji_styles');
59+
60+
// Remove feed links
61+
remove_action('wp_head', 'feed_links', 2);
62+
remove_action('wp_head', 'feed_links_extra', 3);
63+
64+
// Remove RSD link
65+
remove_action('wp_head', 'rsd_link');
66+
67+
// Remove wlwmanifest link
68+
remove_action('wp_head', 'wlwmanifest_link');
69+
70+
// Remove WordPress version
71+
remove_action('wp_head', 'wp_generator');
72+
});

wordpress/theme/index.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Fallback template
4+
*
5+
* This should rarely be seen - the redirect in functions.php
6+
* should catch all frontend requests.
7+
*/
8+
9+
$nextjs_url = getenv('NEXTJS_URL');
10+
?>
11+
<!DOCTYPE html>
12+
<html>
13+
<head>
14+
<meta charset="UTF-8">
15+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
16+
<title>Headless WordPress</title>
17+
<style>
18+
body {
19+
font-family: system-ui, -apple-system, sans-serif;
20+
display: flex;
21+
align-items: center;
22+
justify-content: center;
23+
min-height: 100vh;
24+
margin: 0;
25+
background: #f5f5f5;
26+
}
27+
.container {
28+
text-align: center;
29+
padding: 2rem;
30+
}
31+
h1 { color: #333; }
32+
p { color: #666; }
33+
a {
34+
color: #0070f3;
35+
text-decoration: none;
36+
}
37+
a:hover { text-decoration: underline; }
38+
</style>
39+
</head>
40+
<body>
41+
<div class="container">
42+
<h1>Headless WordPress</h1>
43+
<p>This WordPress installation is configured for headless use.</p>
44+
<?php if ($nextjs_url): ?>
45+
<p><a href="<?php echo esc_url($nextjs_url); ?>">Visit the frontend</a></p>
46+
<?php endif; ?>
47+
<p><a href="<?php echo esc_url(admin_url()); ?>">WordPress Admin</a></p>
48+
</div>
49+
</body>
50+
</html>

wordpress/theme/style.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/*
2+
Theme Name: Next.js Headless
3+
Theme URI: https://github.com/9d8dev/next-wp
4+
Description: Minimal theme that redirects frontend requests to Next.js. For headless WordPress only.
5+
Version: 1.0.0
6+
Author: 9d8
7+
Author URI: https://9d8.dev
8+
License: MIT
9+
*/

0 commit comments

Comments
 (0)