0

I've been playing arround with procedures and tried putting the comment insert into a procedure. I've got some trouble and was wondering if you guys could take a look.

function wp_insert_comment($commentdata) {
$comment_ID = comment_ID;
$comment_post_ID = comment_post_ID;
$comment_author = comment_author;
$comment_author_email = comment_author_email;
$comment_author_url = comment_author_url;
$comment_author_IP = comment_author_IP;
$comment_date = comment_date;
$comment_date_gmt = comment_date_gmt;
$comment_content = comment_content;
$comment_karma = comment_karma;
$comment_approved = comment_approved;
$comment_agent = comment_agent;
$comment_type = comment_type;
$comment_parent = comment_parent;
$user_id = user_id;
global $wpdb;
extract(wp_unslash($commentdata), EXTR_SKIP);

if ( ! isset($comment_author_IP) )
    $comment_author_IP = '';
if ( ! isset($comment_date) )
    $comment_date = current_time('mysql');
if ( ! isset($comment_date_gmt) )
    $comment_date_gmt = get_gmt_from_date($comment_date);
if ( ! isset($comment_parent) )
    $comment_parent = 0;
if ( ! isset($comment_approved) )
    $comment_approved = 1;
if ( ! isset($comment_karma) )
    $comment_karma = 0;
if ( ! isset($user_id) )
    $user_id = 0;
if ( ! isset($comment_type) )
    $comment_type = '';
$wpdb->insert($wpdb->comments, '$comment_ID','$comment_post_ID', '$comment_author', '$comment_author_email', '$comment_author_url', '$comment_author_IP', '$comment_date', '$comment_date_gmt', '$comment_content', '$comment_karma', '$comment_approved', '$comment_agent', '$comment_type', '$comment_parent', '$user_id');

$id = (int) $wpdb->insert_id;

if ( $comment_approved == 1 )
    wp_update_comment_count($comment_post_ID);

$comment = get_comment($id);


do_action( 'wp_insert_comment', $id, $commen );

wp_cache_set( 'last_changed', microtime(), 'comment' );

return $id;

}

So here I only changed the variables on the top because it didn't work out for me with the array that was previously used. I send these variables to:

function _insert_replace_helper( $table, $comment_ID, $comment_post_ID, $comment_author, $comment_author_email, $comment_author_url, $comment_author_IP, $comment_date, $comment_date_gmt, $comment_content, $comment_karma, $comment_approved, $comment_agent, $comment_type, $comment_parent, $user_id, $format = null, $type = 'INSERT' ) {
    $sql = "CALL CommentsProcedure('$comment_ID', '$comment_post_ID', '$comment_author',     '$comment_author_email', '$comment_author_url', '$comment_author_IP', '$comment_date', '$comment_date_gmt', '$comment_content', '$comment_karma', '$comment_approved', '$comment_agent', '$comment_type', '$comment_parent', '$user_id')";

        return $this->query( $this->prepare( $sql, $comment_ID, $comment_post_ID, $comment_author, $comment_author_email, $comment_author_url, $comment_author_IP, $comment_date, $comment_date_gmt, $comment_content, $comment_karma, $comment_approved, $comment_agent, $comment_type, $comment_parent, $user_id ) );
                }

And this is my Storedprocedure

    DELIMITER $$

CREATE PROCEDURE `CommentsProcedure`(IN comment_ID bigint(20), IN comment_post_ID bigint(20), IN comment_author tinytext, IN comment_author_email VARCHAR(100), IN comment_author_url VARCHAR(200), IN comment_author_IP VARCHAR(100), IN comment_date datetime, IN comment_date_gmt datetime, IN comment_content text,
 IN comment_karma int(11), IN comment_approved VARCHAR(20), IN comment_agent VARCHAR(255), IN comment_type VARCHAR(20), IN comment_parent bigint(20), IN user_ID bigint(20))
BEGIN

    INSERT INTO wp_comments(comment_ID, comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_karma, comment_approved, comment_agent, comment_type, comment_parent, user_id)

    VALUES(comment_ID, comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_karma, comment_approved, comment_agent, comment_type, comment_parent, user_id);

END
$$

DELIMITER ;

I have no clue what i'm do wrong, would be helpful if someone could give me some guidance,I've been stuck for so long and im pulling my hair!(Sorry for the bad english, not native speaker, feel free to ask if something is unclear.) Thanks in advance

  • 1
    Please edit your post and fix your MarkUp - in the current state it's unreadable. Then, please revisit your code: $comment_ID = comment_ID; is no PHP. And you should turn on debugging for PHP and WordPress (php.net and the WordPress codex have info on that). – kaiser May 5 '14 at 0:47
  • Thanks for your comment, i turned on both the debugging. Tried to make the markup better, but it's acting up when it's such long lines. How would you suggest me to write when i declare the variables? – Simon B May 5 '14 at 3:24
0

Few notes here. 1, why are you re-declaring variables.

For example:

$comment_ID = comment_ID;
$comment_post_ID = comment_post_ID;
$comment_author = comment_author;
$comment_author_email = comment_author_email; 

Is all pointless, If a variable is set already, no reason to set it a second time. Secondly, unless you pass variables through to the function through arguments like so:

Function wp_insert_comment($some_var, $some_other var) {
}

wp_insert_comment($some_var, $some_other_var);

then once the function runs, those variables at the beginning will = nothing, as they aren't set.

Third, it looks like either you're missing the $ in the second set. If you're working within a class(OOP) , technically you can get away with this as Php will assume these are constants. However, given your code, I'd suggest the issue here is that those variables aren't being passed through to the function before hand.

Try adding echo $comment_ID, or any of the other variables, and see if anything shows up. If not, that's your issue. I suppose another method, if you really don't want to pass through arguments to the function is to use globals, however that's really bad practice, and can lead to big nightmares when it comes to debugging.

Personally, I like to pass arguments as arrays, that way they can be changed or new variables added, without having to change every instance of the function call. For example I'd do something like this:

function some_function ($array=array()) {
// do some stuff 
}

some_function (array('variable_1'=>$variable, 'variable_2'=>$variable_2));

it may be a bit of extra typing, but it's worth it since you make your functions much more flexible.

To answer your question, the reason your variables seem empty, is because you are addressing the array incorrectly. Assuming that $commentdata is the array passed, you'd address the variables like so:

$comment_ID = $commentdata['comment_ID'];
$comment_post_ID = $commentdata['comment_post_ID'];

etc etc. This is because, an array stores data in something called a key=>value pair. Try doing:

var_dump($commentdata);

right at the beginning of your function call, to get a better understanding of what I mean.

As it seems you're relatively new to php, I'd suggest utilizing the following resources, to make the learning a bit easier:

http://www.php.net/ or http://www.w3schools.com/PHP/

or take a look at some of the questions and answers in the php section of SO.

  • What you're saying about the variables being empty seems to be right, but why is it they are empty since the commentdata array(?) is being extracet and some of the variables in that array is being used in the same function. Why can't I just send those values to other function? Does that make sense? – Simon B May 5 '14 at 4:37
  • I edited my answer to answer this question. – Hybrid Web Dev May 5 '14 at 5:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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