I´m searching for a way to hook into comment_post_form
and update_comment_meta
, if something, but i can´t figure out how to get the comment-ID.
The function is in the 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');
Thanks in advance
comment_post
Runs just after a comment is saved in the database. Action function arguments: comment ID, approval status ("spam", or 0/1 for disapproved/approved).
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);
You're missing the arguments in your function.
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 );
" i can´t figure out how to get the comment-ID"
in this case: /wp-includes/comment.php line 1811.
about 10 lines above you see:
$comment_ID = wp_insert_comment($commentdata);
if ( ! $comment_ID ) {
return false;
}
So your comment_ID is what is provided BY the "add_action" (in this case by the wp_insert_comment method/function; so that you can use it in your function, not the other way around. E.g. log the comment_ID to a file or something.
"How does add_action work / parameters ? "
Open /wp-includes/plugin.php, search for "add_action" and around line 400 you see:
**
* 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);
}
"How does do _action work / parameters ? "
idem. Look in wp-includes/plugin.php and look around line 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);
}
So the main "trick" around add_action / do_action / filter is call_user_func_array() : http://php.net/manual/en/function.call-user-func-array.php , once you understand that function, you understand what these functions in WP do.
"How does do _???? work "
So in general : just look at the source code of WordPress (that is the advantage its opensource)
another tip is to use an IDE, something like Eclipse or Netbeans: they can show you a lot of info in the IDE, and on top of that: during "runtime" you get a lot of info through the debugger.