douniang3866 2017-06-10 04:01
浏览 47
已采纳

通过Ajax获取短代码值 - wordpress

I'm new in wordpress and I don't know two much about this.

Situation There is plugin that generate shortcode [unread_messages].
plugin path is wp-content/plugin/chat/ I create a php file under wp-content/custom/messages.php

messages.php

 <?php

  echo do_shortcode('[unread_messages]');

 ?>

And I call request through AJAX like providing url wp-content/custom/messages.php But this php file gives an error call undefined function do_shortcode

How can I access the value of shortcode through which is not a part of plugin.

  • 写回答

2条回答 默认 最新

  • douyao2529 2017-06-10 06:09
    关注

    You Can't get call AJAX call like this first you need to register AJAX call and then send request to admin-ajax.php

    Check out this.

    add_action('wp_ajax_nopriv_unread_messages', 'unread_messages');
    add_action('wp_ajax_unread_messages', 'unread_messages');
    function unread_messages(){
            $output['response'] = do_shortcode('[unread_messages]');
            wp_send_json( $output );
    }   
    

    After then call AJAX like this.

    jQuery.ajax({
            url : 'yourwebsite/wp-admin/admin-ajax.php',
            type : 'post',
            data : {
                action : 'unread_messages'
            },
            success : function( response ) {
    
            }
        });
    

    Learn more about How to Create AJAX in Plugins

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?