-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReactWithWP.php
More file actions
39 lines (37 loc) · 986 Bytes
/
Copy pathReactWithWP.php
File metadata and controls
39 lines (37 loc) · 986 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?php
/**
* Plugin.
*
* @package reactplug
* @wordpress-plugin
*
* Plugin Name: React With WP Plugin
* Description: ReactJS plugin for Wordpress
* Author: Wes Huber
* Author URL: https://weshuber.com
* Version: 1.0
*/
function displayReactApp() {
$current_user = (array) wp_get_current_user()->roles;
ob_start();
?>
<div id="my-react-app"></div>
<?php return ob_get_clean();
}
// register shortcode
add_shortcode('displayReactApp', 'displayReactApp');
add_action('wp_enqueue_scripts', 'enq_react');
function enq_react(){
wp_register_script('display-react',
plugin_dir_url( __FILE__ ) . '/build/index.js',
['wp-element'],
rand(), // Change this to null for production
true);
$current_user = wp_get_current_user();
$data = array(
'email' => $current_user->user_email,
);
wp_localize_script( 'display-react', 'object', $data ); //localize script to pass PHP data to JS
wp_enqueue_script( 'display-react' );
}
?>