Im trying to pass some variables (author of the post, category of the post, and title of the post) to a js file using wp_localize_script.
I cannot, for the life of me get these correct. How can I pull this data about the current post that is being viewed?
Current code:
wp_enqueue_script( 'stats', get_stylesheet_directory_uri() .'/js/t5-demo.js' , array( 'jquery' ), '1.0.0', true );
$categories = implode( ', ', wp_list_pluck( get_the_category( get_the_ID() ), 'name' ) );
$datatoBePassed = array(
'author' => get_queried_object()->post_author,
'category' => $categories,
'title' => single_post_title( '', false )
);
wp_localize_script( 'stats', 'php_vars', $datatoBePassed );
Below is an example of what does work (just sticking static numbers in), so I believe it has something to do with how im trying to pull those variables about the post that is being viewed..
wp_enqueue_script( 'stats', get_stylesheet_directory_uri() .'/js/t5-demo.js' , array( 'jquery' ), '1.0.0', true );
$categories = implode( ', ', wp_list_pluck( get_the_category( get_the_ID() ), 'name' ) );
$datatoBePassed = array(
'author' => '10',
'category' => '11',
'title' => '12'
);
wp_localize_script( 'stats', 'php_vars', $datatoBePassed );
admin_initon the admin side orwp_enqueue_scriptsif its on the front end. See codex.wordpress.org/Function_Reference/wp_enqueue_script