It looks like you are adding the hook just inside the body of the class. Try adding it into an init() method and calling it after it is instantiated or at the very least in the constructor.
I think the issues is that the hook is being registered before the class has been fully read by PHP?
Try this:
final class My_Class_Name {
function init() {
add_action('after_switch_theme', array( $this, 'activate' ));
}
function activate() {
}
}
$class = new My_Class_Name();
$class->init();
Hope this helps!
Related Posts:
- Using register_activation_hook in classes
- WordPress and event-driven programming – what is it about?
- Receiving Stripe Webhooks on a wordpress website
- Change the footer text on the login page
- How to remove a metabox from menu editor page?
- WooCommerce: How to display item meta data in email confirmation using woocommerce_order_item_meta_end
- Audio Player not loading when the content is loaded through Ajax, MediaElement.js no applied
- How to Remove a Filter from the Admin List Table?
- Hooking new functions to actions + passing parameters
- Woocommerce, recognize the loop of related products
- Replace existing content from specific WooCommerce admin orders list column
- How To Change The Html of Products filtration sidebar in Woocommerce?
- Insert all post IDs in new database table
- action hook wp_head higher priority as all other plugins/hooks
- using add_action for a header hook that has an additional parameter
- How to call function at the bottom of post using plugin?
- How to add a second ‘place order’ button in WooCommerce [closed]
- How to do multiple searches (with logical OR) in WP_Query in hook pre_get_posts?
- Randomise upload filenames (or another solution to hide the original image URL from theft?)
- Setting up 2 SMTP accounts: 1 for wordpress and 1 for woocommerce
- wp_loaded hook block script enquequing
- Remove an action hook within a Class
- How to pass arguments to add_action() or retrieve return value of called function?
- hook that when page is starting to load
- How to change home or site url using action hooks or filter?
- Edit incorrect password message WordPress
- Contact Fom 7 – how to add custom HTML inside span.wpcf7-form-control-wrap AND IMMEDIATELY AFTER input.wpcf7-form-control?
- Woocommerce: hook action/filter I could use to add variation id and price with each attribute opt on WooCommerce Rest api
- Remove metabox from WordPress menu editor page?
- Add a product to checkout via form
- Why ajax doesn’t work on certain wordpress hooks and reload the page instead?
- Why ajax doesn’t work on certain wordpress hooks?
- How to add an arrow to menu items has submenus
- Error in custom php function doesn’t exist
- Generating an nonce for Content Security Policy and all scripts – How to make it match/persist for each page load?
- Don’t insert if permalink is the same?
- Return a numerical function value in Customizer controls
- edit_comment_link is not showing for comment author
- Adding function to Genesis genesis_header [closed]
- how to make custom ajax handler?
- How to remove woocommerce_breadcrumb() from do_action( ‘woocommerce_before_main_content’ ); [closed]
- Should `wp_login` be used since it’s deprecated?
- Omit custom post type from wp-sitemap.xml based on meta key using wp_sitemaps_posts_query_args
- Password minimum length in personal subscription [closed]
- How to use a custom hook
- Run a code only on theme activation only during first activation
- Theme not calling Jquery properly
- Get access to variable from previous pageview, excluding ajax-calls
- WordPress redirect redirecting too many times or not at all
- How to have different site identity logos on each page on Astra Theme [closed]
- How can I hook into the wp_mail function used by BackWPup?
- Can not add admin notices from the edit_user_profile_update hook (notices not being displayed)?
- Get user custom field value on function.php
- Comapare get_user_meta value
- Must filter functions receive all arguments passed to them?
- Dynamically Generate Functions and Hooks
- save_post trigerred twice
- Generate a QR code when creating a new WordPress user
- Replace a single variable with add_filter
- Filter wordpress posts without searching the keywords in the post content
- Variation prices breakdown only for single product page
- Removing “There is no account with that username or email address.” error message in “/wp-login.php?action=lostpassword”
- Display custom field value in woocommerce variable product via jQurey
- Prevent header and footer from loading on specific pages with template or plugin
- Add custom field on admin dashboard comments / reviews
- Contain multiple page templates in one PHP custom template file in WordPress?
- Wp_Schedule_Event every few minutes doesn’t work
- Preserving backslashes in post_content
- Trouble Accessing Gravity Forms API (GFAPI)
- How to edit post meta data before publishing the post it self wordpress?
- Enqueue assets from multiple directories using add_action/do_action
- Adding new divs with custom classes to products on WooCommerce shop page [closed]
- Call a single function on two different methods with hooks
- Discount in the specific product title using keyword ( Woocoommerce )
- get Woocommerce product format json for WP_Query
- How do I get the value of a current user’s custom field?
- Is there an option to execute javascript file only on plugin activation
- How do I remove an action hook inside a class that is called by another class?
- Need to forward Data from WooCommerce Webhook sent to same site WordPress REST API custom endpoint
- Hook into all password resets in WordPress and get password before hashing?
- Add class to all meta boxes for a custom post type
- Fatal error when using array_diff() function inside of wp_update_nav_menu hook?
- Why transition_post_type hook is called twice for the same post?
- Clone the “proceed to cart” button and place it above checkout on the cart page, but only appear display size is 320px
- Passing link parameter values to form
- WordPress HTML Helper
- How to add custom checkout field in user details mail template
- Cannot modify header information – headers already sent
- For each loop will not append to the_content hook
- Send a custom notification to customer on WooCommerce cancelled order status
- Assign new post author IF another user in custom field on post transition
- Create and publish a new post every Monday, listing the post titles and links to the posts published during the previous week
- understanding wp_next_scheduled
- Add/alter image attribute before render
- transition_post_status hook, works – but not if the post is new
- Guests comment form – prevent duplicate email addresses
- Woocommerce – add tracking code to another email template
- Is there a hook that I can use when a fatal error occurs?
- Is there a way to pull remotely generated XML, process it, and display it on a Managed WordPress page?
- how to append ACF field data using one of the following filter/ hooks