Calling PHP function from wordpress in ajax?

2019-02-18 22:29发布

问题:

reset.php file:

 <?php 
    add_action('admin_init','popup_template_reset_options');

    function popup_template_reset_options() 
        {
        delete_option('popup_template_on');
        delete_option('popup_template_close');
        delete_option('popup_template_escape');
        delete_option('popup_template_external');
        delete_option('popup_template_home_page');
        delete_option('popup_template_all_pages');
        delete_option('popup_template_template');
        delete_option('popup_cookies_display_after_like');
        add_option('popup_cookies_display_after_like','365');
        //add_option('popup_template_on','1');
        add_option('popup_template_close','1');
        add_option('popup_template_escape','1');
        add_option('popup_template_external','1');
        add_option('popup_template_force_timer','2');
        add_option('popup_template_home_page','1');
        add_option('popup_template_all_pages','1');
        add_option('popup_template_template','2');

    }
    ?>

Script Ajax:

<script type="text/javascript">
    $(document).ready(function() {
        $('#reset_general').click(function()
        {
            $('#result1').css("display", "block");
            jQuery('#result1').animate({'opacity': '1'});

        });
    });
    function resetgeneral() {
        $.ajax({type: 'POST', url: '<?php echo WP_PLUGIN_URL; ?>/fantasticpopuptemplate/inc/reset.php', success: function(response) {

                //$('#fff').find('.form_result').html(response);
                $('#result1').css("display", "none");
                $('#resets1').css("display", "block");
                $('#resets1').html("Settings Resets");
                $('#resets1').fadeOut(2500, "linear");
            }});

        return false;
    }

</script>
<form onsubmit="return resetgeneral();" id="form_general_reset" class="form-1">
    <input type="submit" value="Reset" id="reset_general" name="reset" class="button-secondary"/>
    <input name="action" type="hidden" value="reset" />

</form>

Hi i trying to call the php reset function in ajax but when i googling i know that not possibilities of direct calling php function so I put that particular function in seperate php file and call that php file i am not sure how can i do this in ajax i tried this above code but nothing happen. Settings Resets Message appear. How Can I do this Any Help would be great. Before using the ajax concept i tried with isset function in php But it getting page load every time for that only i jump into the ajax.

回答1:

There is a standard way of achieving AJAX in WordPress plugin by relying on the admin_ajax.php. The file name is misleading, as it can be used in the frontend too, by assigning functions to AJAX actions.

There is a good description on the WordPress Codex:

http://codex.wordpress.org/AJAX_in_Plugins

One thing to be aware of: Your AJAX handler functions will always have to terminate with a die() command, to avoid the extra '0' output from WordPress.