Wordpress duplicate comment detection - wordpress

Does anyone know how to disable duplicate comment detection in Wordpress (2.9.2)? I'm looking for a way to do this programatically without editing core files. We're adding comments via XMLRPC and the duplicate detection in wp-includes/comment.php (line 494) is causing issues during testing.
Thanks!

Actually, you don't need to edit ANY core files to do this. Just put these one filter and two tiny functions in your theme's functions.php file and duplicate comments will no longer be rejected.
add_filter( 'wp_die_handler', 'my_wp_die_handler_function', 9 ); //9 means you can unhook the default before it fires
function my_wp_die_handler_function($function) {
return 'my_skip_dupes_function'; //use our "die" handler instead (where we won't die)
}
//check to make sure we're only filtering out die requests for the "Duplicate" error we care about
function my_skip_dupes_function( $message, $title, $args ) {
if (strpos( $message, 'Duplicate comment detected' ) === 0 ) { //make sure we only prevent death on the $dupe check
remove_filter( 'wp_die_handler', '_default_wp_die_handler' ); //don't die
}
return; //nothing will happen
}

Currently, there are no hooks available to do this without editing core files.
The best way would be to comment out the duplicate check from wp-includes/comment.php

$dupe = "SELECT comment_ID FROM $wpdb->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_approved != 'trash' AND ( comment_author = '$comment_author' ";
if ( $comment_author_email )
$dupe .= "OR comment_author_email = '$comment_author_email' ";
$dupe .= ") AND comment_content = '$comment_content' LIMIT 1";

I had the same issue when replying in the backend on comments.
But just replying with the same comment on the frontend worked fine without changing anything.
Hope this might help someone.

Related

Wordpress function cannot call submit_button() as it ends with "undefinied function"

i am a real very newbie in coding and in Wordpress. Trying my first test plugin to understand basics. I am able to define plugin, register it, so I can see it in plugins, activate it.
My later goal is to be able to create custom form, save user-specific data to new DB table, and then enable reading/editing it.
I tried to follow the instructions from gmazzap placed here: https://wordpress.stackexchange.com/questions/113936/simple-form-that-saves-to-database
I just am having following error from WP in time of trying to display preview of new screen having a shortcode [userform] in it:
*Fatal error: Uncaught Error: Call to undefined function submit_button() in /data/web/virtuals/131178/virtual/www/subdom/test/system/wp-content/themes/twentytwentythree-child/functions.php:14
*
My functions.php of my theme now looks like this:
<?php
add_action('init', function() {
add_shortcode('userform', 'print_user_form');
});
function print_user_form() {
echo '<form method="POST">';
wp_nonce_field('user_info', 'user_info_nonce', true, true);
?>
All your form inputs (name, email, phone) goes here.
<?php
submit_button('Send Data');
echo '</form>';
}
add_action('template_redirect', function() {
if ( ( is_single() || is_page() ) &&
isset($_POST['user_info_nonce']) &&
wp_verify_nonce($_POST['user_info_nonce'], 'user_info')
) {
// you should do the validation before save data in db.
// I will not write the validation function, is out of scope of this answer
$pass_validation = validate_user_data($_POST);
if ( $pass_validation ) {
$data = array(
'name' => $_POST['name'],
'email' => $_POST['email'],
'phone' => $_POST['phone'],
);
global $wpdb;
// if you have followed my suggestion to name your table using wordpress prefix
$table_name = $wpdb->prefix . 'my_custom_table';
// next line will insert the data
$wpdb->insert($table_name, $data, '%s');
// if you want to retrieve the ID value for the just inserted row use
$rowid = $wpdb->insert_id;
// after we insert we have to redirect user
// I sugest you to cretae another page and title it "Thank You"
// if you do so:
$redirect_page = get_page_by_title('Thank You') ? : get_queried_object();
// previous line if page titled 'Thank You' is not found set the current page
// as the redirection page. Next line get the url of redirect page:
$redirect_url = get_permalink( $redirect_page );
// now redirect
wp_safe_redirect( $redirect_url );
// and stop php
exit();
}
}
});
Note: I have not got to DB exercise, point of my question is submit_button.
As indicated in the error, the code on line 1 points to non identified function:
submit_button('Send Data');
I understood from other discussions submit_button should be core function of WP, so I should be able to call it "directly", without the need of a definition.
I tried following:
originally had very similar code within the plugin, moved to functions.php
reinstalled core of WordPress version 6.1.1
tried several different Themes (as it looked this worked for other users, I tried "classic" and "twentytwentythree" )
And still no little step further, still having same issue with error described above. What I am doing wrong?
If someone would confirm this is WP core installation issue, I am ready to reinstall WP from scratch, just trying to save some time, if there might be other cause.
Thank you for any suggestions.

BCC all outgoing emails wordpress

