Satya Prakash
Satya Prakash

Reputation: 3502

PHP script for Wordpress for Comment Approval

I want to write PHP script (standalone) for comment approval.

Objective: I use Aksimet for comment filtering. After Akismet's filtering, few comments get passed and come to my email for approval. I will get comment ID from there and pass it to the script in get parameter (manually).

This way I do not need to login to WP every time. The script will just approve comment so there is not much risk or harm. If script works then It will take less time and I can approve comment any time even from office.


I tried setting the moderation bit in MySQL directly to see if it works or not! The answer is yes and no. It approves the comment but it does not refresh the post. So, my cache show post without new comment. I use Super cache plugin.

The challenge is to write script OUTSIDE of WP environment. So, that I can access the script without Admin login.

Please suggest trick for this and how to start for this.

Upvotes: 0

Views: 1524

Answers (2)

fddf
fddf

Reputation: 1

@param int $comment_id Comment ID. @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'delete'. @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false. @return bool False on failure or deletion and true on success.

Upvotes: 0

Bryan
Bryan

Reputation: 2211

I spent a little time and got this working. The key is that you can use the WP API in your script by including wp-load.php.

I have tested this below and it works in my Wordpress. It uses GET parameters 'commentid' and 'status'. I leave it to you to extend, test for error conditions, provide appropriate security mechanisms, etc.

<?php
include('wp-load.php');

echo 'Running comment approval...<br>';
$wp_error = false;

if(isset($_GET['commentid']) && isset($_GET['status']) )
{   


        $comment_id = $_GET['commentid'];
        $comment_status_to_set = $_GET['status'];

        $current_status = wp_get_comment_status( $comment_id );

        echo 'Current comment status is ' . $current_status .'<br>';
        echo 'Setting status to ' . $comment_status_to_set . '<br>';

        $res = wp_set_comment_status($comment_id, $comment_status_to_set, $wp_error);        

        $new_current_status = wp_get_comment_status( $comment_id ); 
        echo 'Now status is ' . $new_current_status . '<br>';
}
else
{
    echo 'No comment parameters passed in url';
}

?>

And here is the usage for wp_set_comment_status, from the source code:

  • @param int $comment_id Comment ID.
  • @param string $comment_status New comment status, either 'hold', 'approve', 'spam', or 'delete'.
  • @param bool $wp_error Whether to return a WP_Error object if there is a failure. Default is false.
  • @return bool False on failure or deletion and true on success.

Also, so those who are curious know how I went about solving this... basically I checked the /wp-includes/ directory of WP to see if there were any comment related files. There is a file called comment.php in there, which has most, if not all, of the main comment manipulation functions that the wordpress API uses.

Upvotes: 1

Related Questions