WordPress 4.9.4更新后自定义Hook报错求助
Hey Lorenzo, let's break down how to troubleshoot this custom hook issue after updating to WordPress 4.9.4. Since the hook worked perfectly before the update, we'll focus on changes that might have affected WooCommerce, WPML, or core WordPress hook behaviors in that version.
1. Check Hook Priority & WP 4.9.4 Compatibility
WordPress 4.9.x introduced subtle tweaks to hook execution order, especially for post-save actions. Your hook might be running too early, getting overwritten by WooCommerce or WPML's own save handlers.
- Try bumping the priority from the default
10to a higher value like30to ensure your code runs after other core/plugin logic:add_action('save_post_product', 'your_custom_hook_function', 30, 2); - Double-check if you're using hook parameters correctly. For
save_post, the third parameter ($update) tells you if it's a new post or an edit—make sure you're targeting the right scenario (e.g., only new products) withif (!$update) { ... }.
2. Refactor WooCommerce Inventory Logic to Use Official Functions
Directly editing post meta can break after updates if WooCommerce changes its meta key structure or validation rules. Switch to WooCommerce's native functions for reliability:
function your_custom_hook_function($product_id) { // Skip auto-drafts or revisions if (wp_is_post_autosave($product_id) || wp_is_post_revision($product_id)) return; // Enable stock management update_post_meta($product_id, '_manage_stock', 'yes'); // Set stock quantity to 1 (uses WooCommerce's stock handling) wc_update_product_stock($product_id, 1, 'set'); }
3. Fix WPML Default Language Setup with Its Official API
WPML often updates its internal methods, so direct meta manipulation might no longer work. Use WPML's official functions to set the product's language:
function your_custom_hook_function($product_id) { // ... (existing checks) // Only run if WPML is active if (function_exists('wpml_set_element_language_details')) { wpml_set_element_language_details(array( 'element_id' => $product_id, 'element_type' => 'product', 'language_code' => 'en', 'source_language_code' => null // Leave null for original products )); } }
Also, ensure WPML is fully initialized before your hook runs—try hooking into wpml_after_setup instead of save_post_product if you're still seeing issues.
4. Enable Debugging to Catch Hidden Errors
You might be missing PHP notices/warnings that are breaking your hook. Add these lines to your wp-config.php:
define('WP_DEBUG', true); define('WP_DEBUG_LOG', true); define('WP_DEBUG_DISPLAY', false);
Check the wp-content/debug.log file for errors related to your hook, WooCommerce, or WPML. Even a minor undefined variable can stop execution in newer WP versions.
5. Test for Plugin/Theme Conflicts
Updates can expose hidden conflicts with other tools:
- Deactivate all plugins except WooCommerce and WPML, then test your hook. If it works, reactivate plugins one by one to find the culprit.
- Switch to a default theme like Twenty Seventeen to rule out theme-related interference.
6. Use WooCommerce-Specific Hooks for New Products
Instead of save_post_product, try the woocommerce_new_product hook—it runs specifically after a new product is created by WooCommerce, avoiding conflicts with core post-save logic:
add_action('woocommerce_new_product', 'your_custom_hook_function', 20);
Walk through these steps one by one, and you should be able to pinpoint exactly where the issue is. Let me know if you need help interpreting debug logs or adjusting specific parts of your code!
内容的提问来源于stack exchange,提问作者Lorenzo Varano

