将Javascript插入管理员页脚wordpress

时间:2019-02-14 20:47:13

标签: javascript wordpress

我需要在所有WordPress管理页面(后端)上添加Livechat小部件。 Livechat JavaScript代码是这样的:

<!-- Start of LiveChat (www.livechatinc.com) code -->
<script type="text/javascript">
window.__lc = window.__lc || {};
window.__lc.license = 12345678;
(function() {
  var lc = document.createElement('script'); lc.type = 'text/javascript'; lc.async = true;
  lc.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'cdn.livechatinc.com/tracking.js';
  var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(lc, s);
})();
</script>
<noscript>
<a href="https://www.livechatinc.com/chat-with/10437202/" rel="nofollow">Chat with us</a>,
powered by <a href="https://www.livechatinc.com/?welcome" rel="noopener nofollow" target="_blank">LiveChat</a>
</noscript>
<!-- End of LiveChat code -->

Livechat有一个聊天插件,可在前端页面上使用。我实际上想将此代码添加到WordPress后端的每个页面中。

我本来打算通过子主题的functions.php将其放在管理页脚中,但是我不确定如何将这些代码组合在一起。我要插入子主题的代码是这样的:

function remove_footer_admin () {
    echo 'My footer text. Thank you WordPress for giving me this filter.';
}
add_filter( 'admin_footer_text', 'remove_footer_admin' );

我应该在哪里使用此代码?

1 个答案:

答案 0 :(得分:0)

您可以使用admin_footer挂钩在管理页脚中添加JavaScript代码。

如果您还需要在前端添加相同的代码,请也应用wp_footer挂钩。

以下是子主题功能中的完整代码。

// Function to render LiveChat JS code
function lh_add_livechat_js_code() {
?>
    <!-- Start of LiveChat (www.livechatinc.com) code -->
    <script type="text/javascript">
    window.__lc = window.__lc || {};
    window.__lc.license = YOUR_KEY; // use your license key here
    (function() {
      var lc = document.createElement('script'); lc.type = 'text/javascript'; lc.async = true;
      lc.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 'cdn.livechatinc.com/tracking.js';
      var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(lc, s);
    })();
    </script>
    <noscript>
    <a href="https://www.livechatinc.com/chat-with/10437202/" rel="nofollow">Chat with us</a>,
    powered by <a href="https://www.livechatinc.com/?welcome" rel="noopener nofollow" target="_blank">LiveChat</a>
    </noscript>
    <!-- End of LiveChat code -->
<?php
}
add_action( 'admin_footer', 'lh_add_livechat_js_code' ); // For back-end
add_action( 'wp_footer', 'lh_add_livechat_js_code' ); // For front-end