使用简单的 CSS 创建没有 JavaScript 的超快社交分享按钮
已发表: 2019-01-23
我们很高兴地宣布我们全新的插件Crunchy Sharing 。
Crunchy Sharing – 我们很高兴地宣布我们全新的超快速#WordPress社交分享
#插件:https://pro.crunchify.com/crunchy-sharing/超过 25 个社交分享选项,优化、无 JavaScript、响应式、轻量级、最快、符合 GDPR。
查看所有屏幕截图:
我们无法想象没有Social Sharing
按钮的网站。 这是必须且绝对需要的。 正如本教程中的标题所述,我们将介绍how to set up Social Sharing buttons
的步骤:
-
without
使用任何 WordPressPlugins
-
without
任何JavaScripts
-
without
任何performance
影响
这是我写的关于如何通过消除 WordPress 插件来优化您的 WordPress 网站的更多教程。 值得检查。
您是否有以下任何问题,那么您来对地方了:
- 如何实施社交分享
- 没有 JavaScript 的简单 FontAwesome 社交分享按钮
- 社交媒体共享按钮。 没有 JavaScript。 没有跟踪。 超级快。
- 为社交媒体提供共享链接的最简单方法
- 将社交分享按钮添加到任何 WordPress 页面,无需插件。
- 无需 JavaScript 和跟踪的简单社交分享按钮
- DIY 社交分享链接:无需插件 WP 添加社交分享按钮
让我先说一下这背后的一个概念:

为什么要实现自己的社交分享按钮?
- 并非所有但大多数社交共享插件都没有针对您的需求进行优化
- 他们可能会单独加载社交共享图标,这会增加对您服务器的不必要的 HTTP 请求
- 如果您使用的是官方社交分享按钮,那么它
loads Java Script for each Sharing button
这最终会显着影响您的页面加载速度 - 如果您的页面速度很高,您的网站将在 Google 搜索中排名靠前
- 如果您的页面速度很高,用户将更有可能更频繁地返回您的网站,因为这对他们来说是流畅的体验
让我们检查一下您的网站为上述 5 个社交分享插件加载的所有脚本。 如果您添加更多,则为每个add an extra script
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
======== > Twitter script : < ======== <script> ! function ( d , s , id ) { var js , fjs = d . getElementsByTagName ( s ) [ 0 ] , p =/^ http : / . test ( d . location ) ? 'http' : 'https' ; if ( ! d . getElementById ( id ) ) { js = d . createElement ( s ) ; js . id = id ; js . src = p + '://platform.twitter.com/widgets.js' ; fjs . parentNode . insertBefore ( js , fjs ) ; } } ( document , 'script' , 'twitter-wjs' ) ; </script> ======== > Facebook HTML5 script : < ========= <script> ( function ( d , s , id ) { var js , fjs = d . getElementsByTagName ( s ) [ 0 ] ; if ( d . getElementById ( id ) ) return ; js = d . createElement ( s ) ; js . id = id ; js . src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&version=v2.0" ; fjs . parentNode . insertBefore ( js , fjs ) ; } ( document , 'script' , 'facebook-jssdk' ) ) ; </script> ======== > Buffer script : < ======== <script type = "text/javascript" src = "https://d389zggrogs7qo.cloudfront.net/js/button.js" > </script> ======== > Google + script : < ======== <script type = "text/javascript" > ( function ( ) { var po = document . createElement ( 'script' ) ; po . type = 'text/javascript' ; po . async = true ; po . src = 'https://apis.google.com/js/platform.js' ; var s = document . getElementsByTagName ( 'script' ) [ 0 ] ; s . parentNode . insertBefore ( po , s ) ; } ) ( ) ; </script> ======== > Pinterest script : < ======== <script async defer src = "//assets.pinterest.com/js/pinit.js" > </script> ======== > LinkedIn script : < ======== <script src = "//platform.linkedin.com/in.js" type = "text/javascript" > lang : en_US </script> |
no point to load above scripts on each and every page
。
现在让我们开始吧:创建社交Sharing
按钮
第1步
转到主题的function.php
文件并粘贴以下代码。 这将bottom of the post
添加分享按钮。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
function crunchify_social_sharing_buttons ( $content ) { global $post ; if ( is_singular ( ) | | is_home ( ) ) { // Get current page URL $crunchifyURL = urlencode ( get_permalink ( ) ) ; // Get current page title $crunchifyTitle = htmlspecialchars ( urlencode ( html_entity_decode ( get_the_title ( ) , ENT_COMPAT , 'UTF-8' ) ) , ENT_COMPAT , 'UTF-8' ) ; // $crunchifyTitle = str_replace( ' ', '%20', get_the_title()); // Get Post Thumbnail for pinterest $crunchifyThumbnail = wp_get_attachment_image_src ( get_post_thumbnail_id ( $post - > ID ) , 'full' ) ; // Construct sharing URL without using any script $twitterURL = 'https://twitter.com/intent/tweet?text=' . $crunchifyTitle . '&url=' . $crunchifyURL . '&via=Crunchify' ; $facebookURL = 'https://www.facebook.com/sharer/sharer.php?u=' . $crunchifyURL ; $googleURL = 'https://plus.google.com/share?url=' . $crunchifyURL ; $bufferURL = 'https://bufferapp.com/add?url=' . $crunchifyURL . '&text=' . $crunchifyTitle ; $linkedInURL = 'https://www.linkedin.com/shareArticle?mini=true&url=' . $crunchifyURL . '&title=' . $crunchifyTitle ; // Based on popular demand added Pinterest too $pinterestURL = 'https://pinterest.com/pin/create/button/?url=' . $crunchifyURL . '&media=' . $crunchifyThumbnail [ 0 ] . '&description=' . $crunchifyTitle ; // Add sharing button at the end of page/page content $content . = '<!-- Implement your own superfast social sharing buttons without any JavaScript loading. No plugin required. Detailed steps here: https://crunchify.com/?p=7526 -->' ; $content . = '<div class="crunchify-social">' ; $content . = '<h5>SHARE ON</h5> <a class="crunchify-link crunchify-twitter" href="' . $twitterURL . '" target="_blank">Twitter</a>' ; $content . = '<a class="crunchify-link crunchify-facebook" href="' . $facebookURL . '" target="_blank">Facebook</a>' ; $content . = '<a class="crunchify-link crunchify-googleplus" href="' . $googleURL . '" target="_blank">Google+</a>' ; $content . = '<a class="crunchify-link crunchify-buffer" href="' . $bufferURL . '" target="_blank">Buffer</a>' ; $content . = '<a class="crunchify-link crunchify-linkedin" href="' . $linkedInURL . '" target="_blank">LinkedIn</a>' ; $content . = '<a class="crunchify-link crunchify-pinterest" href="' . $pinterestURL . '" data-pin-custom="true" target="_blank">Pin It</a>' ; $content . = '</div>' ; return $content ; } else { // if not a post/page then don't include sharing button return $content ; } } ; add_filter ( 'the_content' , 'crunchify_social_sharing_buttons' ) ; |
我们使用the_content
WordPress 钩子。 它显示当前帖子/页面的内容。
这是您的代码在 functions.php 文件中的样子。

第2步
打开您的 WordPress 主题的style.css
文件并在下面放置代码以获得更好的样式。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
.crunchify-link { padding : 2px 8px 4px 8px !important ; color : white ; font-size : 12px ; border-radius : 2px ; margin-right : 2px ; cursor : pointer ; -moz-background-clip : padding ; -webkit-background-clip : padding-box ; box-shadow : inset 0 -3px 0 rgba ( 0,0,0,.2 ) ; -moz-box-shadow : inset 0 -3px 0 rgba ( 0,0,0,.2 ) ; -webkit-box-shadow : inset 0 -3px 0 rgba ( 0,0,0,.2 ) ; margin-top : 2px ; display : inline-block ; text-decoration : none ; } .crunchify-link:hover,.crunchify-link:active { color : white ; } .crunchify-twitter { background : #00aced ; } .crunchify-twitter:hover,.crunchify-twitter:active { background : #0084b4 ; } .crunchify-facebook { background : #3B5997 ; } .crunchify-facebook:hover,.crunchify-facebook:active { background : #2d4372 ; } .crunchify-googleplus { background : #D64937 ; } .crunchify-googleplus:hover,.crunchify-googleplus:active { background : #b53525 ; } .crunchify-buffer { background : #444 ; } .crunchify-buffer:hover,.crunchify-buffer:active { background : #222 ; } .crunchify-pinterest { background : #bd081c ; } .crunchify-pinterest:hover,.crunchify-pinterest:active { background : #bd081c ; } .crunchify-linkedin { background : #0074A1 ; } .crunchify-linkedin:hover,.crunchify-linkedin:active { background : #006288 ; } .crunchify-social { margin : 20px 0px 25px 0px ; -webkit-font-smoothing : antialiased ; font-size : 12px ; } |
第三步
确保清除站点缓存。 我在我的网站上使用 WP Super Cache 插件。 如果您想优化 WP Super Cache 的所有设置,请按照教程进行操作。

第四步
如果您之前安装过其他社交分享插件,请Deactivate
并Delete
它。 That's it
。
您应该在您的网站上看到漂亮的共享按钮。 查看 Crunchify 上每篇文章末尾的实时示例。
NOTE:
我在我的博客上使用 ShortURL。 如果您想拥有more social sharing buttons
,请访问教程。
如果您想top of the post
显示共享按钮,请使用此代码(仅限第 1 步):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
function crunchify_social_sharing_buttons ( $ content ) { global $ post ; if ( is_singular ( ) | | is_home ( ) ) { // Get current page URL $ crunchifyURL = urlencode ( get_permalink ( ) ) ; // Get current page title $ crunchifyTitle = htmlspecialchars ( urlencode ( html_entity_decode ( get_the_title ( ) , ENT_COMPAT , 'UTF-8' ) ) , ENT_COMPAT , 'UTF-8' ) ; // $crunchifyTitle = str_replace( ' ', '%20', get_the_title()); // Get Post Thumbnail for pinterest $ crunchifyThumbnail = wp_get_attachment_image_src ( get_post_thumbnail_id ( $ post - > ID ) , 'full' ) ; // Construct sharing URL without using any script $ twitterURL = 'https://twitter.com/intent/tweet?text=' . $ crunchifyTitle . '&url=' . $ crunchifyURL . '&via=Crunchify' ; $ facebookURL = 'https://www.facebook.com/sharer/sharer.php?u=' . $ crunchifyURL ; $ googleURL = 'https://plus.google.com/share?url=' . $ crunchifyURL ; $ bufferURL = 'https://bufferapp.com/add?url=' . $ crunchifyURL . '&text=' . $ crunchifyTitle ; $ linkedInURL = 'https://www.linkedin.com/shareArticle?mini=true&url=' . $ crunchifyURL . '&title=' . $ crunchifyTitle ; // Based on popular demand added Pinterest too $ pinterestURL = 'https://pinterest.com/pin/create/button/?url=' . $ crunchifyURL . '&media=' . $ crunchifyThumbnail [ 0 ] . '&description=' . $ crunchifyTitle ; // Add sharing button at the end of page/page content $ variable . = '<!-- Implement your own social sharing buttons without any JavaScript loading. No plugin required. Detailed steps here: https://crunchify.com/?p=7526 -->' ; $ variable . = '<div class="crunchify-social">' ; $ variable . = '<a class="crunchify-link crunchify-twitter" href="' . $ twitterURL . '" target="_blank">Twitter</a>' ; $ variable . = '<a class="crunchify-link crunchify-facebook" href="' . $ facebookURL . '" target="_blank">Facebook</a>' ; $ variable . = '<a class="crunchify-link crunchify-googleplus" href="' . $ googleURL . '" target="_blank">Google+</a>' ; $ variable . = '<a class="crunchify-link crunchify-buffer" href="' . $ bufferURL . '" target="_blank">Buffer</a>' ; $ variable . = '<a class="crunchify-link crunchify-linkedin" href="' . $ linkedInURL . '" target="_blank">LinkedIn</a>' ; $ variable . = '<a class="crunchify-link crunchify-pinterest" href="' . $ pinterestURL . '" data-pin-custom="true" target="_blank">Pin It</a>' ; $ variable . = '</div>' ; return $ variable . $ content ; } else { // if not a post/page then don't include sharing button return $ variable . $ content ; } } ; add_filter ( 'the_content' , 'crunchify_social_sharing_buttons' ) ; |
查看我们的 Crunchy Sharing WordPress 插件:
15% OFF Crunchy 分享插件。 使用代码: CRUNCHIFY 15