如何使用comment_post_form获取Comment-ID

时间:2014-09-15 19:16:00

标签: php wordpress hook

我正在寻找一种方法来挂钩comment_post_formupdate_comment_meta,如果有的话,但我无法弄清楚如何获得评论ID。

该函数位于functions.php

function add_comment_drawing() {

    $drawingsave = $_POST['drawinginput'];
    if ($drawingsave == 'Das Bild wird gespeichert'){
    update_comment_meta(  $comment->ID, 'drawingurl', 'Brad' );
    }


 }
add_action('comment_post', 'add_comment_drawing');

提前致谢

3 个答案:

答案 0 :(得分:3)

Comment Actions

  

comment_post
  在评论保存在数据库中之后运行。 操作功能参数:评论ID,审批状态("垃圾邮件"或0/1表示拒登/已批准)

function add_comment_drawing($comment_id){
    $drawingsave = isset($_POST['drawinginput']) ? $_POST['drawinginput'] : false;
    if($drawingsave == 'Das Bild wird gespeichert'){
        update_comment_meta($comment_id, 'drawingurl', 'Brad' );
    }
}
add_action('comment_post', 'add_comment_drawing', 10, 1);

答案 1 :(得分:1)

你错过了函数中的参数。

function add_comment_drawing( $comment_id, $approval_status ) {

    $drawingsave = $_POST['drawinginput'];
    if ($drawingsave == 'Das Bild wird gespeichert'){
        update_comment_meta(  $comment_id, 'drawingurl', 'Brad' );
    }
}
add_action( 'comment_post', 'add_comment_drawing', 10, 2 );

答案 2 :(得分:1)

“我无法弄清楚如何获取评论ID”

  • add_action :向队列添加操作
  • do_action :执行排队操作的代码中的某处(找到它!)

在这种情况下:/wp-includes/comment.php第1811行。

上面大约10行:

$comment_ID = wp_insert_comment($commentdata);
    if ( ! $comment_ID ) {
        return false;
    }

所以你的comment_ID是由“add_action”提供的(在这种情况下是由wp_insert_comment方法/函数提供的;所以你可以在你的函数中使用它,而不是相反。例如,将comment_ID记录到文件或东西。

“add_action如何工作/参数?”

打开/wp-includes/plugin.php,搜索“add_action”,然后在第400行看到:

**
 * Hooks a function on to a specific action.
 *
 * Actions are the hooks that the WordPress core launches at specific points
 * during execution, or when specific events occur. Plugins can specify that
 * one or more of its PHP functions are executed at these points, using the
 * Action API.
 *
 * @uses add_filter() Adds an action. Parameter list and functionality are the same.
 *
 * @since 1.2.0
 *
 * @param string $tag The name of the action to which the $function_to_add is hooked.
 * @param callback $function_to_add The name of the function you wish to be called.
 * @param int $priority optional. Used to specify the order in which the functions associated with a particular action are executed (default: 10). Lower numbers correspond with earlier execution, and functions with the same priority are executed in the order in which they were added to the action.
 * @param int $accepted_args optional. The number of arguments the function accept (default 1).
 */
function add_action($tag, $function_to_add, $priority = 10, $accepted_args = 1) {
    return add_filter($tag, $function_to_add, $priority, $accepted_args);
}

“_action如何工作/参数?”

同上。查看wp-includes / plugin.php并查看第427行:

/**
 * Execute functions hooked on a specific action hook.
 *
 * This function invokes all functions attached to action hook $tag. It is
 * possible to create new action hooks by simply calling this function,
 * specifying the name of the new hook using the <tt>$tag</tt> parameter.
 *
 * You can pass extra arguments to the hooks, much like you can with
 * apply_filters().
 *
 * @see apply_filters() This function works similar with the exception that
 * nothing is returned and only the functions or methods are called.
 *
 * @since 1.2.0
 *
 * @global array $wp_filter Stores all of the filters
 * @global array $wp_actions Increments the amount of times action was triggered.
 *
 * @param string $tag The name of the action to be executed.
 * @param mixed $arg,... Optional additional arguments which are passed on to the functions hooked to the action.
 * @return null Will return null if $tag does not exist in $wp_filter array
 */
function do_action($tag, $arg = '') {
    global $wp_filter, $wp_actions, $merged_filters, $wp_current_filter;

    if ( ! isset($wp_actions[$tag]) )
        $wp_actions[$tag] = 1;
    else
        ++$wp_actions[$tag];

    // Do 'all' actions first
    if ( isset($wp_filter['all']) ) {
        $wp_current_filter[] = $tag;
        $all_args = func_get_args();
        _wp_call_all_hook($all_args);
    }

    if ( !isset($wp_filter[$tag]) ) {
        if ( isset($wp_filter['all']) )
            array_pop($wp_current_filter);
        return;
    }

    if ( !isset($wp_filter['all']) )
        $wp_current_filter[] = $tag;

    $args = array();
    if ( is_array($arg) && 1 == count($arg) && isset($arg[0]) && is_object($arg[0]) ) // array(&$this)
        $args[] =& $arg[0];
    else
        $args[] = $arg;
    for ( $a = 2; $a < func_num_args(); $a++ )
        $args[] = func_get_arg($a);

    // Sort
    if ( !isset( $merged_filters[ $tag ] ) ) {
        ksort($wp_filter[$tag]);
        $merged_filters[ $tag ] = true;
    }

    reset( $wp_filter[ $tag ] );

    do {
        foreach ( (array) current($wp_filter[$tag]) as $the_ )
            if ( !is_null($the_['function']) )
                call_user_func_array($the_['function'], array_slice($args, 0, (int) $the_['accepted_args']));

    } while ( next($wp_filter[$tag]) !== false );

    array_pop($wp_current_filter);
}

所以围绕add_action / do_action / filter的主要“技巧”是 call_user_func_array()http://php.net/manual/en/function.call-user-func-array.php,一旦你理解了这个功能,你就会明白WP中这些功能的作用。

“怎么办_ ????工作”

所以一般来说:只看一下WordPress的源代码(这是它的开源优势)

另一个提示是使用IDE,比如Eclipse或Netbeans:它们可以在IDE中显示很多信息,最重要的是:在“运行时”期间,您可以通过调试器获得大量信息。 / p>