Hook from plugin doesn't fire up from external PHP script
I have this habit of installing a plugin with all my custom hooks, instead of writing a functions.php
file with the same PHP code in it. That way I get the same functionality without the need of installing a child theme...
So my plugin looks like this:
?php
/**
* Plugin Name: Custom Code
* Description: Contains custom code needed by the website
* Version: 1.1.5
* Author: Me
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: all-custom-code
**/
if (!defined('WPINC')) {
die;
}
add_action('pmxi_gallery_image', 'set_media_category_to_images', 10, 4);
function set_media_category_to_images($post_id, $att_id, $filepath, $is_keep_existing_images = '')
{
// Code here...
}
add_action('pmxi_after_xml_import', 'count_product_images', 10, 2);
function count_product_images($import_id, $import)
{
// Code here...
}
add_filter('wp_unique_term_slug', 'prevent_subcat_sufix', 10, 3);
function prevent_subcat_sufix($slug, $term, $original_slug)
{
// Code here...
}
add_filter('woocommerce_ajax_variation_threshold', 'set_variations_loading_threshold', 10, 2);
function set_variations_loading_threshold($amount, $product)
{
// Code here...
}
In this particular website I have an action hooked to after_delete_post
which looks like this:
add_action('after_delete_post', 'clean_pmxi_tables', 20, 2);
function clean_pmxi_tables($postid, $post)
{
global $wpdb;
$wpdb-query(DELETE FROM {$wpdb-prefix}pmxi_hash WHERE post_id NOT IN (SELECT ID FROM {$wpdb-prefix}posts WHERE post_type = 'product' OR post_type = 'product_variation'););
$wpdb-query(DELETE FROM {$wpdb-prefix}pmxi_posts WHERE post_id NOT IN (SELECT ID FROM {$wpdb-prefix}posts WHERE post_type = 'product' OR post_type = 'product_variation'););
$wpdb-query(DELETE FROM {$wpdb-prefix}pmxi_images WHERE attachment_id NOT IN (SELECT ID FROM {$wpdb-prefix}posts WHERE post_type = 'attachment' AND post_parent IN (SELECT ID FROM {$wpdb-prefix}posts WHERE post_type = 'product' OR post_type = 'product_variation')););
}
I also have an external script in a separate file, which however loads the WP core (with require_once '../wp-load.php';
), which among other operations, deletes some attachments using this code:
if (isset($_GET['id'])) {
if ($image = get_post(intval($_GET['id']))) {
wp_delete_post($image-ID, true);
}
}
So, one would expect the hook defined in my plugin to fire up. But I noticed it doesn't, as I compared the rows in the affected tables and they are the same.
For control, I even put a file_put_contents(ABSPATH . '/TTTT.txt', time() . \r\n, FILE_APPEND);
in my after_delete_post
action, and still no file was written. So I concluded that it doesn't fire up at all... I played with priorities a little bit, changing 10
to 20
, 99
, 5
, but nothing changed...
Now my suspicion fell on hooks not firing up through actions of external scripts in general, but I can't find any resource claiming this, nor can I find any resource to fix that behavior (supposing my suspicion is correct).
Anyone can shed some light on this? TIA. :D