WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Custom Placement of Jetpack's/Sharedaddy's Sharing (22 posts)

  1. andersbalari
    Member
    Posted 2 years ago #

    I wondered how to place Jetpack's/Sharedaddy's sharing functionality where I like and only where I like.

    The automatic placement is triggered by two filter adds within the code of the plugin (within 'sharing-service.php').

    In order to override this in a specific loop it is necessary to remove these filters right before the content tag. Code then should look like this:

    <?php remove_filter( 'the_content', 'sharing_display', 19 ); ?>
      <?php remove_filter( 'the_excerpt', 'sharing_display', 19 ); ?>
      <?php the_content(); ?>

    Then just add the tag <?php echo sharing_display(); ?> wherever you like, but of course within the loop.

    I found a solution suggesting to remove the two adds from the plugin code. But my solution has two big advantages:

    • First, it survives plugin updates. Caveat: The respective code stays unchanged => this should be checked after each update.
    • Secondly, you still have the possibility of automatic sharing placement in each loop you do not modify this way.

    Cheers

    Anders

  2. Copernicus
    Member
    Posted 2 years ago #

    Thanks for this Anders.

    I had the problem that I only wanted the sharing icons/links on a few pages but didn't want to have to go through all of the already-existing pages and manually set the option for showing (or not) the Sharedaddy icons on each page.

    So, following your instructions, I removed the functionality on the template level and set the pages that I did want to have Sharedaddy to use a custom template.

    Worked great.

    Cheers, mate.

  3. sugarweb
    Member
    Posted 2 years ago #

    Thanks Anders, that code really help me too!

  4. Jonathan Wold
    Member
    Posted 2 years ago #

    Excellent tip, Anders! Thanks for sharing.

  5. mesayre
    Member
    Posted 1 year ago #

    Thanks for this useful tip, however, there's one problem with it.

    I used this method successfully on a few blogs. But when I migrated, and Jetpack was not yet installed, any page with this filter call gets served as a blank page because the filter doesn't exist. Not a problem as long as JetPack is installed. But if it gets deactivated, or the good folks at Automattic change something, this will blow up your blog.

    I'm still looking for a safe way to do this. I tried wrapping it in "if (function_exists)..." but this doesn't work either. Same behavior. I get an empty page without a spec of code. The exact same code works flawlessly on blogs that have JetPack. Anyone got any suggestions for making this safe & future-proof?

  6. andersbalari
    Member
    Posted 1 year ago #

    Thanks for the information. That indeed is a problem!

    This works for me:

    <?php if ( function_exists( 'sharing_display' ) ) remove_filter( 'the_content', 'sharing_display', 19 ); ?>
    <?php if ( function_exists( 'sharing_display' ) ) remove_filter( 'the_excerpt', 'sharing_display', 19 ); ?>

    and

    <?php if ( function_exists( 'sharing_display' ) ) echo sharing_display(); ?>

  7. mesayre
    Member
    Posted 1 year ago #

    Thanks Anders. Mmmm, that's what I had, and it was not working for me. I might give it a try again later when I've got some time.

    On a related note, I find it troubling that WordPress wasn't presenting any kind of error info when this was happening, just a completely blank page. I don't actually know enough about WP's error mechanism to know if it's possible to call sharing_display (or other plugin functions) in such a way that it will properly catch & report the error when it's not there and/or gracefully resume with a warning. I assume it was somehow leading to a critical error that stopped PHP in its tracks. Had I not known to look in that one place because I just added that function, it would've taken me a long time to locate.

  8. admturner
    Member
    Posted 1 year ago #

    Just a thought (and one I haven't had a chance to try out) but you might try wrapping it in the is_plugin_active rather than function_exists. This conditional function will check to make sure a plugin is active before executing the code inside the IF.

  9. andersbalari
    Member
    Posted 1 year ago #

    @mesayre: It is puzzling that you got just a blank page - when I tested the error the respective pages where displayed correctly up to the erroneous code line and then there was an error message, just as one would expect.

    I see no reason why function_exists should not work, perhaps you might want to try it again.

    @admturner: is_plugin_active in my opinion is too general for the case in which Automattic changes the plugin just as mesayre warned above.

  10. Gwyneth Llewelyn
    Member
    Posted 1 year ago #

    Thanks @andersbalari, your solution worked well for me. Now to get everything placed correctly using CSS... another chore :P

  11. deuts
    Member
    Posted 1 year ago #

    Unfortunately this was not working for me. I was using Automattic's own Toolbox theme. So I had to comment out the add_filter from the plugin editor. I guess I just have to watch out everytime Jetpack is updated.

  12. andersbalari
    Member
    Posted 1 year ago #

    Unfortunately this was not working for me. I was using Automattic's own Toolbox theme. So I had to comment out the add_filter from the plugin editor. I guess I just have to watch out everytime Jetpack is updated.

    That's puzzling me. Why should remove_filter not work? Did you make sure that you had no typo and placed the code in the correct position? Being dependant on manual corrections after each plugin-update is both dangerous and uncomfortable.

  13. Iva
    Member
    Posted 1 year ago #

    This does not work for me, either. Once I have added the filter and enabled sharing on specific pages; it didn't get placed where I wanted it; it's still going to its usual place, instead of a div I put it in.

  14. andersbalari
    Member
    Posted 1 year ago #

    Hi Iva,

    are you sure that you proceeded exactly as suggested? All has to take place within the loop. The filters have to be removed before the content or the excerpt is displayed and <?php echo sharing_display(); ?> has to be placed within the loop as well.

    ... Once I have added the filter ...

    Is this just a typo? The filters have to be removed, not added, in order to override the automatic placement.

    If you don't manage to make it work, just post the relevant code sections.

    Cheers

    Anders

  15. Iva
    Member
    Posted 1 year ago #

    It eventually worked; just not immediately; and yes, it was a typo. Thank you. :)

  16. frankjackson
    Member
    Posted 1 year ago #

    @andersbalari I love you man. That was awesome. 10 seconds to paste in, and works perfectly. So happy w/ this. Thanks for sharing man.

  17. Clifford Paulick
    Member
    Posted 1 year ago #

    In my experience, I have to check the box for the CPT I'm trying to add this to, but then if I add the PHP echo, it shows up twice instead of just once.
    Do I need the box checked for the CPT? I'd prefer not and then only include Jetpack Sharing only where I explicitly include it via PHP or shortcode.
    What am I missing?
    Thanks for your help.

  18. Clifford Paulick
    Member
    Posted 1 year ago #

    Update: I'm not using it in the loop. Is there any way to include it outside of the loop? I'm just using the CPT for wp-admin input and using a custom single page display code (no loop). It's a very custom display and I want to add the sharing buttons right where I want them.
    Thank you.

  19. Robert Simpson
    Member
    Posted 1 year ago #

    @andersbalari do I need to remove the filter for every loop or can I just add the remove_filter lines once to my functions.php?

  20. andersbalari
    Member
    Posted 1 year ago #

    In my experience, I have to check the box for the CPT I'm trying to add this to, but then if I add the PHP echo, it shows up twice instead of just once.

    What do you mean with CPT? I'm not familiar with this abbreviation.

    @andersbalari do I need to remove the filter for every loop or can I just add the remove_filter lines once to my functions.php?

    It should work to remove the filters within your functions.php - just give it a try and let us know the result ;-).

  21. Clifford Paulick
    Member
    Posted 1 year ago #

    CPT = "Custom Post Type"

    Sorry it wasn't clear.

  22. MACscr
    Member
    Posted 1 year ago #

    I havent had any luck with this working on my blog either. No matter what i do, it still shows up twice. Ive tried changing the priority to higher, but no difference. =/

Topic Closed

This topic has been closed to new replies.

About this Topic