Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.
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('adduser');
            socket.emit('sendchat', '<?php echo $data; ?>');
        });
    </script>
    <?php
}
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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.