Title: Share Target
Author: Felix Arntz
Published: <strong>2021 年 4 月 6 日</strong>
Last modified: 2021 年 4 月 6 日

---

搜索插件

![](https://ps.w.org/share-target/assets/banner-772x250.png?rev=2509752)

**该插件尚未通过WordPress的最新3个主要版本进行测试**。 当与较新版本的WordPress一起
使用时，可能不再受到维护或支持，并且可能会存在兼容性问题。

![](https://ps.w.org/share-target/assets/icon-256x256.png?rev=2509752)

# Share Target

 作者：[Felix Arntz](https://profiles.wordpress.org/flixos90/)

[下载](https://downloads.wordpress.org/plugin/share-target.1.0.0-beta.1.zip)

 * [详情](https://cn.wordpress.org/plugins/share-target/#description)
 * [评价](https://cn.wordpress.org/plugins/share-target/#reviews)
 *  [安装](https://cn.wordpress.org/plugins/share-target/#installation)
 * [开发进展](https://cn.wordpress.org/plugins/share-target/#developers)

 [支持](https://wordpress.org/support/plugin/share-target/)

## 描述

By using this plugin, you can share content like images, media, URLs and text directly
to your WordPress site from a [capable device and browser](https://caniuse.com/web-app-manifest).
For example, you can share a photo that you just took on your phone to your WordPress
site in the same way that you would otherwise share it with your friends in a native
messaging app.

When you share content to your WordPress site using this plugin, it will automatically
start a new block editor draft post with it; you can then just add anything else
you like, or immediately publish it as is. For certain use-cases, e.g. an image 
blog, creating a post with the shared image might be everything that’s needed before
publishing right away!

To name a few other examples of what you can share to your WordPress site using 
this plugin, think about audio and video files, short text notes, Spotify playlists–
basically anything that you can share from an app on your phone you can now also
share directly to your WordPress site.

#### Requirements

In order to use this plugin, you will also need to install and activate the [PWA plugin](https://wordpress.org/plugins/pwa/),
which provides the basic infrastructure for your site to become a PWA.

#### Usage

With both plugins active, once you visit your site on a [capable device and browser](https://caniuse.com/web-app-manifest),
you should see a prompt (via the PWA plugin’s functionality) to add the plugin to
your home screen (e.g. similar to an app on your phone).

Once you have added your website to the home screen of your device, it should be
available as a target when sharing any content from that device, e.g. a photo or
a URL.

When you share something to your WordPress site, the installed PWA of your website
should open. You should then (potentially after logging in) land directly on a new
post in the block editor where the shared content has already been added. If you
are sharing a media file (e.g. an image or video file), it will be automatically
added to your site’s media library.

#### Background information

This plugin is powered by the Web Share Target API. The [Web Share Target API](https://web.dev/web-share-target/)
is a modern browser API which allows sharing images, media, URLs and basic text 
content directly to your website, relying on the regular sharing UI that is for 
example integrated in your phone. [See here for more technical background information about the Web Share Target API.](https://w3c.github.io/web-share-target/)

#### Third-party developer API

The Share Target plugin allows other plugins to integrate with it. You can customize
what should happen with incoming shared content when it arrives in the block editor,
conditionally overriding the default behavior of the plugin.

In PHP, enqueue your custom JavaScript file. Make sure to include the `share-target`
script in its dependencies. For example:

    ```
    wp_enqueue_script(
      'my-share-target-handler',
      '/assets/js/my-share-target-handler.js',
      array( 'share-target' )
    );
    ```

In your JavaScript file you can then add your custom share handler using the following
function:

**`wp.shareTarget.registerShareHandler( options )`**

Registers a new share handler for incoming shared data.

Parameters:

 * `options`: _(Object) (required)_ Handler options.
 * `options.handle`: _(Function) (required)_ Share handler function. Must be asynchronous
   and accept an object with properties `title`, `description`, `link` (each strings),
   and `attachment` (object). Any of these may be undefined. Depending on the data,
   the function should decide whether to handle it and if so run the necessary logic
   and return true, to stop following handlers from being called. Otherwise, it 
   should return false.
 * `options.priority`: _(number) (optional)_ Priority for the handler. A lower number
   means higher priority, like for WordPress hooks. Default is 10.

The following example handles any shared Spotify content and embeds it into the 
post:

    ```
    // Matches a shared Spotify URL.
    const spotifyRegex = /^https:\/\/open\.spotify\.com/;

    wp.shareTarget.registerShareHandler( {
      handle: async ( { link, attachment } ) => {
        // Do not handle if a media file is being shared.
        if ( attachment ) {
          return false;
        }

        // If a shared Spotify URL, embed it.
        if ( link && link.match( spotifyRegex ) ) {
          wp.data.dispatch( 'core/block-editor' ).insertBlocks( [
            wp.blocks.createBlock( 'core/embed', {
              url: link,
              type: 'rich',
              providerNameSlug: 'spotify',
              responsive: true,
            } ),
          ] );
          return true;
        }

        // Otherwise fall back to other handlers.
        return false;
      },
      priority: 5,
    } );
    ```

## 屏幕截图

 * [[
 * Sharing media and text from a notes app on the phone directly into a WordPress
   site about cocktails
 * [[
 * The shared content from the other screenshot as initially displayed in the block
   editor, with only one click left to publish

## 安装

**Make sure you have the [PWA plugin](https://wordpress.org/plugins/pwa/) installed
before using the Share Target plugin.**

 1. Upload the entire `share-target` folder to the `/wp-content/plugins/` directory
    or download it through the WordPress backend.
 2. Activate the plugin through the ‘Plugins’ menu in WordPress.

## 常见问题

### Why does my website not show up as a target for shared content?

In order for your website to show up in your device’s integrated sharing UI, you
need to install your website as a PWA on your device. Make sure to use a [browser and OS that supports adding your website as a PWA to your homescreen](https://caniuse.com/web-app-manifest).

### Where should I submit my support request?

For regular support requests, please use the [wordpress.org support forums](https://wordpress.org/support/plugin/share-target).
If you have a technical issue with the plugin where you already have more insight
on how to fix it, you can also [open an issue on Github instead](https://github.com/felix-arntz/wp-share-target/issues).

### How can I contribute to the plugin?

If you have some ideas to improve the plugin or to solve a bug, feel free to raise
an issue or submit a pull request in the [Github repository for the plugin](https://github.com/felix-arntz/wp-share-target).
Please stick to the [contributing guidelines](https://github.com/felix-arntz/wp-share-target/blob/main/CONTRIBUTING.md).

You can also contribute to the plugin by translating it. Simply visit [translate.wordpress.org](https://translate.wordpress.org/projects/wp-plugins/share-target)
to get started.

## 评价

此插件暂无评价。

## 贡献者及开发者

「Share Target」是开源软件。 以下人员对此插件做出了贡献。

贡献者

 *   [ Felix Arntz ](https://profiles.wordpress.org/flixos90/)

[帮助将「Share Target」翻译成简体中文。](https://translate.wordpress.org/projects/wp-plugins/share-target)

### 对开发感兴趣吗?

您可以[浏览代码](https://plugins.trac.wordpress.org/browser/share-target/)，查看
[SVN仓库](https://plugins.svn.wordpress.org/share-target/)，或通过[RSS](https://plugins.trac.wordpress.org/log/share-target/?limit=100&mode=stop_on_copy&format=rss)
订阅[开发日志](https://plugins.trac.wordpress.org/log/share-target/)。

## 更新日志

#### 1.0.0-beta.1

 * Initial beta release

## 额外信息

 *  版本 **1.0.0-beta.1**
 *  最后更新：**5 年前**
 *  活跃安装数量 **不到10**
 *  WordPress 版本 ** 5.2 或更高版本 **
 *  已测试的最高版本为 **5.7.15**
 *  PHP 版本 ** 7.0 或更高版本 **
 *  语言
 * [English (US)](https://wordpress.org/plugins/share-target/)
 * 标签
 * [image](https://cn.wordpress.org/plugins/tags/image/)[media](https://cn.wordpress.org/plugins/tags/media/)
   [sharing](https://cn.wordpress.org/plugins/tags/sharing/)[web](https://cn.wordpress.org/plugins/tags/web/)
 *  [高级视图](https://cn.wordpress.org/plugins/share-target/advanced/)

## 评级

尚未提交反馈。

[Your review](https://wordpress.org/support/plugin/share-target/reviews/#new-post)

[查看全部评论](https://wordpress.org/support/plugin/share-target/reviews/)

## 贡献者

 *   [ Felix Arntz ](https://profiles.wordpress.org/flixos90/)

## 支持

有话要说吗？是否需要帮助？

 [查看支持论坛](https://wordpress.org/support/plugin/share-target/)