最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

actions - Two functions with different arguments and add_actions, but identical code

programmeradmin1浏览0评论

I feel like this is simple.

I have two functions that contain identical code, but need to have different add_actions and arguments. There's got to be a way to do this so I'm not copying and pasting between the two when I want to add more code, plus you're not supposed to duplicate code. What am I missing?

One is for ACF and fires when updating a post type and one is for Admin Columns that fires the same actions but when editing the same post type inline. I have these in a plugin file if that helps.

    function acp_editing_saved_usage1( AC\Column $column, $post_id, $value ) {

runs same code

    }
    add_action( 'acp/editing/saved', 'acp_editing_saved_usage', 10, 3 );


    function my_acf_save_post( $post_id ) {

runs same code

    }
    add_action('acf/save_post', 'my_acf_save_post');

I feel like this is simple.

I have two functions that contain identical code, but need to have different add_actions and arguments. There's got to be a way to do this so I'm not copying and pasting between the two when I want to add more code, plus you're not supposed to duplicate code. What am I missing?

One is for ACF and fires when updating a post type and one is for Admin Columns that fires the same actions but when editing the same post type inline. I have these in a plugin file if that helps.

    function acp_editing_saved_usage1( AC\Column $column, $post_id, $value ) {

runs same code

    }
    add_action( 'acp/editing/saved', 'acp_editing_saved_usage', 10, 3 );


    function my_acf_save_post( $post_id ) {

runs same code

    }
    add_action('acf/save_post', 'my_acf_save_post');
Share Improve this question asked Jan 8, 2021 at 19:02 Drew SchlichtmannDrew Schlichtmann 31 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 2

If I am not missing something here, you could have a third/main function, with two different wrappers for it.

function wpse381184_main(){
    //do your thing!
}

function acp_editing_saved_usage1( AC\Column $column, $post_id, $value ) {
    // call wpse381184_main()
    wpse381184_main();
}
add_action( 'acp/editing/saved', 'acp_editing_saved_usage', 10, 3 );
function my_acf_save_post( $post_id ) {
    // call wpse381184_main()
    wpse381184_main();
}
add_action('acf/save_post', 'my_acf_save_post');

If you need wpse381184_main to use different number of arguments depending on the calling wrapper, you could structure it with the case with more arguments, and simply ignore some arguments depending which hook called it, by using the current_filter() function.

function wpse381184_main($post_id, $hook, AC\Column $column, $value){
    if( 'acp/editing/saved' ===  $hook){
        //do your thing in case A and exit!
        return;
    }
    // do your thing in "default" case B!
}

function acp_editing_saved_usage1( AC\Column $column, $post_id, $value ) {
    // call wpse381184_main()
    wpse381184_main($post_id, current_filter());
}
add_action( 'acp/editing/saved', 'acp_editing_saved_usage', 10, 3 );
function my_acf_save_post( $post_id ) {
    wpse381184_main($post_id, current_filter());
}
add_action('acf/save_post', 'my_acf_save_post');
发布评论

评论列表(0)

  1. 暂无评论