0

Using this tutorial I was able to create sharing buttons, but the problem that the code inject right after the_content();, I would like to have the ability to call anywhere in my templates. My understanding that add_filter('the_content') is the reason for that, so how can I change it to call it anywhere.

function social_buttons($content) {
    global $post;
    $permalink = get_permalink($post->ID);
    $title = get_the_title();
    if(!is_single() && !is_page()) {
        $content = $content . '<div class="contentBottom">
        <ul class="share">
            <li><a href="#"><i class="fa fa-share-alt"></i>شارك</a>
                <ul class="social">
                   <li><a href="https://www.facebook.com/sharer/sharer.php?u='.$permalink.'"
             onclick="window.open(this.href, \'facebook-share\',\'width=580,height=296\');return false;"><i class="fa fa-facebook"></i></a></li>
                   <li><a href="http://twitter.com/share?text='.$title.'&url='.$permalink.'"
            onclick="window.open(this.href, \'twitter-share\', \'width=550,height=235\');return false;"><i class="fa fa-twitter"></i></a></li>
                   <li><a href="https://plus.google.com/share?url='.$permalink.'"
           onclick="window.open(this.href, \'google-plus-share\', \'width=490,height=530\');return false;"><i class="fa fa-google-plus"></i></a></li>
                   <li><a href="#"><i class="fa fa-envelope"></i></a></li>
               </ul>
           </li>
        </ul>
        <ul class="printComment">
            <li><a href="javascript:window.print()" rel="nofollow"><i class="fa fa-print"></i>طباعة</a></li>
            <li><a href="#"><i class="fa fa-comments-o"></i>شارك بالتعليق</a></li>
        </ul></div>';
    }
    return $content;
}
add_filter('the_content', 'social_buttons');
5
  • See this post Commented Jan 3, 2015 at 15:48
  • I was looking at it actually, but it will make me change the whole build. Is it possible to fix my code?
    – Kareem
    Commented Jan 3, 2015 at 16:08
  • @ShadyMRasmy it return with this error: Warning: Missing argument 1 for social_buttons(), defined line 1
    – Kareem
    Commented Jan 3, 2015 at 16:30
  • if you remove return $content there will be no output. call it like echo social_buttons('') with an empty string as argument to get rid of the warning.
    – Milo
    Commented Jan 3, 2015 at 16:31
  • @Milo do you mean to put echo social_buttons('') after return $content; ?
    – Kareem
    Commented Jan 3, 2015 at 16:36

1 Answer 1

1

I actually link to the wrong post although this post should still help you. The actual post I wanted to link to was this one

You should use this second link as a guide as there are some issues with your code, although not major. The biggest concern here is your repetition of code

To get back to what you actually need, you have to remove the filter and all instances of $content which is passed by reference to the the_content filter.

You can simply modify your existing code to the following

function social_buttons() {
    global $post;
    $permalink = get_permalink($post->ID);
    $title = get_the_title();
    if(!is_single() && !is_page()) { ?>
        <div class="contentBottom">
        <ul class="share">
            <li><a href="#"><i class="fa fa-share-alt"></i>شارك</a>
                <ul class="social">
                   <li><a href="https://www.facebook.com/sharer/sharer.php?u='.$permalink.'"
             onclick="window.open(this.href, \'facebook-share\',\'width=580,height=296\');return false;"><i class="fa fa-facebook"></i></a></li>
                   <li><a href="http://twitter.com/share?text='.$title.'&url='.$permalink.'"
            onclick="window.open(this.href, \'twitter-share\', \'width=550,height=235\');return false;"><i class="fa fa-twitter"></i></a></li>
                   <li><a href="https://plus.google.com/share?url='.$permalink.'"
           onclick="window.open(this.href, \'google-plus-share\', \'width=490,height=530\');return false;"><i class="fa fa-google-plus"></i></a></li>
                   <li><a href="#"><i class="fa fa-envelope"></i></a></li>
               </ul>
           </li>
        </ul>
        <ul class="printComment">
            <li><a href="javascript:window.print()" rel="nofollow"><i class="fa fa-print"></i>طباعة</a></li>
            <li><a href="#"><i class="fa fa-comments-o"></i>شارك بالتعليق</a></li>
        </ul></div>
    <?php 
}
}

You can now call the function where you like as follows

social_buttons();
2
  • no more errors, thanks to you. just a curly bracket missed at the end of the code
    – Kareem
    Commented Jan 3, 2015 at 18:58
  • Great, glad it worked. Thanks for the feedback. Must admit, did not test the code, you are correct, missing a curly at the end. Enjoy :-) Commented Jan 3, 2015 at 19:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.