最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

how to get wordpress home url in javascript? - Stack Overflow

programmeradmin1浏览0评论

in php get_home_url() I using 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>

I want to use 'http://localhost/wordpress' to javascript function to get wordpress home url.

in php get_home_url() I using 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>

I want to use 'http://localhost/wordpress' to javascript function to get wordpress home url.

Share Improve this question asked May 15, 2018 at 1:32 jomjom 561 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

You can get Wordpress's Home Url from the WP-JSON link tag as follows:

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

If you are looking for a way to find host of the current page, then use document.location.

console.log("document.location.href : "+ document.location.href);
console.log("document.location.origin : "+ document.location.origin);
console.log("document.location.hostname : "+ document.location.hostname);

 // --> If you want to pass another url to fetch domain home name then you can use following function.


var tmp = document.createElement('a');
tmp.href   = "http://www.example./my-page";
console.log("host : "+tmp.host);
console.log("Host : "+tmp.hostname);

发布评论

评论列表(0)

  1. 暂无评论