Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+3 votes
1.4k views
in Q2A Core by
I am trying to implement a google +1 button to replace the voting system in Q2A. I believe I have correctly implemented the buttons but I am unsure of how to fetch the question URL and add it as the href element in the <g:plusone></g:plusone> tag.

I am trying to use qa-theme as follows:

function voting_inner_html($post)
        {
            $this->output('<g:plusone HREF=?></g:plusone>');
    
        }
 

I have tried several different combinations to figure out how to get the full url for each question but haven't had any luck. Any suggestions? Thanks.
by
changing back to my default theme for now so you won't see the +1 implementation. Still appreciate any suggestions.

1 Answer

+2 votes
by

Ha, that's an interesting way to go, if a little sneaky! Not sure what your HREF thing is for, but the code for the +1 button is this:

<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>
<g:plusone size="tall"></g:plusone>

You can move the script tag to anywhere on the page - recommended to be before the closing </body> tag, so maybe in Q2A's footer() function. Something like this should work:

function voting_inner_html($post)
{
    $this->output('<g:plusone size="tall"></g:plusone>');
}

function footer()
{
    qa_html_theme_base::footer();
    $this->output('<script type="text/javascript" src="https://apis.google.com/js/plusone.js"></script>');    
}

 
by
edited by
I have included the script and have it working just fine. The problem is, that unless you specify the href element in the <g:plusone></g:plusone> tag, all of the buttons count as a vote only for the current url.  What I would like to do is use the database information to specify the question url for each button so I can have multiple buttons on a single page and they will all count as votes not for the page they are on but for the page that is specified in the href element. That way when there are multiple questions displaying on one page users can vote for any one of them and the vote gets counted in the right place. Hope that makes sense.  Right now I have 10 buttons on 1 page but all count as a vote for the current url and not the url of the question.  Any other thoughts?
by
I think I need to use $question['url']  inside of the voting_inner_html($post) something like this

function voting_inner_html($post)
{
    $this->output('<g:plusone href="$question['url']"></g:plusone>');
}

I've tried this in several different ways and I'm getting a php undefined variable error.  Any other thoughts?
by
Here is a link to the documentation for the + 1 button. It talks about using the href element if you have multiple buttons on one page to specify which url receives the vote when each of the buttons are clicked:

http://code.google.com/apis/+1button/#script-parameters
by
OK I see - assumed you just had one button on each question page, not on the list of questions. The problem with your code above is that you put the variable inside a string. Try this:

$this->output('<g:plusone href="'.$question['url'].'"></g:plusone>');
by
Just wanted you and anyone else who is interested I figured it out.

www.mathhomeworkanswers.org

I had to incorporate the buttons into the following function:

function q_item_title($question)
        {
           
            $this->output(
                '<DIV CLASS="qa-q-item-title-b">',
                '<g:plusone HREF="http://mathhomeworkanswers.org/'.$question['url'].'"></g:plusone>',
                '</DIV>'
            );
               
        $this->output(
                '<DIV CLASS="qa-q-item-title">',
                '<A HREF="'.$question['url'].'">'.$question['title'].'</A>',
                '</DIV>'
            );
        }

I think this was because the title links are part of a loop on most of those pages. In order to get the url variable to work correctly I had to be inside of the loop.  There may be another way to do this. . .just what I came up with and it seems to work.
...