can I pass arguments to my function through add_action?

后端 未结 13 2191
慢半拍i
慢半拍i 2020-11-29 01:07

can I do something like that? to pass arguments to my function? I already studied add_action doc but did not figure out how to do it. What the exact syntax to pass two argum

13条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 01:25

    I have made a code to send parameters and process.

    function recibe_data_post() {
    
    $post_data = $_POST;
    
    if (isset($post_data)) {
    
        if (isset($post_data['lista_negra'])) {
    
            $args = array (
                'btn'  =>  'lista_negra',
                'estado'=>  $post_data['lista_negra'],
            );
    
            add_action('template_redirect',
                       function() use ( $args ) {
                           recibe_parametros_btn( $args ); });
        }
        if (isset($post_data['seleccionado'])) {
            $args = array (
                'btn'  =>  'seleccionado',
                'estado'=>  $post_data['seleccionado'],
            );
    
            add_action('template_redirect',
                       function() use ( $args ) {
                           recibe_parametros_btn( $args ); });
    
            }
        }
    }
    
        add_action( 'init', 'recibe_data_post' );
    
    function recibe_parametros_btn( $args ) {
    
    $data_enc = json_encode($args);
    $data_dec = json_decode($data_enc);
    
    $btn = $data_dec->btn;
    $estado = $data_dec->estado;
    
    fdav_procesa_botones($btn, $estado);
    
    }
    
    function fdav_procesa_botones($btn, int $estado) {
    
    $post_id = get_the_ID();
    $data = get_post($post_id);
    
    if ( $estado == 1 ) {
        update_field($btn, 0, $post_id);
        } elseif ( $estado == 0 ) {
           update_field($btn, 1, $post_id);
        }
    
    }
    

提交回复
热议问题