Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get Plugin Path from within Plugin still problematic! Needs core function! #122

Closed
q2apro opened this issue Oct 21, 2014 · 3 comments
Closed

Comments

@q2apro
Copy link

q2apro commented Oct 21, 2014

This is a well known topic but has not yet been resolved. @svivian I cannot find the original discussion in the qa-forum, but this problem is so important that I need to issue it here.

Situation: Often programmers need to point to some files like CSS or JS that is located in their plugin folder.

Problem: How to get the correct path to the plugin, under all possible circumstances.

The approach until now:

  1. Create a layer.
  2. And code like that (example from one of the q2apro plugins):

class qa_html_theme_layer extends qa_html_theme_base {

// needed to get the plugin url
function qa_html_theme_layer($template, $content, $rooturl, $request)
{
    if(qa_opt('q2apro_bestusers_enabled')) {
        global $qa_layers;
        $this->plugin_url_bestuserspro = $qa_layers['q2apro Best Users Layer']['urltoroot'];
    }
    qa_html_theme_base::qa_html_theme_base($template, $content, $rooturl, $request);
}
// ...

Now I can use $this->plugin_url_bestuserspro everywhere to get the path.

WRONG!

If the user has not enabled friendly URLs, but uses queries / URLs such as domain.com/index.php?qa=208&qa_1=24 the output looks like this:

<link rel="stylesheet" type="text/css" href="./index.php?qa=qa-plugin&qa_1=q2apro-best-users-pro&qa_2=styles.css">

And leads to a 404 not found!

So my urgent request: Please provide a core function that you throw in the plugin name and that returns the correct path, which should work for all URL structures.


My not-foolproof workaround for now:

            // option_neat_urls is set to userfriendly URLs that path is correctly set
            $neaturls = qa_opt('neat_urls');
            if($neaturls==1) {
                $this->output('<link rel="stylesheet" type="text/css" href="'.qa_path($this->plugin_url_bestuserspro).'styles.css">');
            }
            else {
                // important: if user changes the plugin folder name it will not work!
                $this->output('<link rel="stylesheet" type="text/css" href="'.qa_opt('site_url').'qa-plugin/q2apro-best-users-pro/styles.css">');
            }
        }
// insert CSS file
function head_script(){
    qa_html_theme_base::head_script();
    if(qa_opt('q2apro_bestusers_enabled')) {
        if($this->request == 'bestusers' || $this->request == 'bestusers-year') {
            // option_neat_urls is set to userfriendly URLs that path is correctly set
            $neaturls = qa_opt('neat_urls');
            if($neaturls==1) {
                $this->output('<link rel="stylesheet" type="text/css" href="'.qa_path($this->plugin_url_bestuserspro).'styles.css">');
            }
            else {
                // important: if user changes the plugin folder name it will not work!!!
                $this->output('<link rel="stylesheet" type="text/css" href="'.qa_opt('site_url').'qa-plugin/q2apro-best-users-pro/styles.css">');
            }
        }
    }
}
@svivian
Copy link
Collaborator

svivian commented Oct 27, 2014

Just noting here that after a bunch of searching I found the discussion we had was on issue #42 here on Github. Will check into this soon.

@svivian
Copy link
Collaborator

svivian commented Oct 27, 2014

OK unless I'm misunderstanding your issue, you should be able to just use the QA_HTML_THEME_LAYER_URLTOROOT constant, as described in the docs: http://www.question2answer.org/layers.php

So you can get rid of the class contructor (qa_html_theme_layer function) and instead of using $this->plugin_url_bestuserspro put QA_HTML_THEME_LAYER_URLTOROOT instead.

@q2apro
Copy link
Author

q2apro commented Oct 28, 2014

It works!

Such good news. This should be communicated to all developers who still use the "old" method. This way, no plugin will break anymore.

Thanks a lot for your time spent to solve the problem!

PS: And QA_HTML_THEME_LAYER_DIRECTORY to get the server path for the plugin!

@q2apro q2apro closed this as completed Oct 28, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants