Posts Tagged ‘auto update’

July 28, 2010 | Getting P2 to Auto Update

Posted by joebeaudoin at 1:25 am | Permalink | Comments (0)
Topics: How-To,Programming,g33k | Tags: , , , , , , , ,

On certain servers that do not run PHP 5.2 or higher, the Auto Update feature of the P2 WordPress theme—which was recently implemented by FrakMedia! Productions for internal communications between all its workers—does not work. A quick run-down: the P2 theme is for micro-blogging. It is basically micro-blogging meets Twitter meets web forums, on Red Bull.

Now, “auto update” means that whenever you post something it will show up automatically (using AJAX and JSON), so you do not have to refresh the browser page.

In internally testing this, FrakMedia!’s PR coordinator Jon recently pointed this out to me, and I set out to find the problem. And it has a solution, which is highlighted in this thread on WordPress’s support site. It fixes the auto update problem when it comes to posting new threads, but it doesn’t deal with updating when replies to threads are posted.

So, you only need to fix one file. That file is p2/inc/ajax.php and need to be modified thusly:

1. At line 4, add:

require (ABSPATH . WPINC . ‘/class-json.php’);

define('DOING_AJAX', true);
@header('Content-Type: text/html; charset=' . get_option('blog_charset'));
require (ABSPATH . WPINC . '/class-json.php');

2. Starting at line 207, we need to change how the JSON encode is called. Here’s the block that you’ll need to make the changes to:

nocache_headers();
echo json_encode( array(
    'numberofnewposts' => $number_of_new_posts,
    'html' => $posts_html,
    'lastposttime' => gmdate('Y-m-d H:i:s')
) );

The following highlighted lines (lines 207 and 209) are the changes that need to be made.

$json = new Services_JSON();
nocache_headers();
echo $json->encode( array(
    'numberofnewposts' => $number_of_new_posts,
    'html' => $posts_html,
    'lastposttime' => gmdate('Y-m-d H:i:s')
) );

This solves the issue with auto updating any newly started threads only, not any replies to said threads. That’s the next step.

3. At line 318, remove this line:

echo json_encode( $json_data );

And add this block of code:

$json = new Services_JSON();
nocache_headers();
echo $json->encode( $json_data );

Once you’ve saved the file and uploaded it to your server, or edited through your WordPress dashboard’s editor (Appearance -> Editor), then this problem will be fixed. Simply refresh your browser, and start making things happen.

Important Note: As with any other theme, whenever you install an update from the publisher for said theme you will be overwriting any changes you made to the code. So make sure that you modify the code again after the update to reproduce the modifications noted in this article! Do keep in mind that should the files from said update be totally recoded, the above code may or may not work as intended. (The lines may not be there or may have moved to an entirely new file, for instance, and other fun stuff like that.)

Twidddle, Twaddle, Twitter, Twatter

Tweets for twats.

XBox Live

Gaming for geeks.

Friends Blogs

Check out my friends's blogs as well.