admin管理员组

文章数量:1278853

WordPress 5.8.1

functions.php

add_action( 'deleted_theme', 'nonverbis_delete_extra_table', 10, 2 );
function nonverbis_delete_extra_table( $stylesheet, $deleted ){
    error_log("delete_extra_table", 0);
}

What I do:

The log is empty. I mean that everything that this function does is writing "delete_extra_table" to the log. I don't have a debugger, so, I use writing to log for the debug purpose.

But the log is empty. This means that either a hook is wrong. Or some params (10, 2). Or I confused something else (like log or something).

To the best of my understanding, this line should have appeared in the log after I had pressed "Delete".

Could you help me understand why nothing has been written to the log?

And of course, the table has not been dropped.

WordPress 5.8.1

functions.php

add_action( 'deleted_theme', 'nonverbis_delete_extra_table', 10, 2 );
function nonverbis_delete_extra_table( $stylesheet, $deleted ){
    error_log("delete_extra_table", 0);
}

What I do:

The log is empty. I mean that everything that this function does is writing "delete_extra_table" to the log. I don't have a debugger, so, I use writing to log for the debug purpose.

But the log is empty. This means that either a hook is wrong. Or some params (10, 2). Or I confused something else (like log or something).

To the best of my understanding, this line should have appeared in the log after I had pressed "Delete".

Could you help me understand why nothing has been written to the log?

And of course, the table has not been dropped.

Share Improve this question edited Nov 2, 2021 at 19:16 Kifsif asked Nov 2, 2021 at 19:03 KifsifKifsif 1012 bronze badges 3
  • 1 If that code is in the theme then it's not going to run because the code was deleted when the theme was deleted. – Jacob Peattie Commented Nov 3, 2021 at 0:41
  • You are right. But what hook should I use? I want to drop the database table I created for this theme. – Kifsif Commented Nov 3, 2021 at 7:06
  • 1 You could only do it when switching theme with switch_theme. Themes don’t normally create database tables. That sort of thing usually belongs in plugins, which have the appropriate capabilities for cleaning up when deleted. – Jacob Peattie Commented Nov 3, 2021 at 7:32
Add a comment  | 

1 Answer 1

Reset to default 0

There are two direct actions that you can hook into while deleting a theme

do_action( 'delete_theme', $stylesheet );

And

do_action( 'deleted_theme', $stylesheet, $deleted );

Now about the error_log() function, you seem to be missing a argument, the destination.
One of my debugging tool is a custom error_log function, this is its code.

error_log(print_r($some_value, true), 3, __DIR__ . '/log.txt');
error_log("\r\n\r\n", 3,  __DIR__ . '/log.txt');

This will create a log.txt file in the same folder where you called this functions. If you use this in functions.php the log.txt will be in the root of your theme.

本文标签: Catch the moment when theme is deleted