Wordpress : add_action not working in plugin or functions.php - wordpress

I've got two functions:
add_action( 'woocommerce_order_status_completed' , 'after_order_complete' , 10);
function after_order_complete( $order_id )
{
error_log("woocommerce_order_status_completed", 0);
}
add_action( 'wp_login', 'after_login', 10, 2);
function after_login( $user_login, $user ) {
error_log("wp_login", 0);
}
that I can't seem to get called, tried in a plugin and functions.php but they never fire and no errors are logged.
How is it I debug this to find out why they fail or does anyone know how to get these to work?
In the same code I have another add_action but this time using user_register and that works as expected from plugin or functions.php
Edit:
I have the wp_login figured out, action does get called but only when first visiting the back end, site is front end only for users so used membership system hook instead.

Related

How to detect current page in WordPress plugin development

I'm currently developing a plugin where I will restrict certain pages to the logged-in users only. I mean the normal pages we create in Wordpress.
I found this online but it does not seem to work for me.
function check_the_current_page(){
echo $slug = basename(get_permalink());
}
add_action( "init", "check_the_current_page" );
Another option I had was to detect current page id as shown in the code below. nothing gets printed on the screen, probably I'm using the wrong hook or something, someone help me
function check_the_current_page(){
global $post;
echo "Post id: ".$post->ID;
}
add_action( "init", "check_the_current_page" );
if only I'm able to get output in one of the above scenarios, then it would be very simple, but now nothing is being printed on the screen for both scenarios
Try This Code Work For Me global $post dosent work in init
function check_the_current_page() {
global $post;
echo "Post id: " . $post->ID;
}
add_action( "wp", "check_the_current_page" );

Fire function when Stripe has error in WooCommerce

I am trying to fire a custom function when a credit card error occurs in the WooCommerce checkout flow.
I seem to be able to get standard woo errors by using but it seems that the stripe plugin does not us woocommerce_add_error
add_filter( 'woocommerce_add_error', 'my_woocommerce_add_error' );
I found the following hook in the WooCommerce Stripe documentation, but I can't seem to get it to work with add_action
wc_gateway_stripe_process_payment_error ($error, $order) – Called when
an error occurs during the process payment event.
add_action( 'wc_gateway_stripe_process_payment_error', 'test_this' );
What am I missing? Is there another filter or hook that I should be using?
As per documentation https://docs.woocommerce.com/document/stripe/ this hook take 2 params $error, $order so you have to tell it to WordPress:
add_action( 'wc_gateway_stripe_process_payment_error', 'test_this', 10, 2 );
function test_this($error, $order) {
// something
}
It's working fine on my side
The only way I could figure out how to do this is by watching for dod inserts on the dom. This is the code I used below.
Note: You should probably use mutated events these days but for some reason, I couldn't figure it out.
$(document).on('DOMNodeInserted', function(e) {
if ( $(e.target).hasClass('woocommerce-error') ) {
console.log('stripe input error');
}
});

How to use conditional add_action for Wordpress

I am having a small issue where i need to call add_action method which is conditional. Basically here, first checking whether the plugin active or not by admin_init action. If true then i need another add_action call from that action.
Please find the scenario what i am looking for:
function check_some_other_plugin() {
if ( is_plugin_active('some-plugin.php') ) {
//if true then vc_before_init action will run
add_action( 'vc_before_init', 'vc_master_home_slider' );
}
}
add_action( 'admin_init', 'check_some_other_plugin' );
function vc_master_home_slider() {
//my stuff
}
I am really eager to know how this things can be possible. Thanks in advance.

remove_action fails on woocommerce_template_loop_product_thumbnail

I wrote a simple plugin that performs some logic on thumbnails in WooCommerce. This plugin worked great for about a year, until the client switched to a new theme. Now the plugin is no longer working and I've narrowed the problem to remove_action() failing.
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10);
if (! remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 10) ) {
echo 'FAILED to remove action<br/>';
}
/**
* WooCommerce Loop Product Thumbs
**/
if ( ! function_exists( 'woocommerce_template_loop_product_thumbnail' ) ) {
function woocommerce_template_loop_product_thumbnail() {
echo woocommerce_get_product_thumbnail();
}
}
I don't know if this is because of the order plugins are loaded, or if I'm making the call incorrectly due to a change in WooCommerce. I've read some topics that indicate the remove_action() should follow my custom add_action() so I reversed the order accordingly. It doesn't work either way - remove_action() is always returning FALSE.
I've been banging my head against a wall all day trying to figure this out. Can anyone give me a clue on a sure-fire way to execute the remove, or a way to debug it?
Thanks.
Try changing the priority flag of [add/remove]_action() to something higher, like 90.
It's a little hard to guess without seeing what the theme is actually doing!
add_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 90);
if (! remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_template_loop_product_thumbnail', 90) ) {
echo 'FAILED to remove action<br/>';
}

Restricting Wordpress Admin Options to Admins

I thought this would be an easy thing but many hours have gone by and still no results.
I am creating a Wordpress plug-in that should only appear in the dashboard if the user is an admin. I wrapped my hooks in the is_admin() method, but when I log in as a user who is just a subscriber, I still see the menu.
Isn't it just that easy???
Here's a code except starting right below the comment section to register the plugin... everything not shown is just functions doing their job ...
if( is_admin ){
add_action('admin_menu', 'ppm_talentexpo_add_page');
add_action('admin_menu', 'ppm_expos_submenu');
} // end is_admin
function ppm_talentexpo_add_page() {
$mypage = add_menu_page('Talent Expo', 'Talent Expos', 2, 'ppmtalentexpo', 'jwg_talentexpo_options_main_page', '/wp-admin/images/media-button-music.gif' , 21);
add_action( "admin_print_scripts-$mypage", 'jwg_ppmtalentexpo_admin_head' );
} // end function
It looks like you left out the parentheses when calling is_admin in the conditional.
Try
if( is_admin() ){
add_action('admin_menu', 'ppm_talentexpo_add_page');
add_action('admin_menu', 'ppm_expos_submenu');
}
Also if you're not using an older WordPress install, add_menu_page allows you to specify a capability that WordPress will check for. This lets WordPress manage showing the item or not.
So you can define a custom capabilty (or reuse an existing one), and the menu should take care of itself.
add_action('admin_menu', 'ppm_talentexpo_add_page');
add_action('admin_menu', 'ppm_expos_submenu');
function ppm_talentexpo_add_page() {
$mypage = add_menu_page('Talent Expo', 'Talent Expos', 'my_custom_talent_expos_capability', 'ppmtalentexpo', 'jwg_talentexpo_options_main_page', '/wp-admin/images/media-button-music.gif' , 21);
add_action( "admin_print_scripts-$mypage", 'jwg_ppmtalentexpo_admin_head' );
}

Resources