Support » Plugin: Shariff Wrapper » Filter for home_url

  • Resolved Rasso Hilber

    (@nonverbla)


    Hi again πŸ™‚

    I have another question: I am developing offline, but would like to get the actual share counts from the online version of the site showing on my offline install. I think this could be achieved using the url parameter in your shortcode:

    [shariff url="http://www.example.com/"]

    But – I think it would be much cleaner to have a filter for the home_url() or something. Would this be possible?

    https://wordpress.org/plugins/shariff/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Jan-Peter

    (@starguide)

    Hi,

    you would rather need the “the_permalink” filter, but that would affect all of WordPress, not only Shariff and probably break something on your local installation.

    Just for testing purposes I would probably not consider the hassle to mess around with that.

    JP

    Thread Starter Rasso Hilber

    (@nonverbla)

    Of course I would need a custom filter from you for this πŸ™‚

    I basically just would like to have this in line 1970 of shariff.php:

    else $output .= ' data-url="' . esc_url( apply_filters('shariff_automatic_url', get_permalink() ) ) . '"';

    Then, I could do this in my functions.php file:

    function sp_shariff_automatic_url( $url ) {
      return str_replace('http://r.local', 'http://online.com', $url);
    }
    add_filter('shariff_automatic_url', 'sp_shariff_automatic_url');

    That would just be awesome!! I just tested it and it works as expected.

    Plugin Author Jan-Peter

    (@starguide)

    The problem is, if would implement this, any other plugin could modify the share url with this filter, without anybody noticing. This is a little bit too risky for a feature that is not even meant to be used on productive environments. But we could maybe set a constant in the wp-config.php to enable this feature first, just to be safe.

    Thread Starter Rasso Hilber

    (@nonverbla)

    So… I solved it like this:

    // get the current page url, no matter where we are on the site (also works for archive pages)
    function sp_get_current_url() {
      $server = untrailingslashit( $_SERVER['HTTP_HOST'] );
      $uri = $_SERVER['REQUEST_URI'];
      if( strpos($uri, '/') !== 0 ) {
        $uri = '/'.$uri;
      }
      return 'http' . (isset($_SERVER['HTTPS']) ? 's' : '') . '://' . "{$server}{$uri}";
    }
    // do the shortcode and replace the home_url() if the site is in development mode
    function sp_get_sharing_buttons() {
      $url = sp_get_current_url();
      if ('development' === WP_ENV ) {
        $url = str_replace(WP_HOME_DEV, WP_HOME_PROD, $url);
      }
      ob_start();
      echo do_shortcode("[shariff url='{$url}' backend='on' lang='en']");
      return ob_get_clean();
    }
    // call this from your template file
    echo sp_get_sharing_buttons();
    Thread Starter Rasso Hilber

    (@nonverbla)

    marking it as resolved

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Filter for home_url’ is closed to new replies.