How to trash a post if it contains specific explicit words? - wordpress

I am trying to write a wordpress filter that would change the post status to trash if it contains explicit words, but I can't manage to get it to work. Could you please help me?
This is what I got so far:
add_filter('wp_insert_post_data', 'delete_invalid_posts', '99');
function delete_invalid_posts($data) {
$false_titles = array("*****", "******");
if (in_array($data['post_title'], $false_titles) {
// If post data is invalid then
$data['post_status'] = 'trash';
}
return $data;
}

If you want to search the title for Explicit Words, you may use this code:
add_filter('wp_insert_post_data', 'delete_invalid_posts', 99);
function delete_invalid_posts($data) {
$false_titles = array("*****", "******");
$title_arr = explode(' ', $data['post_title']);
$found = array_intersect($false_titles, $title_arr);
if (!empty($found)) {
$data['post_status'] = 'trash';
}
return $data;
}
I've not tested the code, So try it and if you have any question don't hesitate to ask.

I might be wrong, but I think you are missing a closing parentheses here...?
Are you getting an error message?
if (in_array($data['post_title'], $false_titles) // <--- HERE should be a ")"
Like I said, I could be mistaken or there may be other issues...

Related

Yet another WordPress Ajax 400 Bad Request problem

I am certain I am making a basic mistake here. I am getting the lovely 400 Bad Request response from WordPress when I try to make an Ajax call. I have one Ajax call that works, and this one that does not. I have looked at several posts both here and elsewhere, but remain stumped as to where I have erred.
Okay, the relevant code. First, here is how I do the add_action and script registration:
if ($_GET["page"] == "krudkat_data_structures") {
add_action("wp_ajax_krud_save_new_connection", "krud_save_new_connection");
}
// Other Stuff, for the Ajax call that works.
if ($_GET["page"] == "krudkat_data_structures") {
wp_register_script("krud_data_structures", plugin_dir_url( __DIR__ ) . "/js/dataStructures.js", array("jquery"));
wp_localize_script("krud_data_structures", "krudAjax", array( "ajaxurl" => admin_url("admin-ajax.php")));
wp_enqueue_script("krud_data_structures");
}
This sits in a function outside of a class. My JS call is like this:
var krudNewConnect = { action:"krud_save_new_connection",
dbname:$("#krud_new_dbname").val().trim(),
dbhost:$("#krud_new_dbhost").val().trim(),
dbconnect:$("#krud_new_dbconnect").val().trim(),
dbusername:$("#krud_new_dbusername").val().trim(),
dbpassword:$("#krud_new_dbpassword").val() };
$.post(krudAjax.ajaxurl, krudNewConnect, function(newConnectData) {
console.log(newConnectData);
});
Finally, my PHP method is, thus far, very simple:
function krud_save_new_connection() {
$dbname = $_POST["dbname"];
$dbhost = $_POST["dbhost"];
$dbconnect = $_POST["dbconnect"];
$dbusername = $_POST["dbusername"];
$dbpassword = $_POST["dbpassword"];
$dbport = 0;
$dbsocket = '';
if (!empty($dbconnect)) {
if (is_numeric($dbconnect)) {
$dbport = $dbconnect * 1;
} else {
$dbsocket = $dbconnect;
}
}
echo "Port: " . $dbport . "\nSocket: " . $dbsocket;
wp_die();
}
This is not expected to work when somebody is not logged in as an admin, so I omitted the no_priv add_action. I am certain this is not the problem; I did add that into my code and it had no impact.
What newbie mistake have I made? :)
First, enqueue the script and then localize the script for ajax. It will fix the issue. Check the codex: https://codex.wordpress.org/AJAX_in_Plugins

Trigger WooCommerce Email on Custom Order Actions

I have been experimenting all day and researching the whole web, and I cant seem to get this action to work. Basically I'm trying to trigger a Woo Email when a custom order action is selected. In this case, its a gift receipt.
Please Note: When I turn on debugging, I get a headers already sent notice, none when its off.
Here is the code which I had tried:
function gift_receipt_add_order_meta_box_action($actions)
{
global $theorder;
$actions['send_gift_receipt'] = __('Send Gift Receipt', 'enyc');
return $actions;
}
add_action('woocommerce_order_actions', 'gift_receipt_add_order_meta_box_action');
function gift_receipt_wc_process_order_meta_box_action()
{
$mailer = WC()->mailer();
$mails = $mailer->get_emails();
if (!empty($mails))
{
foreach ($mails as $mail)
{
if ($mail->id == 'wc_gift_order_email')
{
$mail->trigger($order->id);
}
}
}
}
add_action('woocommerce_order_action_send_gift_receipt', 'gift_receipt_wc_process_order_meta_box_action');
Thanks.
function gift_receipt_wc_process_order_meta_box_action()
is missing $order
function gift_receipt_wc_process_order_meta_box_action($order)
might this be the issue?
So I figured it out after some more coffee. The problem was 2 fold:
1) I did not pass the order ($order) info to the function gift_receipt_wc_process_order_meta_box_action()
2) the id (name) of the email was actually 'wc_gift_order' instead of 'wc_gift_order_email'
Thanks for the help!

wordpress author post count limit

