Use PHP Class in WordPress functions

If you have defined the class MyCart directly in functions.php with your hooks. I’d expect for the following to work: // add action $cart = new MyCart(); function emdr_add_to_cart(){ $items = $cartClass->get_users_cart_contents(); // process items } add_action( ‘woocommerce_add_to_cart’, ’emdr_add_to_cart’); I might also add that duplicating the “state” of the users cart in 2 places is … Read more

Combine one action and one filter

the filter function must be inside the action function, because if it isnt it is not recognize. So I think. At least with variables is like this. add_action( ‘woocommerce_before_checkout_form’, ‘add_checkout_error’, 9 ); function add_checkout_error() { wc_print_notice( __( ‘An error message.’, ‘woocommerce’ ), ‘error’ ); add_filter(‘woocommerce_order_button_html’, ‘remove_order_button_html’ ); function remove_order_button_html( $button ) { $button = ”; … Read more

Assign user role by text field in WordPress (Woocommerce) [closed]

Here is the code: function ur_update_role( $valid_form_data, $form_id, $user_id ) { global $table_prefix; $assign_roles_list = array(); if( isset( $valid_form_data[‘billing_ie’]) && !empty( $valid_form_data[‘billing_ie’]->value ) ) { array_push( $assign_roles_list, ‘ie’ ); } if ( ! empty( $assign_roles_list ) ) { // Re-ordering roles according to priority. $user_roles_list = ur_get_default_admin_roles(); foreach ( $user_roles_list as $key => $value ) … Read more

Hook to change the site URL

You can filter site_url using a filter – called site_url The hook has the following signature: apply_filters( ‘site_url’, string $url, string $path, string|null $scheme, int|null $blog_id ) You can use it like this: add_filter( ‘site_url’, ‘wpse_381006_custom_site_url’, 10, 1 ); function wpse_381006_custom_site_url( $url ){ if( is_admin() ) // you probably don’t want this in admin side … Read more