WordPress麻烦集成了Ajax功能

时间:2019-03-08 17:10:04

标签: php ajax wordpress

我正在创建带有插件的WP Fiche 页面。 此页面的内容需要另一个文件important_file.php中包含的许多变量和函数。这就是为什么我将所有内容都放在content_fiche()函数中并使用require_once('important_file.php')的原因。

问题是我想使用JQuery Ajax管理页面内容的形式并调用文件important_file.php中包含的函数。

我真的不想修改文件important_file.php,因为我没有编写代码。

<?php
class fiche_content{
    private $file_base;

    function __construct(){
        $this->file_base = plugin_dir_path( dirname( __FILE__ ) ) . 'fiche.php'; 
        $this->init();
    }   

    function init() {
        add_action('wp_enqueue_scripts', array($this,'scripts_js'));
        add_action('wp_loaded', array($this, 'add_page_fiche'));
    }  

    function scripts_js(){
        wp_enqueue_script('fiche_scripts', plugins_url('JS/scripts.js', __FILE__), array('jquery'), '1.0', true);
        wp_localize_script('fiche_scripts', 'ajax_object', array('ajaxurl' => admin_url( 'fiche.php' ),
        ));
    }

    function add_page_fiche() {
        $new_page = array(
            'post_title'    => wp_strip_all_tags( 'Fiche Projet' ),
            'post_content'  => $this->content_fiche(),
            'post_status'   => 'publish',
            'post_author'   => 1,
            'post_type'     => 'page'
        );
        wp_insert_post( $new_page );
    }    

    function content_fiche(){
        require_once ( '.../important_file.php');
        $foo = $var_in_important_file;      
        $html = '<div>'. $foo .'</div>';
        $html .= '<form id="form" method="POST">
                      <input type="hidden" name="build" value="1" />
                      <input type="submit" name="submit_build" value="Submit" />
                </form>';
        return $html;

这是我的JS文件,应管理以下表单:

jQuery(document).ready(function($) {
    $(document).on('submit', '#form',function(e){
        $.post({
            url: my_ajax_object.ajax_url,
            data: {
                data,
                'action': 'function_in_important_file'
            },
            done: function(result) {
            }
        });
    });
});

通常要将我的JS提交连接到PHP函数,但是仅仅这样做还不够:

add_action( 'wp_ajax_function_in_important_file', 'function_in_important_file' ); 

很显然,ajax文件无法访问important_file.php及其功能。这是我第一次使用OOP插件,所以我不知道该如何进行。

1 个答案:

答案 0 :(得分:0)

这是关于WordPress Ajax函数的手册

    class fiche_content{
        private $file_base;

        function __construct(){
            $this->file_base = plugin_dir_path( dirname( __FILE__ ) ) . 'fiche.php'; 
            $this->init();
        }   

        function init() {
            add_action('wp_enqueue_scripts', array($this,'scripts_js'));
            //add_action('wp_loaded', array($this, 'add_page_fiche'));

            // Register the Ajax action to bind the corresponding WordPress function

            add_action( "wp_ajax_nopriv_function_in_important_file", array ( $this, 'add_page_fiche' ) );
            add_action( "wp_ajax_function_in_important_file",        array ( $this, 'add_page_fiche' ) );



        }  

        function scripts_js(){
            wp_enqueue_script('fiche_scripts', plugins_url('JS/scripts.js', __FILE__), array('jquery'), '1.0', true);
            wp_localize_script('fiche_scripts', 'ajax_object', array('ajaxurl' => admin_url( 'fiche.php' ),
            ));
        }

        function add_page_fiche() {
            $new_page = array(
                'post_title'    => wp_strip_all_tags( 'Fiche Projet' ),
                'post_content'  => $this->content_fiche(),
                'post_status'   => 'publish',
                'post_author'   => 1,
                'post_type'     => 'page'
            );
            wp_insert_post( $new_page );
        }    

        function content_fiche(){
            require_once ( '.../important_file.php');
            $foo = $var_in_important_file;      
            $html = '<div>'. $foo .'</div>';
            $html .= '<form id="form" method="POST">
                          <input type="hidden" name="build" value="1" />
                          <input type="submit" name="submit_build" value="Submit" />
                    </form>';
            return $html;
        }
    }