building sharing buttons need php for 'this page'

4 replies
I am building sharing buttons for example
<a href="https://twitter.com/share?url=http%3A%2F%2Fmy-site.com%2Fhome&text=Awesome site:" name="Share on Twitter" target="_blank"><img src="http://my-site.com/wp-content/themes/atahualpa/images/social/Twitter.png"</img></a>

What I want to do is use php in the ?url=

portion to specify the current page.
#building #buttons #page #php #sharing
  • Profile picture of the author Brandon Tanner
    This is how you get the current URL in PHP...

    PHP get current page URL

    So if you implemented the code from the example above, then your button code would look like...

    <a href="https://twitter.com/share?url=<?php echo $currentUrl; ?>&text=Awesome site:" name="Share on Twitter" target="_blank"><img src="http://my-site.com/wp-content/themes/atahualpa/images/social/Twitter.png"</img></a>

    ^ Any page that code goes on would need to have a .php extension, obviously.
    Signature

    {{ DiscussionBoard.errors[7992492].message }}
    • Profile picture of the author rhinocl
      Strange when I did that every link comes out as http://my-site.com/index.php?

      This is a Wordpress site and the share buttons are in a widget
      I am using the exec-php plugin to keep the widget from stripping out the php
      {{ DiscussionBoard.errors[7992689].message }}
      • Profile picture of the author SteveJohnson
        Originally Posted by rhinocl View Post

        Strange when I did that every link comes out as http://my-site.com/index.php?

        This is a Wordpress site and the share buttons are in a widget
        I am using the exec-php plugin to keep the widget from stripping out the php
        It's not strange, once you understand how WordPress works internally.

        You have to tap into the WordPress query object - the base database query that builds the page.

        Code:
        <?php
        global $wp_query;
        $current_url = trailingslashit( get_bloginfo('home') ) . $wp_query->query['name'];
        ?>
        Signature

        The 2nd Amendment, 1789 - The Original Homeland Security.

        Gun control means never having to say, "I missed you."

        {{ DiscussionBoard.errors[7993055].message }}
  • Profile picture of the author galihkartiwa
    if you are using wordpress :
    <a href="https://twitter.com/share?url=<?php the_permalink(); ?>text=<?php the_title(); ?>:" name="Share on Twitter" target="_blank"><img src="http://my-site.com/wp-content/themes/atahualpa/images/social/Twitter.png"</img></a>
    {{ DiscussionBoard.errors[7996014].message }}

Trending Topics