1

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 );
  • I this in the admin or on the front end. Also, this code should be in an action, either admin_init on the admin side or wp_enqueue_scripts if its on the front end. See codex.wordpress.org/Function_Reference/wp_enqueue_script – Manny Fleurmond May 4 '15 at 3:32
  • font end — it is in an enqueue script (see first line..) – js111 May 4 '15 at 21:57
  • Only thing I can think of is that the post isn't ready yet in that action – Manny Fleurmond May 5 '15 at 1:18
1

Your code should be in an action callback function:

function wpse186202_enqueue_scripts(){
    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 );
}

add_action( 'wp_enqueue_scripts', 'wpse186202_enqueue_scripts' ); //Front end enqueueing
add_action( 'admin_enqueue_scripts', 'wpse186202_enqueue_scripts' ); //Admin enqueueing

The action will call the function at the right moment for it to be enqueued properly. Choose the one that works for where you want it enqueued.

| improve this answer | |
  • see updated question.. there is no bug in the function itself (its working with static numbers), just in how im trying to pull those variables? – js111 May 4 '15 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.