Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
757 views
in Plugins by

Developing the on-site-notifications plugin and others I got to know that a lot of users have not correctly specified their websites URL in the qa_opt('site_url'). This causes e.g. Ajax plugins not to work since we have another domain (already www.blabla.com is not the same as blabla.com). Check topic on "same domain policies".

The workaround is to use qa_path_html('') instead.

However, it would be nice to have it just working. Because many existing and new developers are writing their plugins depending on site_url.

One idea would be to warn within the admin panel that "site_url" does not match the current website URL and to have a button to update the "site_url". As easy as that.

What do you think?

Q2A version: 1.6.3

2 Answers

+3 votes
by
selected by
 
Best answer

I think in most situations calling qa_path_html is the solution since you are already on the site and only need a relative URL.

qa_opt('site_url') is only intended to be used in situations where specifying the domain is necessary, e.g. external code, emails, RSS feeds, canonical tag.

Seems strange to me that some people have not set the URL correctly. Have they just left it blank? I'm not sure we can add a warning otherwise because the site may work without www and with www (or with any subdomain) so giving the warning would be wrong in those cases.

 

By the way, one workaround if you definitely need the current domain, is to call qa_default_option('site_url') which generates the appropriate URL.

by
my case (an odd one I admit): My hosting provider give us the option to use a shared ssl certificate (using cpanel & moddir). In order to use this shared SSL, I have to go to https://OtherURL/~myusername/ , while for regular traffic I go to http://myUrl/

Here are the sites: http://respuestas.ca/ & https://gator3090.hostgator.com/~alvarofg/
by
Thanks for the insight Scott. If calling qa_default_option('site_url'), is this overriding the qa_opt('site_url') value? If so, we could call it when installing the plugin to make sure it has been set correctly.
by
Unfortunately that is advise that that the CORE isn't following - in qa-base.php replacing > qa_opt('site_url'),< with > qa_default_option('site_url') < solve the issue for me completely.
Maybe that's a quick fix for everyone.



function qa_path_absolute($request, $params = null, $anchor = null)
{
    //return qa_path($request, $params, qa_opt('site_url'), null, $anchor);
    return qa_path($request, $params, qa_default_option('site_url'), null, $anchor);
   
}
0 votes
by
edited by

I think q2a core should be added with one more function like this - this small tweak will be more informative . 

function get_base_url()
{
/* First we need to get the protocol the website is using */
$protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"], 0, 5)) == 'https' ? 'https://' : 'http://';
 
$root = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $_SERVER['DOCUMENT_ROOT']);
if(substr($root, -1) == '/')$root = substr($root, 0, -1);
$base = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, rtrim(QA_BASE_DIR, '/'));
 
 
/* Returns localhost OR mysite.com */
$host = $_SERVER['HTTP_HOST'];
 
$url = $protocol . $host . '/' . str_replace($root, '', $base );
 
return (substr($url, -1) == '/') ? substr($url, 0, -1) : $url;
}

 

 

 

or else any of the methods from this http://stackoverflow.com/questions/2820723/how-to-get-base-url-with-php 

by
qa_path_html only returns the path, not the domain name.
by
Oh my bad . Did that mistake in hurry . Thanks for pointing it out .
I have updated my answer . Will it be better if we could have a function like this in core ??
...