WordPress并通过编码将操作添加到按钮

时间:2020-04-26 作者:Omnia Magd

我在函数中创建动作。php

add_action(\'wp_ajax_myfilter\', \'misha_filter_function\'); // wp_ajax_{ACTION HERE} 
 function misha_filter_function()
 {
   echo "my button action"; 
 }
如何创建按钮来调用此方法wp_ajax_myfilter

1 个回复
最合适的回答,由SO网友:Karthick 整理而成

Try below method.

<a id="view_site_description" href="javascript:void(0);">View Our Site Description</a>

add_action( \'wp_footer\', \'add_js_to_wp_footer\' );
function add_js_to_wp_footer(){ ?>
    <script type="text/javascript">
    jQuery(\'#view_site_description\').click(function(){
        jQuery.ajax({
            type: \'POST\',
            url: ajaxurl,
            data: {"action": "myfilter"},
            success: function(data){alert(data);}
        });
        return false;
    });
    </script>
<?php }