How to properly setup an activation hook

You first example looks corrent. There is however a typo, hence class ‘MyPluginAdmin’ not found. You include myplugin-admin.php, but then your question seems to suggest the page holding the class is in fact my-plugin-admin.php (with a hypen). If I correct the typo and run the code there are no warnings. Edit (this works correctly): my-plugin.php: … Read more

Third level navigation class

You don’t need to write your custom Walker for that… Let’s take a look on built-in Walker_Nav_Menu. You’ll find this function: public function start_lvl( &$output, $depth = 0, $args = array() ) { if ( isset( $args->item_spacing ) && ‘discard’ === $args->item_spacing ) { $t=””; $n = ”; } else { $t = “\t”; $n … Read more

Delay an action until current action is completed

Note that the third input argument for the save_post callback, tells us whether it’s an update or not. That could be helpful here, I think. Taking your example as is, one suggestion could be: add_action( ‘save_post’, function( $post_ID, $post, $update ) { postUpdate( $post_ID ); if ( $update ) { deleteCacheOnCloudfront( $post_ID, $post, $update ); … Read more

Execute Hook on the footer or header after activating a plugin

Checking the action hooks API reference, “activated_plugin” is an Advanced hook. It operates outside the normal loop which would permit you to do what you want. A better way would be to add_action(‘admin_footer_text’), as illustrated in this WPBeginner article. However, if you only want this to show when a particular plugin is activated, you can … Read more

When using the_author hook, how can I determine the PHP file that generates each call to `the_author()`?

You can determine the calling files using debug_backtrace in PHP, which will give you the backtrace of the functions called and the file called from. WP core provides wp_debug_backtrace_summary, which makes doing things like this easier. Using the condition stated in your question of being called from my_file.php you could do something like this: add_filter( … Read more

Randomize attachment IDs

For attachment posts, the following should do it, i.e. use the wp_insert_attachment_data hook along with get_post(): function my_random_post_id( $data, $postarr ) { // Runs if the post is being created and not updated. if ( empty( $postarr[‘ID’] ) ) { // Locate a yet-unused ID in the (wp_)posts table. do { // Based on the … Read more

check to see if hook is available

No, for this to work reliably you would need to register hooks, which can’t be done. The only way to know if a template triggers a hook is to load the template and find out. There is a solution to your problem, but testing if a hook is available is not that solution.