$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>woocommerce offtopic - Variable function names|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

woocommerce offtopic - Variable function names

matteradmin7PV0评论

I am working on a plugin that will add messages to a WooCommerce product at set places on a single item/product page and run into an issue that to be honest I have never needed a requirement for in the past.

The issue is variable function names.

What I want to be able to do is add multiple messages at key places in a product page, while I am able to target the places I want from a custom post type via a select options box I dont know how many messages in the page someone might want to add and with only one function name this is going to cause errors such as already declared if multiple messages are added for one product.

Is there a way to generate or use 'variable_function' names in a PHP function? I have tried a few ways all have not worked and even the mighty Google has failed to help me with a work around or way to allow for this to be done.

Take the below for an example I can call this once.

add_action( 'woocommerce_before_single_product', 'variable_function', 15 );

function variable_function() {
echo 'this is the first message';
}

But what if i wanted to call the below also to add a second message at a different location I will get the error already declared on line ---,

add_action( 'woocommerce_before_add_to_cart_form', 'variable_function', 15 );

function variable_function() {
echo 'This is a second message';
}

without adding a single option for all possible areas this leaves me only able to add one message per single product and to add single options means the plugin will be bloated and not versatile.

So is there way to random generate functions name such as

add_action( 'woocommerce_before_single_product', 'variable_function'.$randomn_info, 15 );

    function variable_function.$randomn_info() {
    echo 'this is the first message';
    }

Or am I missing something very obvious?

I appreciate any help you might be able to give!

Thanks....

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far