Last active
March 6, 2019 17:08
-
-
Save davidsword/8821bc8a8adee3abc71cf4d0dee896f0 to your computer and use it in GitHub Desktop.
WordPress - Pass PHP values into JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
wp_scripts()->add_data( | |
'my_js_theme', | |
'my_theme_data', | |
sprintf( | |
'var my_theme_data = %s;', | |
wp_json_encode( [ | |
'nonce' => wp_create_nonce( 'wp_rest' ), | |
'path' => $path, | |
'blogname' => get_option( 'blogname' ), | |
'blogdescription' => get_option( 'blogdescription' ), | |
'posts_per_page' => get_option( 'posts_per_page' ), | |
'URL' => array( | |
'api' => esc_url_raw( get_rest_url( null, '/wp/v2' ) ), | |
'root' => esc_url_raw( $url ), | |
), | |
'user_id' => get_current_user_id(), | |
'user_email' => wp_get_current_user()->user_email, | |
'any_other_php_var' => 'value from php for js here', | |
] ) | |
) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment