如何在javascript中获取wordpress主页url?

jom*_*jom 2 javascript wordpress

在 php get_home_url() 中我使用 ajax。

<script>
$j=jQuery.noConflict();
    $j(document).ready(function(){
        $j('.new_post_btn').click(function(){
            var dataString = {
            'action': 'new_post_action',
            'post_status': 'draft',
        };

            dataString=
                jQuery.ajax({
                    type: "POST",
    url: "http://localhost/wordpress/wp-admin/admin-ajax.php",
                  data: dataString,
                                    success: function(msg){
                         if(msg!='false')
                                                 {
                                                     msg=msg.substring(0, msg.length-1);
                                                     window.location='http://localhost/wordpress/wp-admin/post.php?post='+msg+'&action=elementor';
                                                 }
                       }

                 });
        });
});
</script>
Run Code Online (Sandbox Code Playgroud)

我想使用' http://localhost/wordpress '到javascript函数来获取wordpress主页url。

Mar*_*hes 7

您可以从 WP-JSON 链接标签获取 WordPress 的主页 Url,如下所示:

// get JSON url
var WpJsonUrl = document.querySelector('link[rel="https://api.w.org/"]').href
// then take out the '/wp-json/' part
var homeurl = WpJsonUrl.replace('/wp-json/','');
Run Code Online (Sandbox Code Playgroud)