Archive for the ‘programming’ Category

Adding Threaded Comments for Author Pages on P2 Wordpress Theme

Sunday, May 10th, 2009
No Gravatar

P2 is one of the sweetest wordpress themes out there (see http://ma.tt/2009/05/how-p2-changed-automattic/), but for certain applications it needs tweaking.  At MyChances.net, we wanted to give each of our members their own blog so they could write about the college application process.  To accomplish this, we needed two additional features: 1) Threaded comments for author pages.  If members are going to feel like they have a legit blog, they need to have user comments on the articles they are writing.  2.)  If the logged in user is the author of the blog, that user should see the “Hi, watcha up to?” quick post box on their blog – NOT just on the main page (aka the “firehose” – thanks Twitter!).

Adding comments to author pages in the P2 wordpress theme is an easy process.  You just need a few minutes and a very small amount of programming knowledge.

Step 1:  Open up wp-content > themes > P2 > entry.php.

Step 2:  find the following line:  if ( ( is_home() || is_front_page() )) $withcomments = true;

Step 3: change the line to   if ( ( is_home() || is_front_page() || is_author() ) ) $withcomments = true;

Step 4: Save entry.php, and make sure that you upload it to your P2 directory.

Step 5: Profit.

If you need comments on the tags page, you can simply change the line to this:

if ( ( is_home() || is_front_page() || is_author() || is_tag() ) ) $withcomments = true;

Essentially you are adding in some logic so that if the page the user is looking at “is an author page” OR “is a tag page” then turn on the withcomments variable so the page is allowed to show the threaded comments feature which we all love.

You can see this functionality live here:

http://www.mychances.net/membership/blog/author/Brent/

and here:

http://www.mychances.net/membership/blog/tag/member-blogs/

Now for our second need, showing the quick post box on author’s own author page (if the logged in user is the author).

Step 1:  Open up wp-content > themes > P2 > author.php.

Step 2:  At the very top of the page, add this line in the php brackets like so:

<?
get_currentuserinfo();
?>

This allows you to access information about the current user, such as their username.  To let php see the username, you can access the data like this:

$current_user->user_login (So if you want to echo out the username of the logged in user, you can put <?=$current_user->user_login?>

Step 3:  Just below the line that has <div class=”sleeve_main”  id=”userpage”> insert this block of code:

<?
if( current_user_can( ‘publish_posts’ ) && $current_user->user_login==$author->user_nicename) require_once dirname( __FILE__ ) . ‘/post-form.php’;
?>

This checks two things.  1) Can the current user even post things.  2) If the current username is equal to that of the author.  If these criteria are met, it will display the “post form” which is the quick post box.  Otherwise, nothing shows up, so it just looks like a blog page for a normal visitor.

That’s it!  Please let me know if you have implementation questions.

Brent

  • Share/Bookmark

Forking Daemons in PHP

Thursday, April 30th, 2009
No Gravatar

Note: this is from http://bipinb.com/making-php-program-as-daemon.htm . It has been intermittently offline, so I’m archiving it here for future reference.


<?php
include_once('createdb.php');
declare(ticks=1);
$pid = pcntl_fork();
if ($pid == -1) {
die("could not fork");
} else if ($pid) {
exit(); // we are the parent
} else {
// we are the child
}
// detatch from the controlling terminal
if (posix_setsid() == -1) {
die("could not detach from terminal");
}
$posid=posix_getpid();
$fp = fopen("/var/run/process.pid", "w");
fwrite($fp, $posid);
fclose($fp);
// setup signal handlers
 pcntl_signal(SIGTERM, "sig_handler");
 pcntl_signal(SIGHUP, "sig_handler");
// loop forever performing tasks
 $dbobject = new DB();
 $dbobject->getCon();
 while (1) {
// do something interesting here, here i have called a function from other flile called "createdb.php"
$dbobject->CopyCallFiles();
}
 fclose($fp);
 function sig_handler($signo)
 {
switch ($signo) {
 case SIGTERM:
 // handle shutdown tasks
 exit;
 break;
 case SIGHUP:
 // handle restart tasks
 break;
 default:
 // handle all other signals
 }
}
?>
  • Share/Bookmark

Recursive str_replace in PHP

Thursday, March 20th, 2008
No Gravatar

I was looking for a recursive str_replace in php tonight and I couldn’t find any, so I wrote one. This takes the exact same parameters as str_replace, in the same order. It recursively searches the $subject, replacing $search with $replace, while keeping track of how many times ($count) it has replaced the $subject.


Example:

$count=0;
$string = "; ; I; am; ; ; ; ; become; ; ; ; ; ; ; ; ;";
string = str_replace_recursive('; ;',';',$string,$count);

echo $string;
echo $count;

Outputs:
string --> I; am; become;
count --> 12

(Note that this is not the best code to use for this situation; can you write a faster solution to this example using only plain vanilla str_replace?)

  • Share/Bookmark