i am trying to limit the post for authors , currently i am allowing 2 post no more then 2 post , currently i have few users with more then 2 post so how do i fix my code so it dont allow them to make any more post.
if ($post_count < 2) {
return TRUE;
} else {
echo 'You have already posted your maximum number of posts.';
return FALSE;
}
}
as i said few authors have more then 2 post so using this code will allow them to make post because the if stating wil be bypassed because its trying to detect value 2 since they gone past that.
You should use wp_insert_post_data filter. And in this filter define your conditions.
So it will be something like that:
add_filter('wp_insert_post_data', 'aa_func_20150916080916', 20, 2);
function aa_func_20150916080916($data , $postarr)
{
$warn = "You have already posted your maximum number of posts";
$current_user = get_current_user_id();
$user_post_count = count_user_posts($current_user);
if($user_post_count >2 ) {
return wp_die($warn);
}
return $data;
}
Please note: it's quick answer, you should change code for your purposes.
Docs: https://codex.wordpress.org/Plugin_API/Filter_Reference/wp_insert_post_data

Populating a custom field from the Wordpress Postie Plugin

I am using the Postie plugin to auto post from email on my Wp blog.
I am trying to populate two custom fields (mail_meta_from and mail_meta_replyto") with the "from" and "reply to" fields
add_filter('postie_post_before', 'add_custom_field');
//Get the "from" and "replyto" email details
add_filter('postie_filter_email2', 'get_emaildetails', 10, 3);
function get_emaildetails( $from, $toEmail, $replytoEmail) {
DebugEcho("step-01b");
DebugDump("from " . $from);
DebugDump("toEmail " . $toEmail);
DebugDump("replytoEmail " . $replytoEmail);
$fromField = $from;
$replytoEmail = $replytoEmail;
return $from;
return $replytoEmail;
function add_custom_field($post) {
add_post_meta($post['ID'], 'mail_meta_from', '$from');
add_post_meta($post['ID'], 'mail_meta_replyto', $replytoEmail);
return $post;
}
}
This has been driving me nuts for the past 2 days, and I have tried multiple variations of the above but with no success.
At the moment, I am getting the error
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'add_custom_field' not found or invalid function name in /home/sites/mysite.com/public_html/wp-includes/plugin.php on line 213
I try and learn from my mistakes, but I am not getting anywhere with this...
The default answer for help with this on the WP forum is to check out http://postieplugin.com/extending/.
Which I have... repeatedly.
Any help would be greatly appreciated!
You are defining the add_custom_field() function inside the scope of the get_emaildetails() function because it is within the curly braces. You should also consider using a more unique name for your function, or encapsulate it an an object namespace. The error you are getting indicates that when the apply_filter() is called for postie_post_before it can't find a function called add_custom_field(). Use the code below to properly scope the function, though note that you have some other syntax errors as well.
add_filter('postie_post_before', 'add_custom_field');
function add_custom_field($post) {
// the variable $from is not defined, and it will not evaluate if enclosed
// in single quotes. if you want the value of $from define it then use "{$from}"
add_post_meta($post['ID'], 'mail_meta_from', '$from');
// $replytoEmail is not defined
add_post_meta($post['ID'], 'mail_meta_replyto', $replytoEmail);
return $post;
}
//Get the "from" and "replyto" email details
add_filter('postie_filter_email2', 'get_emaildetails', 10, 3);
function get_emaildetails( $from, $toEmail, $replytoEmail) {
DebugEcho("step-01b");
DebugDump("from " . $from);
DebugDump("toEmail " . $toEmail);
DebugDump("replytoEmail " . $replytoEmail);
// what is this for?
$fromField = $from;
// setting to itself, then not used?
$replytoEmail = $replytoEmail;
return $from;
// this will never return?
return $replytoEmail;
}

recursive function using joomla db object

I want to write a recursive function in joomla that get all the child level categories using a category id using joomla's jmodel's db object.Following is my code that I have written:
function getChildCategories($type){
$query = "SELECT id FROM #__cd_categories WHERE parent_id='$type'";
echo $query."<br/>";
$this->_db->setQuery($query);
$list = $this->_db->loadObjectList();
if ($this->_db->getErrorNum()) { echo $this->_db->stderr(); return false; }
foreach($list as $record){
$this->childCategories[]= $record->id;
echo $record->id."<br/>";
return $this->getChildCategories($record->id);
}
return true;
}
So now problem is that, in joomla we use $this->_db_setQuery method and $this->_db->loadObjectList method , so in recursive call the result set, I think it overwrite, I think because the object is same. So can any one tell the way that how to overcome this problem? If you can solve this by using loop even that would be also very helpful for me.
I also think that once values are assigned to $list variable then that over write shouldn't be problem.So seems strange.Please tell if some one can tell me the way to do it?
thanks in advance
I don't think the issue is with the database object getting overwritten. It has been a bit since I have been struggling with recursive functions but I think the issue is with assigning the $list variable.
Should you not be returning that variable instead of boolean true like this:
function getChildCategories($type) {
$query = "SELECT id FROM #__cd_categories WHERE parent_id='$type'";
$this->_db->setQuery($query);
$list = $this->_db->loadObjectList();
if ($this->_db->getErrorNum()) { echo $this->_db->stderr(); return false; }
if ($list) {
foreach($list as $record){
$list->childCategories = $this->getChildCategories($record->id);
}
return $list;
} else {
return;
}
}

Resources