I have a section of code that looks as follows:
function macro_ad_short_code_function(){
return wp_get_attachment_image(get_option('macro_ad_image_uploader'));
}
function pods_ad_short_code_function(){
return wp_get_attachment_image(get_option('pods_ad_image_uploader'));
}
function live_foods_ad_short_code_function(){
return wp_get_attachment_image(get_option('live_foods_ad_image_uploader'));
}
function captive_fish_ad_short_code_function(){
return wp_get_attachment_image(get_option('captive_fish_ad_image_uploader'));
}
function captive_inverts_ad_short_code_function(){
return wp_get_attachment_image(get_option('captive_inverts_ad_image_uploader'));
}
function refugium_ad_short_code_function(){
return wp_get_attachment_image(get_option('refugium_ad_image_uploader'));
}
function coral_ad_short_code_function(){
return wp_get_attachment_image(get_option('coral_ad_image_uploader'));
}
function cleanup_ad_short_code_function(){
return wp_get_attachment_image(get_option('cleanup_ad_image_uploader'));
}
function saltwater_ad_short_code_function(){
return wp_get_attachment_image(get_option('saltwater_ad_image_uploader'));
}
function shipping_ad_short_code_function(){
return wp_get_attachment_image(get_option('shipping_ad_image_uploader'));
}
function white_ad_short_code_function(){
return wp_get_attachment_image(get_option('white_ad_image_uploader'));
}
function wholesale_ad_short_code_function(){
return wp_get_attachment_image(get_option('wholesale_ad_image_uploader'));
}
function add_shorty() {
add_shortcode(get_option('macro_ad_short_code'), 'macro_ad_short_code_function');
add_shortcode(get_option('pods_ad_short_code'), 'pods_ad_short_code_function');
add_shortcode(get_option('live_foods_ad_short_code'), 'live_foods_ad_short_code_function');
add_shortcode(get_option('captive_fish_ad_short_code'), 'captive_fish_ad_short_code_function');
add_shortcode(get_option('captive_inverts_ad_short_code'), 'captive_inverts_ad_short_code_function');
add_shortcode(get_option('refugium_ad_short_code'), 'refugium_ad_short_code_function');
add_shortcode(get_option('coral_ad_short_code'), 'coral_ad_short_code_function');
add_shortcode(get_option('cleanup_ad_short_code'), 'cleanup_ad_short_code_function');
add_shortcode(get_option('saltwater_ad_short_code'), 'saltwater_ad_short_code_function');
add_shortcode(get_option('shipping_ad_short_code'), 'shipping_ad_short_code_function');
add_shortcode(get_option('white_ad_short_code'), 'white_ad_short_code_function');
add_shortcode(get_option('wholesale_ad_short_code'), 'wholesale_ad_short_code_function');
}
add_action('plugins_loaded', 'add_shorty');
Is there a cleaner way to write this with a for loop? (I understand how I would do this for the function add_shorty, but I am not sure how I would have a loop that would define the functions macro_ad_short_code_function, pods_ad_short_code_function, etc.
It looks like this(https://stackoverflow.com/a/2112337/5439315) might be a viable way, but that was 2 PHP versions ago
2
Answers
So, you could use a function alias to just rename functions but that’s not going to help with your
get_option
argument. The solution you posted seems to be the best answer if the functions are not part of a class. If you are in a class, use the magic __call() method and check if the function name matches^(.+)_ad_short_code_function$
and then use the match in theget_option
argument as so:return wp_get_attachment_image(get_option("{$match}_ad_image_uploader"));
I think you can use anonymous functions to achieve this. I have not tested it but I think you can apply the concept: