• Resolved andersbalari

    (@andersbalari)


    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

Viewing 15 replies - 1 through 15 (of 21 total)
  • 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.

    Thanks Anders, that code really help me too!

    Excellent tip, Anders! Thanks for sharing.

    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?

    Thread Starter andersbalari

    (@andersbalari)

    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(); ?>

    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.

    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.

    Thread Starter andersbalari

    (@andersbalari)

    @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.

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

    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.

    Thread Starter andersbalari

    (@andersbalari)

    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.

    Iva

    (@supersonicsquirrel)

    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.

    Thread Starter andersbalari

    (@andersbalari)

    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

    Iva

    (@supersonicsquirrel)

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

    frankjackson

    (@frankjackson)

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

Viewing 15 replies - 1 through 15 (of 21 total)
  • The topic ‘Custom Placement of Jetpack's/Sharedaddy's Sharing’ is closed to new replies.