doujue1246 2014-05-15 20:51
浏览 69
已采纳

通过插件从外部网站请求Wordpress流程表单

I would like to create an endpoint similar to

http://localhost/wordpress/xxxx/ 

that whenever is called is processing POST request. So my situation looks like:

Site A Web Form -> User submits data -> Wordpress (http://localhost/wordpress/xxxx/ ) -> I am parsing and processing request

How can I achieve it without calling plugin files directly ? It seems that the simplest way to detect if the 'xxxx' is used as a part of url is to use parse_request:

add_action('parse_request', 'xxxx_parse_request');
function xxxx_parse_request($wp) {

    if (array_key_exists('xxxx', $wp->query_vars)) {
                        $location =  plugin_dir_url(__FILE__).'includes/issuer_json.php';
                // process the request.
         wp_redirect($location);
    }
}

but it works only with requests like wordpress?xxxx=1 but not with wordpress/xxxx. I thought that it would work for me and I created a test form to check it. Note that I specify the action of the form to be http://localhost/wordpress/?xxxx=1 and specified a hidden input to test passing the data.

<form action="http://wordpress/?xxxx=1"> 
<input type="hidden" name="test" value="tes1"/>

<input type="submit" />
</form>

Whenever I submit the form, I am redirected to the page with url like: http://wordpress/?test=tes1 so the form overwrote the default action http://wordpress/?xxxx=1

I am not sure how to deal with the problem. Any help will be appreciated.

  • 写回答

1条回答 默认 最新

  • dongtan1845 2014-05-21 21:55
    关注

    I seems to me that you need to use the WP admin-ajax handler.

    Basically in your plugin you create something like:

    add_action( 'wp_ajax_my_plugin_function', 'my_plugin_function' );
    
    function my_plugin_function() {
        // Handle request then generate response using WP_Ajax_Response
        echo "You reached my plugin function";
        // important!
        exit();
    }
    

    then in your ajax or form action you POST to http://mydomain.com/wp-admin/admin-ajax.php with a POST parameter (or hidden input) called action with value 'my_plugin_function'.

    I don't often submit forms directly but using jQuery AJAX it would be something like:

    $.ajax({
        type: "POST",
        url: "http://mydomain.com/wp-admin/admin-ajax.php",
        data: {'action' : 'my_plugin_function, 'myvar':'another_var'},
        error: function(jqXHR, textStatus, errorThrown){                                        
            console.error("The following error occured: " + textStatus, errorThrown);                                                       
        },
        success: function(data) {                                       
            console.log("Hooray, it worked!");                                                                  
        }
    });
    

    you can find more info in http://codex.wordpress.org/AJAX_in_Plugins

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?