Wordpress - action when post is deleted (more then one) - wordpress

I'm having problem with triggering function on "delete_post" action.
I'm building functionality where i had to add one more table in database (i cannot use wp_postmeta for this one).
Because of this I created a custom table which have that additional data related with posts.
I would like that, when admin delete post from admin panel, to delete data for that post in my custom table (i don't want redundant data to stay there when post is gone)
And i implemented something like this:
add_action('admin_init', 'codex_init');
function codex_init() {
if (current_user_can('delete_posts')) add_action('delete_post', 'delete_something');
}
function delete_something($postid) {
//here im deleting everything from that table with that post id for that post type
}
And this works perfect if someone click to delete only one post. But when I want to delete more posts at once (checking buttons on wordpress admin menu and selecting delete option - bulk deletion), this will not work?
Am i doing something wrong here, or is there different action when someone want to delete several posts at once?
EDIT:
As indicated in the comment bellow, this action works perfectly for bulk and individual delete actions as well. My issue was related with the usage of the global $post variable - to get the id of individual posts - when instead I should use parameter which is provided to the function directly.

I looked at the code in wp-admin/edit.php it does trigger the function wp_delete_post which has your action.
Try this. Does it die and hit the var_dump?
add_action('admin_init', 'codex_init');
function codex_init() {
if (current_user_can('delete_posts')) add_action('delete_post', 'delete_something');
}
function delete_something($postid) {
var_dump('trigger'.$postid);die();
//here im deleting everything from that table with that post id for that post type
}

Related

How to POST to admin-post.php from Avada formbuilder form

Using the Avada theme's form builder functionality, I’m attempting to use the “Send to URL” option to POST the form and then run an api call in a plugin I’ve written.
I was thinking I could set the “Send to URL” value to /wp-admin/admin-post.php and add a hidden field to the form named “action” with a value of “make_api_call” to get it to run the code in my plugin set up like so:
add_action('admin_post_make_api_call', 'make_api_call');
function make_api_call()
{
//todo: make the api call
wp_redirect(‘another-page’);
}
However, this does not work and returns this from the server: {"status":"error","info":"url_failed"}
What I want to do is to POST the form to admin-post.php, have my plugin run code when it POSTs, and then redirect to another page.
I've checked the documentation for the AVADA theme and it only says that you can specify a url to post to but doesn't give any additional details.
So I eventually got something to work, and I'm not knowledgeable or experienced enough with WordPress development to know if it is the "right" way, but it works well.
I realized that using the "Send to Url" option on the Avada form, it was POSTing the form to the admin-ajax.php file. There's plenty of documentation on that and I was able to partially make that work but I was not able to make it fit my use case b/c even though there is a way to configure the Avada form to redirect to a different URL on success I couldn't append parameters to that URL based on the return value from admin-ajax.php.
For future reference, here's what I was able to make work but not fit my use case by having the Avada form submission set to Send to Url. (I'm recreating this and some of it's from memory since I went with a different solution, so it may not be 100% runnable.)
The way admin-ajax works is all requests to admin-ajax.php are eventually handled by a WordPress action (filter?) like so:
add_action( 'wp_ajax_my_action', 'my_function' );
In the above, my_action is what you've set as the form's action by creating a hidden input element on your html form named action and setting it's value to "my_action". The my_function argument is the name of the function you want to run when the action happens.
add_action( 'wp_ajax_my_action', 'my_function' );
function my_function(){
//do stuff
}
Watching the request in Chrome's dev tools, I could see the action the form was setting was fusion_form_submit_form_to_url.
So ended up with this:
add_action( 'wp_ajax_fusion_form_submit_form_to_url', 'my_function' );
function my_function(){
//do stuff
}
You can see that the url you enter in the Form Submission URL field gets passed to admin-ajax as fusionAction. Whether the Avada theme does something additional with that - I don't know but you could use it to control the logic that gets executed in my_function. I suspect there's an action in the Avada form that works similar to the wp_ajax_ WordPress action but by the time I got this far I realized this wasn't going to work so I pivoted to the actual solution, below.
All of that worked okay but you can't redirect out of a call to admin-ajax.php unless you do it on the client side and I didn't want to dive into that.
What I was able to make work was configuring the Avada form to do a traditional HTTP POST. I added a hidden input element on the Avada form with a name of formName and the value set to the name of the form I wanted to handle.
In my plugin code, I hooked into the WordPress init action as in the code sample below, and then customized the logic to be executed based on which formName was sent in.
add_action('init', 'callback_function');
function callback_function()
{
if (isset($_POST['formName'])) {
$form = $_POST['formName']; //from the hidden input element "formName"
//there is a call to wp_redirect in each case
switch ($form) {
case 'form1':
process_form_1();
break;
case 'form2':
process_form_1();
break;
default:
# code...
break;
}
//without this "exit" you will get errors similar to "headers already sent"
exit;
}
}
This allowed me to run the code I needed to run based on what form was submitted, and redirect to the correct place afterward.

SilverStripe field-level Page editing permissions

I need to implement field-level permissions in a Page model, in a SilverStripe 3.2 website.
Let's imagine I have an ArticlePage.php model. It has the usual fields like $MenuTitle and $Content, and I've added other properties like $Subtitle and $Author.
I can protect the whole model by using providePermissions() and the associated canEdit() methods, but I need to protect individual fields / page properties.
What I need to do is:
Admins should be able to edit all fields
Users in another permissions group should only be able to edit and save $Subtitle
Is this possible in SilverStripe 3.2? Is there a SilverStripe way of doing it?
If not, is there a way I can Identify the user group of the current user and then perhaps conditionally show the $field->addFieldToTab() code? Is it possible to stop the user saving a field by posting the data maliciously, perhaps by adding the missing fields via inspector?
Thanks in advance.
So here's my own answer. This post was helpful: https://www.silverstripe.org/community/forums/customising-the-cms/show/11693
You can conditionally show CMS fields and tabs using code like the post demonstrates:
public function getCMSFields()
if(!Permission::check('PERMISSION_LABEL'){
$fields->removeFieldFromTab("Root.Main","MenuTitle");
$fields->removeByName('BannerImages');
// etc...
}
// etc...
}
Having defined the permission:
public function providePermissions()
{
return array(
'PERMISSION_LABEL' => 'Can edit some fields',
);
}
My concern with this approach was that a user could still create a form field on the page using inspector or JS and submit values for fields they should not be able to see.
Having tested this it appears that field values are not saved if they are not listed on the page, but are sent with the POST data. Although I'd love to know if a SilverStripe expert could confirm that.

BuddyPress: Modify Name field template in Register form

The registration form in the Buddypress auto generates html for the dynamic fields (such as name). How can I modify the label text (for example replace "(required)" with an * (asterisk).
If you're looking to make that type of change to BuddyPress profile fields, you can use these filters:
add_filter( 'bp_get_the_profile_field_name', function ($field_name){
if($field_name === 'Name') {
return 'Your Name';
}
return $field_name;
});
add_filter( 'bp_get_the_profile_field_required_label', function($string, $id) {
return '*';
});
If you're looking to make changes to those account fields on the left, it looks like you'll need to use a template file copied from BuddyPress into your theme. A bit more intrusive, but will work.
Copy this: buddypress/bp-templates/bp-legacy/buddypress/members/register.php
To here: /your-theme/buddypress/members/register.php
Hope this helps a bit! Customizing BuddyPress & WordPress markup can get stringy real fast, so I'd recommend tracing the code back to a filter or action if you can find one. If not, look for a template file you can bring into your own theme.
If I did not undestand you wrong, It depends if you want to do it in the client (with Javascript for example) or from the server (with the back-end languaje). But the common languajes have a replace() method that takes two arguments, usually strings with the text you want to replace and the one you want to replace it with.
This page explains the replace method:
https://www.w3schools.com/jsref/jsref_replace.asp
Hope it help

Custom Order Action in WooCommerce

I am trying to add Custom Order Action in WooCommerce Orders Page.
I want to add two new options in Bulk Order Actions Dropdown in WooCommerce
Mark Refunded
Mark On- Hold
Any help in this regard is highly appreciated.
There are two parts to solve with this objective.
The first part is to get a custom Order Action in the metabox of the individual Orders Page. I was trying to accomplish the same thing, but didn't find anything definitive on it, so I created a tutorial here:
http://neversettle.it/add-custom-order-action-woocommerce/
The second part is to add a custom order action in the Bulk Actions drop-down of the main Orders page. Skyverge has an excellent tutorial on that here:
http://www.skyverge.com/blog/add-custom-bulk-action/
The only specific thing you will need to take note of is to use the correct post_type. For WooCommerce orders you will need to use this in place of the first example on that tutorial:
add_action('admin_footer-edit.php', 'custom_bulk_admin_footer');
function custom_bulk_admin_footer() {
global $post_type;
if($post_type == 'shop_order') {
?>
<script type="text/javascript">
jQuery(document).ready(function() {
jQuery('<option>').val('export').text('<?php _e('Export')?>').appendTo("select[name='action']");
jQuery('<option>').val('export').text('<?php _e('Export')?>').appendTo("select[name='action2']");
});
</script>
<?php
}
}
Notice the shop_order replaces the post for the condition checking which post_type to add the bulk actions to.
But fundamentally, #brasofilo is correct - for the most part WooCommerce uses standard WordPress structures, post_type mechanisms, and taxonomies. The process is the same for adding a bulk action to the Orders page as it is to the Posts page.
However, you are correct about the custom Order Actions on the individual Orders page - that is WooCommerce only and you'll need to reference the first tutorial on how to solve that part.

WordPress - Custom Post Type Entry Won't Load Into A Page

I created a very basic custom post type in my WP site to hold events. Basically, a user can input an event title, some information about the event and the date.
Currently, the only time the events are being displayed are in my sidebar where I just pull the title and date of the upcoming events.
However, I want to be able to allow the user to also click on the title of the link to lead to the event's individual page. I tried previewing my custom post type event and it kept leading me to my 404.php page. The events don't seem to load into a page at all.
I'm hoping this is a small property that I didn't set when I registered my post type. Any help?
Wordpress has incorporated custom post types into the template hierarchy and you have access to them at single-$posttype.php and archive-$posttype.php.
If you create these templates and your posts don't show up in the loop, you need to reset your permalinks/flush your rewrite rules.
As well, it would help to see how you are registering your post types, as in order for your archive-$posttype.php to be available you need to call has_archive => true in your arguments.
What i've done when i needed it:
Add this code in the same function that create your post type:
add_action("template_redirect", array(&$this, 'template_redirect'));
function template_redirect() {
global $wp;
if ($wp->query_vars["post_type"] == "events")
{
include(TEMPLATEPATH . "/events.php");
die();
}
}
Then, just create events.php with the_loop (like single.php)!
Hope it helps
[]'s

Resources