Hi I would like to ask you guys if this is possible in Woocommerce hooks
the hook I'm using is this one,
woocommerce_order_status_processing
I want this hook only to be called in user side after payment, which is works ok but in the admin if I change the oder status, this hook is also triggering, can I disabled my custom hook in admin and will run/trigger only for the user side?
add_action( 'woocommerce_order_status_processing', 'order_extracode' );
function order_extracod( $order_id) {
.....
}
the above code is the function and hook I added, I tried !is_admin() but it is not working, and still processing this function in Admin Orders
thanks (TIA)
This hook will be executed every time there's a statu change.
What you can do is to decide if you want to execute the code in the front or in the Dashboard.
add_action( 'woocommerce_order_status_processing', 'order_extracode' );
function order_extracod( $order_id) {
if( ! is_admin()){
// Your code here
}
}
The only conditional tag to detect if you are in the dashboard or not is the is_admin()
WooCommere cannot tell the difference between who is triggering the woocommerce_order_status_processing action.
If you want something to happen when the user completes payment, you could try the woocommerce_payment_complete hook in abstract-wc-order.php.
Alternatively you can use current_user_can() function to determine the whether the hook can be executed or not, like this
if( !current_user_can( 'administrator' ) && !current_user_can( 'manage_options' ) ) {
//do your stuff
}
Related
I need to write a function in WordPress.
I would like to function to work on this principle:
- Checking function if the user is logged,
- Checking whether I am currently on the "my-account"
- And what i need (Previous page is the "checkout" subpage)
function add_login_check()
{
if ( is_user_logged_in() && is_page( 'my-account' ) ) {
wp_redirect('http://google.com');
exit;
}
}
add_action('wp', 'add_login_check');
I need to add a function wp_get_referer?
You are trying to hook to late, the headers are already sent.
You can use other action like init or wp_loaded
add_action('init', 'add_login_check');
The Action Reference page shows the order of the WordPress actions.
Hope it helps !
iam implementing an event system on wordpress which allows an restricted user the following:
Create a new organizer (custom-post-type)
Create an event (custom-post-type)
Creating an event the user should choose an organizer for a special event by a list. Therefore i handle the organizer cpt as a taxonomy inside the event cpt.
Now my question is:
How can i only show the organizers, which this specific user has created? I face the problem, that all existing events are shown on every event for every user.
If you need any code or screenshots let me know, thank you very much in advance!
Solved it with Advanced Custom Fields plugin (Relationship Field). It is possible to relate cpts into another cpt. I learned to NOT use a cpt as a taxonomy.
Approach for filtering only user created posts
You can achieve it by adding the following code to your functions.php file
function posts_for_current_author($query) {
global $pagenow;
if( 'edit.php' != $pagenow || !$query->is_admin )
return $query;
if( !current_user_can( 'edit_others_posts' ) ) {
global $user_ID;
$query->set('author', $user_ID );
}
return $query;
}
add_filter('pre_get_posts', 'posts_for_current_author');
I'm looking for a way to disappear woocommerce billing address in checkout page when the user is logged in. Is that possible? And in first place, Why is that?! Why billing address form is always showing in checkout page even when the user is logged in?!!!
1) For your question one try this:
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
function custom_override_checkout_fields( $fields ) {
if( is_user_logged_in() ){
unset($fields['billing']);
$fields['billing'] = array();
}
return $fields;
}
2) For your question two :
If user wants to modify something in billing form, this is the place where he can modify nothing else. If you think this is not correct, better to develop a plugin, maybe a lot of people will be thinking this.
I wanna do the following:
If a user is logged out, he can view the page id=10, if a user is logged in and view page id=10 he will be redirected to page id=5. I tried adding the below code into my header, but it didn't work.
add_action( 'init', 'check_redirect_page' );
function check_redirect_page() {
if ( is_user_logged_in() && is_page( 10 ) ) {
wp_redirect( get_permalink( 5 ) );
exit;
}
}
Try using the wp hook rather than init; WordPress won't have got far enough at init to know whether it's dealing with a particular page or not.
add_action( 'wp', 'check_redirect_page' );
From the Conditional Tags documentation:
Warning: You can only use conditional query tags after the
posts_selection action hook in WordPress (the wp action hook is the
first one through which you can use these conditionals). For themes,
this means the conditional tag will never work properly if you are
using it in the body of functions.php, i.e. outside of a function.
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' );
}