You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
|\$slug | string | The page slug where the React app will live on the site. The loader will also reserve this slug with WordPress, preventing any new posts from being made at the same URL. If any existing posts share the defined slug, they will not be able to be accessed on the front-end of the site once rewrite rules are flushed. |
66
66
|\$root_id | string | The id of the root element that the React app should mount to. By default, Create React App has this as `'root'`. |
67
-
|\$plugin_dir_path | string | The absolute path to the plugin directory that contains the react app. In most situations, this should be `plugin_dir_path( __FILE__ )`. |
67
+
|\$cra_directory | string | The absolute path to the plugin directory that contains the react app. In most situations, this should be `plugin_dir_path( __FILE__ )`. This can also be a URL to the base directory of a React app on a different website.|
68
68
|\$role | string | The WordPress user role required to view the page. If a user tries to access the page without this role, they will be redirected to the site's [home_url()](https://developer.wordpress.org/reference/functions/home_url/). If no authentication is needed, this should be set as `'nopriv'`. |
69
69
|\$callback | callable | Optional callback function. This is only fired on the registered page before the React app assets are enqueued. |
70
70
|\$wp_permalinks | array | Optional array of subdirectories off of the defined slug that we DO WANT WordPress to handle. |
When a React plugin is registered with this loader plugin, a [virtual page](https://metabox.io/how-to-create-a-virtual-page-in-wordpress/) is created within WordPress. As a result, this new page will not show up within the regular pages/posts list in wp-admin. Because of the nature of creating a virtual page by adding new rewrite rules to WordPress, the rewrite rules will need to be flushed before the new page will be accessible.
@@ -106,6 +105,24 @@ wp rewrite flush
106
105
107
106
Trailing slash has been removed for registerd React app pages. This was done in an effort to create consistency in behavior with create-react-app's node-server structure (the environment that fires up when you run `npm start`).
108
107
108
+
#### Remote React App Support
109
+
110
+
Support for loading React apps hosted on other websites is available as of version __1.4.0__.
111
+
112
+
This can be achived by using a URL to the root of the React app For the 3rd argument in the register method.
113
+
If your React app's asset-manifest.json is https://example.org/my-react-app/asset-manifest.json, use the first part of the URL (omit asset-manifest.json).
Copy file name to clipboardExpand all lines: lib/API.php
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -15,20 +15,20 @@ class API {
15
15
*
16
16
* @param string $slug The slug to tell WordPress to stop handling so react can handle routing.
17
17
* @param string $root_id The id of the root element we will be mounting our react app to.
18
-
* @param string $plugin_dir_path The absolute path to the plugin directory that contains the react app.
18
+
* @param string $cra_directory The absolute path to the plugin directory that has the CRA based react app. Can also be a URL to a remote CRA base react app.
19
19
* @param string $role The role required to view the page.
20
20
* @param callable $callback The callback function that fires before assets are enqueued to the page.
21
21
* @param array $wp_permalinks An array of subdirectories off of the defined slug that we DO WANT WordPress to handle.
if ( ! array_key_exists( 'entrypoints', $assets ) ) {
139
+
trigger_error( 'React App Loader: Entrypoints key was not found within your react app\'s asset-manifest.json. This may indicate that you are using an unsupported version of react-scripts. Your react app should be using react-scripts@3.2.0 or later.', E_USER_WARNING ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_trigger_error
140
+
return [];
141
+
}
142
+
143
+
$filtered_assets = array_map(
144
+
function( $asset_path ) use ( $base_url ) {
145
+
return"$base_url/$asset_path";
146
+
},
147
+
array_values( $assets['entrypoints'] )
148
+
);
149
+
150
+
return$filtered_assets;
151
+
}
152
+
95
153
/**
96
154
* Attempt to load a file at the specified path and parse its contents as JSON.
97
155
*
98
156
* @param string $path The path to the JSON file to load.
99
-
* @return array|null;
157
+
* @return array|null
100
158
*/
101
159
publicstaticfunctionload_asset_file( $path ) {
102
160
if ( ! file_exists( $path ) ) {
@@ -117,7 +175,7 @@ public static function load_asset_file( $path ) {
Copy file name to clipboardExpand all lines: lib/Virtual_Page.php
+39-17Lines changed: 39 additions & 17 deletions
Original file line number
Diff line number
Diff line change
@@ -25,11 +25,11 @@ class Virtual_Page {
25
25
private$root_id;
26
26
27
27
/**
28
-
* The absolute path to the plugin directory that contains the react app.
28
+
* The absolute path to the plugin directory that has the CRA based react app. Can also be a URL to a remote CRA base react app.
29
29
*
30
30
* @var string
31
31
*/
32
-
private$plugin_dir_path;
32
+
private$cra_directory;
33
33
34
34
/**
35
35
* The user role required to view to view this page.
@@ -65,19 +65,19 @@ class Virtual_Page {
65
65
*
66
66
* @param string $slug The slug to tell WordPress to stop handling so react can handle routing.
67
67
* @param string $root_id The id of the root element we will be mounting our react app to.
68
-
* @param string $plugin_dir_path The absolute path to the plugin directory that contains the react app.
68
+
* @param string $cra_directory The absolute path to the plugin directory that has the CRA based react app. Can also be a URL to a remote CRA base react app.
69
69
* @param string $role The role required to view the page.
70
70
* @param callable $callback The callback function that fires before assets are enqueued to the page.
71
71
* @param array $wp_permalinks An array of subdirectories off of the defined slug that we DO WANT WordPress to handle.
0 commit comments