So I want to BCC all outgoing emails from my wordpress site to two static, hard coded email addresses.
I went to the pluggable.php file and hard coded the BCC headers to the wp_mail() function like this:
function wp_mail( $to, $subject, $message, $headers = ['Bcc: example#mail.com', "bcc: maik#gmail.com"], $attachments = array() ) {
But nothing seems to be happening.
What am I missing?
Please do not edit core WordPress files!
Instead, use the relevant hook like wp_mail in your case.
Here's an example and this code would be added into the theme functions file, or you can add it as a Must Use plugin:
add_filter( 'wp_mail', 'my_wp_mail_args' );
function my_wp_mail_args( $args ) {
// Just replace the email addresses with the correct ones. And note that you
// don't have to add multiple Bcc: entries - just use one Bcc: with one or
// more email addresses separated by a comma - Bcc: <email>, <email>, ...
if ( is_array( $args['headers'] ) ) {
$args['headers'][] = 'Bcc: example#mail.com, maik#gmail.com';
} else {
$args['headers'] .= "Bcc: example#mail.com, maik#gmail.com\r\n";
}
return $args;
}
PS: If you added that as a Must Use plugin, don't forget to add the <?php at the top.
And BTW, just to explain the "nothing seems to be happening", it's because the $headers (fourth parameter) value there can be overridden when the function is called, e.g. wp_mail( 'foo#example.com', 'test', 'test', [ 'From: no-reply#example2.com' ] ) — the $headers is set, hence the default value is not used.
So I hope this answer helps, and keep in mind, never edit any core WordPress files! First, because in many WordPress functions (and templates as well) there are hooks that you can use to modify the function/template output, and secondly, your edits will be gone when WordPress is updated. 🙂

strpos() on title in a metabox in wordpress fails to find '-'

I was coding a simple metabox for wordpress and have a little issue when saving data.
The meta box is in my "create article"-page has two textfields. These are saved as post-meta, when the post is saved.
While saving I check if the fields were filled - if they are empty I take the post title and extract the data I need. The idea is to take everything that is before the first "-". If there is no minus sign, the whole title should be saved in my custom field. Now, this fails to find "-" in the title (alltough there is one) and returns the whole title every time:
function get_from_title($title) {
$pos = strpos($title, '-');
if ($pos) {
return trim(substr($title, $pos));
}
else {
$pos = strpos($title, '–'); //added this since two different signs could be used
if ($pos) {
return trim(substr($title, $pos));
}
else {
return $title;
}
}
}
the function that is calling get_from_title is getting the title via get_the_title( $post_id ) and this works without problems.
Is wordpress encoding the title somehow? Why can't strpos find the minus sign? What should I look for instead?
Thanks
I replied to the thread you started on this topic in the WordPress forums. You can find your answer there.
Alternatively, here's what I said. :)
Ah yes. This is a tricky one. So, why can't strpos find a hyphen in
the title when clearly we can see one? Because there isn't one. hehe.
What WordPress is doing here is converting your hyphen ( minus sign )
into an en-dash.
This will give you diddly-squat:
$pos = strpos( $title, '-' );
You want this:
$pos = strpos( $title, '–' );
Let me know how things turned out for you. :)

Hook before deleting a file in wordpress

Few days ago, I was assigned to task in wordpress. This task is to limit media folder capacity for each user. For example, each user can upload only files only for 100MB. I found a really nice post, which explained and used hooks named wp_handle_upload_prefilter and wp_handle_upload for retrieving the file size and storing them before upload.
Right now, I need to know, or hook some function when user presses "Delete permanently" in media folder, so that I can recalculate the capacity of media folder for specific user. Can anyone help me with this?
In case anyone else needs to react to deleted media, this was my solution to get around the issue that there's no delete media action:
function get_media_item_args( $args ) {
if ($args["delete"] && $_GET["action" == "delete-post"])
{
$current_post_id = !empty( $_GET['id'] ) ? (int) $_GET['id'] : 0;
if ($current_post_id > 0)
{
//Do your thing here.
}
}
return $args;
}
add_filter( 'get_media_item_args', 'get_media_item_args', 10, 1 );
Maybe you can use delete_attachment hook for that. However, this one only affect files that is already attached to post.

Wordpress filter on adding meta?

In wordpress, I need to program it such that anytime someone enters or updates a post meta called "start_date", a bit of code is run on what is entered before it is saved.
I need to take what is entered and convert it to a unix timestamp.
Is there a way to do this?
If not, is there a way to add the code on publish or update of the post such that it checks for that meta and updates it if needed?
Assuming you're creating the metaboxes and custom fields with your plugin, you can do the following. Otherwise, it depends on how their saving the data as it could overwrite yours.
Here's something to get you started though depending on what the case is.
add_action('save_post', 'update_the_post_meta', 100, 2);
function update_the_post_meta($post_id, $post) {
if ( defined('DOING_AJAX') && DOING_AJAX ) { return; }
if ( defined('DOING_CRON') && DOING_CRON ) { return; }
if ($post->post_type == 'revision') { return; }
if ( isset($_REQUEST['start_date']) ) :
//do your timestamp code here and save it in $timestamp
add_post_meta($post_id, 'start_date', $timestamp, true) or update_post_meta($post_id, 'start_date', $timestamp);
else :
delete_post_meta($post_id, 'start_date');
endif;
}
Right now the priority of the add_action is set to 100 (the higher the number, the less priority it has). So, if you're trying to override someone else's function, you may need to increase the priority number. Also, this is assuming the name of the input field is "start_date".

Resources