0

I want to create a bootstrap modal for social sharing buttons in my wordpress blog. For the modal to be visible, I have to place it outside the post (loop) but its trigger (button) inside the post. I want to pass the URL and Title of the post to the Sharing Buttons inside the modal. I have created two variables inside the post to get its URL and title:

 <?php 
 // Get current post URL 
 $URL = get_permalink();     
 // Get current post title
 $Title = str_replace( ' ', '%20', get_the_title());  ?>

Then, I created the trigger inside the post to display the modal

<button class="btn btn-link btn-lg" id="socialbtn" data-toggle="modal" data-target="#myModal">Share</button>

And in the modal body, I have many buttons, like

<a class="ssk ssk-text ssk-facebook" href="https://www.facebook.com/sharer.php?u=***URL of the post***">Facebook</a>    
<a class="ssk ssk-text ssk-twitter" href="https://twitter.com/share?url=$URL&text=$Title$" >Twitter</a>

What I want to do is to replace the $URL in href of Facebook and Twitter with the URL of the post and likewise the title. I know that I have to use JavaScript, but I'm unable to figure out the correct syntax. Will be thankful for help. :)

1

6 Answers 6

1

I did it like this: In the trigger button, I set the data attributes as follows:

<button class="btn btn-link btn-lg" id="socialbtn" data-url="<?php echo $URL; ?>" data-title="<?php echo $Title; ?>">Share</button>

then got the facebook and twitter URLs in separate variables and then set the href attributes as follows:

<script>
$(document).on("click", "#socialbtn", function () {
     var url = $(this).data('url');
     var title = $(this).data('title');
     var fburl = "https://www.facebook.com/sharer.php?u="+url;
     var twurl = "https://twitter.com/share?url="+url+"&text="+title;
     $("#facebook").attr("href", fburl);
     $("#twitter").attr("href", twurl);
</script>

And in the modal body, used the anchor tags as follows:

<a class="ssk ssk-text ssk-facebook" id="facebook" href="#">Facebook</a>
<a class="ssk ssk-text ssk-twitter" id="twitter" href="#">Twitter</a>
Sign up to request clarification or add additional context in comments.

Comments

0

You have to output them using PHP:

<a class="ssk ssk-text ssk-facebook" href="https://www.facebook.com/sharer.php?u=<?php echo $URL; ?>">Facebook</a>

<a class="ssk ssk-text ssk-twitter" href="https://twitter.com/share?url=<?php echo $URL; ?>&text=<?php echo $Title; ?>" >Twitter</a>

2 Comments

There is one modal and multiple posts in the loop - the OP is looking for a way to set content in the modal depending on which button is clicked
Yes exactly, there is one modal and multiple posts.
0

$(".ssk-facebook").attr("href", "YourNewURL");

$(".ssk-twitter").attr("href", "YourNewURL");

1 Comment

This could be a hint in a comment, but it is no where near complete enough for an answer
0
$( "#socialbtn" ).click(function() {

   $(".ssk-facebook").attr("href", "https://www.facebook.com/sharer.php?u=<?= $URL ?>");

   $(".ssk-twitter").attr("href", "https://twitter.com/share?url=<?= $URL ?>&text=<?= $Title ?>");

});

Comments

0

You have to identify which button the user have clicked on.

You can archive that by using unique IDs OR by adding hidden inputs or divs containing the correct hrefs for each post.

Then select those "href-container" relatively to the button and change the modal. ("closest(), parent(), nextAll() etc.)

Do you need code examples?

Comments

0

Use data attribute to add link with every post's share button:-

HTML

<!-- Share button (PLACE IT INSIDE YOUR LOOP) -->
<a data-url="<?php echo url; ?>" data-title="<?php echo $title;?>" class="open-share btn btn-primary" href="#">share</a>

<!-- Model popup (PLACE IT AFTER LOOP) -->
<div class="modal hide" id="sharepopup">
 <div class="modal-header">
    <button class="close" data-dismiss="modal">×</button>
    <h3>Share </h3>
  </div>
    <div class="modal-body">
<a id="facebook" class="ssk ssk-text ssk-facebook">Facebook</a>

<a id="twitter" class="ssk ssk-text ssk-twitter" >Twitter</a>

    </div>
</div>

JS

<script>
$(document).on("click", ".open-share", function () {
     var url = $(this).data('url');
     var title = $(this).data('title');
       $('#facebook').attr("href", "http://facebook.com?share="+url);
       $('#facebook').attr("title", "http://twitter.com?share"+title);
       $('#twitter').attr("href", url);
       $('#twitter').attr("title", title);
// Initialize popup with Javascript.
     $('#sharepopup').modal('show');
});


</script>

5 Comments

Tried, but as I said there are many buttons (of different posts) on the page, it passes the url and title of only the first post on that page
Sorry ! I thought you have only one post, for making share button for different posts i suggest you to use data- tag with model. See the updated code . If everything is working please dont forget mark my answer as correct and vote for my answer.
Bro, The href of the facebook or twitter button vl not be just $('#facebook').attr("href", url); rather $url is to be added after facebook.com/sharer.php?u= I tried doing this: $("#facebook").attr("href", "facebook.com/sharer.php?u=<?= $url ?>") with with #facebook being a valid identifier, but it didn't work
Ok. I will update the code with facebook.com share prefix url.
Manzoor , If everything is working, please don't forget to mark my answer as right answer and give vote also.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.