通过$ _POST变量值将参数传递给Wordpress do_action函数

时间:2014-11-15 20:19:31

标签: ajax wordpress post plugins action

我的插件中有一个动作处理程序,它捕获了一个发布到admin-ajax.php的表单。动作命名空间是' handle_custom_form_submission'。

我可以通过do_action(' namespace',' argument')函数轻松地将参数传递给我的动作处理程序。但是,如果我通过AJAX捕获一个动作,据我所知,我所能做的就是有一个$ _POST var,其名称=" action"和value =" namespace"。有没有办法以这种方式将参数传递给我的动作调用?喜欢

<input name="action" value="handle_custom_form_submission( 'a' )"/>

以下是更详细的情况:

我正在尝试编写自定义表单插件,以便我可以在Wordpress网站上轻松处理多个独特的表单。

在我的最新项目中,我在一个页面上有一个联系表格,在另一个页面上有一个推荐提交表格。在我的代码中,我只是将它们指定为&#39; custom-form-a&#39;和&#39; custom-form-b​​&#39;。

我有3个主要操作挂钩:

  1. include_custom_form_js($ form_id)

  2. include_custom_form_html($ form_id)

  3. handle_custom_form_submission()

  4. 我希望能够传递#3,handle_custom_form_submission(),$ form_id。我现在唯一能想到的就是声明两个单独的函数,handle_custom_form_a_submission()和handle_custom_form_b_submission()。有更干净的方式吗?

    我的主要插件文件:

    <?php
    /*
    Plugin Name: Custom Form
    Plugin URI: 
    Description: Add custom form HTML and handle its submission on the backend.
    Version: 0.1
    Author: James Heston
    */
    
    require_once( dirname( __FILE__ ) . '/php/constants.php' );
    
    class CustomForm{
    
      // paths
      private $plugin_dir = CUSTOMFORM_DIRNAME;
      private $plugin_path = CUSTOMFORM_URLPATH;
    
      // action names
      private $action_include_js = 'include_custom_form_js';
      private $action_include_html = 'include_custom_form_html';
      private $action_handle_submission = 'handle_custom_form_submission';
    
      // other vars
      private $recipient_email = ''; // probably should pull from some WP option
    
    
    
      public function __construct(){
        $this->add_hooks();
      }
    
      private function add_hooks(){
        // include necessary js with appropriate MyAjax properties on page where `do_action( 'include_custom_form_js', $form_id )` action is called
        add_action( $this->action_include_js, array( $this, $this->action_include_js ), 10, 1 );
        // include form html on page where `do_action( 'include_custom_form_html', $form_id )` action is called
        add_action( $this->action_include_html, array( $this, $this->action_include_html ), 10, 1 );
        // handle contact form submission
        add_action( 'wp_ajax_' . $this->action_handle_submission, array( $this, $this->action_handle_submission ) );
        add_action( 'wp_ajax_nopriv_' . $this->action_handle_submission, array( $this, $this->action_handle_submission ) ); 
      }
    
      function include_custom_form_js( $form_id ){    
    
        wp_enqueue_script(
          $this->action_include_js,
          $this->plugin_path . '/js/custom-form-' . $form_id . '.js',
          array(),
          false,
          true
        );
        $localize_array = array(
          'ajaxurl' => admin_url( 'admin-ajax.php' ),
          'namespace' => $this->action_handle_submission
        );
        wp_localize_script( $this->action_include_js, 'MyAjax', $localize_array );
      }
    
      function include_custom_form_html( $form_id ){    
        include( $this->plugin_dir . '/html/custom-form-' . $form_id . '.html' );
      }  
    
      function handle_custom_form_submission(){
        $form_id = 'a';
        include( $this->plugin_dir . '/php/handle-custom-form-' . $form_id . 'submission.php' );
        include( $this->plugin_dir . '/php/handle-custom-form-' . $form_id . 'submission.php' );
      }
    
      function send_email(){
        // 
      }
    
      static function instance(){
        global $CustomForm;
        if(! isset($CustomForm) ){
          $CustomForm = new CustomForm();
        }
      }
    
    }// class CustomForm
    
    if(! isset($CustomForm) ){
      CustomForm::instance(); 
    }
    ?>
    

0 个答案:

没有答案