如何使用Java脚本执行快捷码

时间:2017-10-02 作者:Doppy

假设有以下短代码:

[spu-close class="" text="" align=""]
我想在单击按钮时显示此短代码这是我的html:

 <input class="button b" onclick="openFile(link);"  type="button" value="download" />
这是我的js文件:

function openFile(link){
//I want do something like this
    do_shortcode(\'[spu-close class="" text="" align=""]\', false );
   //  

}
我不知道我该怎么做,有人能帮我吗?

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

要执行服务器端->wordpress->php的“ShortCode”,通过JavaScript执行哪个客户端需要使用AJAX!

您可以使用以下内容:

1-在您的排队中。js文件:

jQuery(document).ready(function($) {
    $(\'.buttonClass\').on(\'click\', function() {
        $.ajax({
            url: Param.doShortCode,
            type: \'POST\',
            data: {
                action: \'handle_ajax_shortcode\',
            },
            success: function() {
                //do something on success 
            },
            error: function() {
                //do something on error
            }
        })
    })
});
2-在php文件中:

//localize your script
$Param = array(

  \'doShortCode\'=>admin_url( \'admin-ajax.php?action=handle_ajax_shortcode\' ),

);
wp_localize_script(\'handle_ajax_shortcode\',\'Param\', $Param);

//executes for users that are not logged in.
add_action( \'wp_ajax_nopriv_handle_ajax_shortcode\', \'handle_ajax_shortcode\' );
//executes for users that are logged in.
add_action( \'wp_ajax_handle_ajax_shortcode\', \'handle_ajax_shortcode\' );


function handle_ajax_shortcode(){
  //put whatever you want to be execute when JavaScript event is triggered
  do_shortcode( string $content, bool $ignore_html = false )
  // Don\'t forget to stop execution afterward.
  wp_die();

}
有关更多信息,请查看Link1 &;Link2 &;Link3

结束