Execute function only if action hook fires - wordpress

Can anybody give me a hint? :D
I want the function update_option only to be executed, if the action hook "pre-plupload-upload-ui" fires.
Actually it's loading on every page load.
Goal: Everytime the Upload Panel loads, the option should switch to "redirect" :)
function pz_action_pre_plupload_upload_ui( ) {
$download_method = get_option( 'woocommerce_file_download_method' );
if ( $download_method && $download_method != 'redirect') {
update_option( 'woocommerce_file_download_method', 'redirect' );
}
};
add_action( 'pre-plupload-upload-ui', 'pz_action_pre_plupload_upload_ui', 10, 0 );

You could use the did_action() function to retrieve the number of times an action is fired.
Source # https://developer.wordpress.org/reference/functions/did_action/
<?php
if ( did_action( 'pre-plupload-upload-ui' ) >= 1 ) {
// ...
};
[1]: https://developer.wordpress.org/reference/functions/did_action/

Related

save_post for custom meta box returns 302

I have a php class inside my plugin that generates custom metaboxes for my post types.
Theses metaboxes contain inputs that I want to save with the "save_post" hook, as I have done a million times :
class CcmComponentMetaboxElement{
public function __construct($config){
/* Some logic */
$this->config = $config;
$this->ccm_metabox_display();
$this->ccm_metabox_save();
}
public function ccm_metabox_display(){
$metabox_id = $this->config->id;
$context = property_exists($this->config, 'context') ? $this->config->context : 'advanced';
$priority = property_exists($this->config, 'priority') ? $this->config->priority : 'default';
add_meta_box(
$metabox_id,
$this->config->title,
array($this, 'ccm_metabox_display_callback'),
$this->config->screens,
$context,
$priority
);
}
public function ccm_metabox_display_callback(){
/* Some logic that displays metabox with custom post_metadata inside */
}
public function ccm_metabox_save(){
foreach($this->config->screens as $screen){
$action_name = 'save_post_' . $screen;
add_action($action_name, function($post_id){
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return;
}
if(empty($_POST)){
error_log( 'EMPTY POST' );
return;
}
if ( ! array_key_exists( $this->nonce_name, $_POST ) ||
!wp_verify_nonce( $_POST[$this->nonce_name], $this->nonce_value )) {
error_log( 'NONCE NOT VERIFIED' );
return;
}
foreach($this->config->fields as $field){
if(array_key_exists( $field->id, $_POST )){
update_post_meta( $post_id, $field->id, $_POST[$field->id]);
}
}
}, 10, 1);
}
}
}
The issue i'm facing this time, is that when I'm saving a post with a custom metabox, my hook from my plugin is well executed, but the script stops when checking if $_POST is empty (it records the 'EMPTY POST' inside my debug.log file).
After clicking on "update", fetch executes the save action but returns 302 from the server
The system always returns a 302 status code and $_POST is always empty, no matter what I write in my function, except when I var_dump the $_SERVER variable (for some reason, when I do this it works fine, but the Worpress fetch api doesn't like it).
Does anyone have any suggestions to help me ?
Thanks !
I tried many solutions that didn't work in my case, including refreshing permalinks, forcing https in wp-config, ....
I only activated my plugin and an empty theme of mine with the bare minimum.
I have the same problem with the hook 'restafterinsert_post'

is it possible to execute WordPress hook dynamically?

The only possible is if I try to dynamic WordPress hook tag or callback. But I've failed to dynamic both like a complete hook. The scenario is here 👇 have a look and give me a solution if it is possible.
foreach ( $option_root as $option_root_key => $option_root_value ) { // Loop through admin options.
$wp_hook_tag = $option_root_value['wp_hook_tag']; // Getting hook tag like, wp_head, wp_footer, etc.
$wp_template = $option_root_value['wp_template']; // Getting template like is_single(), is_page(), etc.
$some_page_id_selected = $option_root_value['page_ids']; // Selected Page IDs.
$custom_code = $option_root_value['custom_code']; // Custom code like '<script>alert("Hello World!");</script>'
add_action(
$wp_hook_tag,
function() use (
$acc_libraries,
$wp_template,
$custom_code ) {
if ( 'page' === get_post_type() && is_page( $some_page_id_selected ) ) {
if ( $wp_template() ) {
echo $custom_code;
}
}
}
);
}
Note: When I run this code, get_post_type() is not working.

WordPress - woocommerce_process_shop_order_meta doesn´t work

I am trying to include a function after an order is updated (admin side), but it doesn´t work. I want to call a function when someone update an order (order status, etc...). I am trying this:
add_action( 'woocommerce_process_shop_order_meta', 'woocommerce_process_shop_order', 1, 1);
function woocommerce_process_shop_order () {
// Code
}
Ive been trying to redirect to other web with header("Location: www.example.com"), but the page doesn´t redirect when an order is update :(
Please, can you help me to solve it?
Thanks!
There you go. Tested and works. Goes into functions.php file of your child theme:
add_action( 'post_updated', 'post_updated_action', 20, 3 );
function post_updated_action( int $post_id, WP_Post $post_after, WP_Post $post_before ): void {
// To be sure it gets called when updated from the admin dashboard and is order
if ( $post_before->post_type !== 'shop_order' || ! is_admin() ) {
return;
}
wp_safe_redirect( home_url() );
exit;
}

Custom cron job doesn't scheduled in Wordpress

I read several questions here and from Google search results, but I wasn't able to make it work. I have a function I want to run once every day which will fetch data from external source and store it on options page.
add_action( 'wp', 'update_stock_market' );
add_action( 'get_stock_market_daily_data', 'get_stock_market_data', 1, 1 );
function update_stock_market() {
if ( ! wp_next_scheduled( 'get_stock_market_daily_data' ) ) {
wp_schedule_event( time(), 'daily', 'get_stock_market_daily_data', array($company) );
}
}
function get_stock_market_data($company ) {
//do something here to get $data;
update_option( 'stock_market_data', $data);
}
However, no matter what happens, I cannot find the scheduled cron in the wp-options table under the option-name of cron
This code is working for me on a clean instance of WordPress, instead of looking directly in the database have you tried using the plugin WP Crontrol? Alternatively, with debug enabled, try adding a log entry to you update_stock_market function to check that the sechudled event is being called.
add_action( 'wp', 'update_stock_market' );
add_action( 'get_stock_market_daily_data', 'get_stock_market_data', 1, 1 );
function update_stock_market() {
error_log( 'triggered' );
if ( ! wp_next_scheduled( 'get_stock_market_daily_data' ) ) {
wp_schedule_event( time(), 'daily', 'get_stock_market_daily_data', array($company) );
}
}
function get_stock_market_data($company ) {
//do something here to get $data;
update_option( 'stock_market_data', $data);
}

WP Delete User Button missing

We have a Wordpress installation with a lot of users. If we wanted to delete a user so far, it took a long time until the delete button appeared. Then the deletion always worked. But now the button doesn't appear anymore. I have the feeling it's because it always first goes through the 400,000 users and it's because of that. I'm using WP version 4.9.8
if there are many users it is loading very long. then you can filter the users to be displayed like this:
add_action( 'load-users.php', function () {
// Make sure the "action" is "delete".
if ( 'delete' !== filter_input( INPUT_GET, 'action' ) ) {
return;
}
add_filter( 'wp_dropdown_users_args', function ( $query_args, $args ) {
if ( 'reassign_user' === $args[ 'name' ] ) {
$query_args[ 'role' ] = 'administrator';
}
return $query_args;
}, 10, 2 );
} );

Resources