WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
function node_wp($post_ID) {
    global $post;
    $post_title = get_the_title( $post_ID );
    $post_url = get_permalink( $post_ID );
    $json = array(
        'title_post' => $post_title, 
        'url' => $post_url
    $data = json_encode($json); 
    $server_url = get_option('$node_server_url_op');
    <script src="<?php echo $server_url; ?>"></script>
    <script type="text/javascript">
        var socket = io.connect('<?php echo $server_url; ?>');
        socket.on('connect', function(){
            socket.emit('sendchat', '<?php echo $data; ?>');
add_action('publish_post', 'node_wp');

When running my script I get this:

Warning: Cannot modify header information - headers already sent

Can you tell me how to properly add Javascript?

share|improve this question

By adding the function to the publish_post hook it executes when the post gets published as the page is refreshing. That's whats causing the headers already sent message.

This looks like some sort of chat that gets saved back to the database. If this needs to run on the front end of the site you could hook into wp_footer:

add_action('wp_footer', 'node_wp' );

In your node_wp function you can get the id using

global $post; $post_id = $post->ID;

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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