惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V2EX - 技术
V2EX - 技术
T
Troy Hunt's Blog
The Last Watchdog
The Last Watchdog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
S
Secure Thoughts
Hacker News - Newest:
Hacker News - Newest: "LLM"
WordPress大学
WordPress大学
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
博客园 - 司徒正美
TaoSecurity Blog
TaoSecurity Blog
博客园 - Franky
有赞技术团队
有赞技术团队
Google Online Security Blog
Google Online Security Blog
罗磊的独立博客
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
NISL@THU
NISL@THU
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Recent Commits to openclaw:main
Recent Commits to openclaw:main
K
Kaspersky official blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
W
WeLiveSecurity
SecWiki News
SecWiki News
Google DeepMind News
Google DeepMind News
O
OpenAI News
V
V2EX
AI
AI
博客园_首页
Apple Machine Learning Research
Apple Machine Learning Research
大猫的无限游戏
大猫的无限游戏
美团技术团队
C
Cyber Attacks, Cyber Crime and Cyber Security
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 三生石上(FineUI控件)
G
GRAHAM CLULEY
月光博客
月光博客
T
Threatpost
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
V
Vulnerabilities – Threatpost
Last Week in AI
Last Week in AI
爱范儿
爱范儿
C
CXSECURITY Database RSS Feed - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Cloudbric
Cloudbric
酷 壳 – CoolShell
酷 壳 – CoolShell
The Hacker News
The Hacker News
N
News | PayPal Newsroom
Know Your Adversary
Know Your Adversary

Example blog

Ghost routing explained with routes.yaml examples — routes, collections & taxonomies Ghost redirects explained with practical redirects.yaml examples How to Serve JSON or XML Data in Ghost How to Change Your Default Post Template in Ghost Ghost Custom Theme Settings How to add Google Fonts to Ghost Themes How to Configure Ghost with Mailgun Ghost Theme Structure & Organization How to Make Custom Templates in Ghost Themes How to Install Ghost CMS via Ghost-CLI How to Add Syntax Highlighting to Ghost The best Ghost CMS hosting platforms Ghost 6.0 — Social Web and Native Ghost Analytics Ghost CMS Social web beta, email 2FA and more author social links How to keep custom design settings when updating your Ghost theme How to use the public preview card in Ghost How to add members-specific sections in Ghost How to customize the default content CTA in Ghost How to build a custom homepage in Ghost An overview of Ghost editor cards and and how they work Ghost CMS News: One-time payments & collecting sales tax How to customize the private site access template in Ghost How to add custom fonts in Ghost themes Ghost CMS News: Additional payment methods & internal linking How to set meta data of your custom routes & collections in Ghost Ghost CMS News: TK reminders, ActivityPub and improvements How to format dates and use the date helper in Ghost Ghost CMS internationalization, custom fonts & spam protection Understanding and optimizing post and page settings in Ghost CMS Trigger the Ghost Portal popup based on page scroll progress How to Deploy Your Ghost Theme with GitHub Actions How to create a custom recommendations page for your Ghost CMS theme Ghost CMS News: New editor, refreshed settings & recommendations Ghost CMS News: Source - the new default theme and native image editing How to Add Different Logos for Dark & Light Themes in Ghost Upgrade to Node v18 for Ghost CMS & common errors Ghost Snippets: Adding Schema markup in Ghost How to add a scroll-based progress bar to your Ghost CMS site How to Create a Custom Navigation Template in Ghost How to Segment Subscribers with Member Labels in Ghost The best way to create a contact page in Ghost How to Use the Ghost Subscription Forms in Your Theme & Posts How to update your Ghost site URL when self-hosting Ghost CMS News: Signup cards and better landing pages How to Add Breadcrumbs to Your Ghost Theme How to integrate the Applause Button into your Ghost theme How to Hide a Post From the Home Page in Ghost CMS Ghost CMS block and contentFor helper overview & tips How to add Image Lightbox to Ghost Themes Ghost CMS News: Signup form embed, new editor and bulk actions. Biron Themes becomes Bright Themes 🎉 How to Audit & Test a Ghost Theme How to Create Related Posts Section in Ghost How to Use Ghost Filter Expressions Ghost CMS newsletter settings and post analytics How to Update Your Ghost Theme When We Release New Versions Ghost CMS News: Automatic list cleaning & email troubleshooting How to Create a Featured Post Slider in Ghost How to Add Reading Time to Ghost Blog Posts How to Create a Post Archive for Your Ghost Theme Ghost CMS Price Helper Overview & Tips Self-Hosted Ghost CMS with DigitalOcean Ghost CMS News: Member Growth Sources & Audience Feedback How to Open External Links in a New Tab in Ghost How to Integrate Google Analytics with Ghost Ghost CMS News: History Log & Newsletter Analytics How to Automatically Add Member Labels From Referral Traffic in Ghost? Ghost CMS News: Free trials & email notifications Ghost CMS Match Helper Usage & Tips Ghost CMS News: Native Comments Ghost CMS News: Ghost Explore & Premium Newsletter Previews Ghost Snippets: Custom Progress Cards & Steps Ghost CMS News: Native Search The Best Ghost CMS Search Plugins & Libraries Ghost Snippets: Custom Stylish Tables How to Add a Scroll Top & Progress Indicator Button in Ghost Ghost Snippets: Accordions How to Add Tag Dropdown to Your Ghost Theme Ghost 5.0 released 🎉 Ghost Snippets: Custom Subscribe Forms Ghost Snippets: Custom Alert Boxes How to Show Post or Newsletter Issue Number in Ghost How to Self-Host Google Fonts in Ghost CMS Ghost CMS News: Membership, Tiers & Activity Feed Custom Pages in Ghost CMS Themes Ghost CMS Search Using Fuse.js Ghost CMS News: Offers and Discounts & New Cards Ghost CMS News: Membership tiers, email CTA & filters Ghost CMS News: Grammarly Support & Feature Image Captions. Ghost CMS News: Improved Member Accounts & Personalized Newsletter. Ghost 4.0 Launched With Native Newsletters & Membership Ghost CMS News: Cancellation Feedback & Newsletter Stats. Ghost CMS News: Improved Member Dashboard & Sidebar Views. Ghost CMS News: Google Pay support, email newsletters & secondary navigation Ghost CMS News: Member Support, Portal & Content Snippets. Ghost CMS News: Referral Programs & Responsive Images. Ghost Themes as Progressive Web Apps Ghost 3.0 is here with brand new features Ghost 2.0 is here with new features How to Add Search to Ghost Using ghostHunter
How to use Code Injection in Ghost
Bright Themes · 2024-07-01 · via Example blog

Code Injection in Ghost CMS is a robust and powerful feature that provides you with the ability to inject custom scripts or styles directly into your website. This feature opens up a realm of possibilities for customization that extends far beyond what custom theme settings alone can expose in Ghost Admin. With Code Injection, you can seamlessly integrate third-party tools, implement custom styles, or embed tracking scripts to enhance your site's functionality.

How Code Injection works?

As the name suggests Code Injection injects code into your website in specific predefined places. Code can be injected into either the header or the footer of a webpage. These two areas of a webpage serve different purposes when it comes to code injection. The Site Header box is designed for styles and code that need to run early in the loading process, while the Site Footer box is catered towards code that is meant to run later, such as tracking scripts.

Code Injection is added via the {{ghost_head}} and {{ghost_foot}} helpers, which are included in the default.hbs of Ghost themes, before the closing </head> and </body> tags respectively.

<html>
  <head>
    <title>{{title}}</title>
      
    {{ghost_head}}<!-- Code Injection Header added here -->  
  </head>

  <body>
    <!-- Your elements & content -->

    {{ghost_foot}}<!-- Code Injection Footer added here -->    
  </body>
</html>

Furthermore, Code Injection can be defined at the site level or at individual post/page level. Site-wide code injection applies to all pages across the site, allowing you to add custom code that will affect the entire site. On the other hand, post-specific code injection only applies to individual posts, allowing for unique customization on a per-post basis.

How to use Code Injection?

To use Code Injection, in your Ghost admin dashboard go to Settings > Code Injection. There you have access the site-wide Code Injection (/ghost/#/settings/code-injection), with the two main sections: Site header and Site footer.

<style>
  /* custom styles and css should go in the header */
</style>

Everything in the site footer will be injected before the closing </head> tag.

<script>
  /* custom scripts and js generally go in the footer */
</script>

Everything in the site footer will be injected before the closing </body> tag.

For individual post/page injections you can open a specific post/page in the Admin, then from the Post settings > Code injection you get the two main sections: Post header and Post footer.

Common use cases for Code Injection

By injecting custom code, you can tailor your site to meet specific needs and preferences. Here are some common use cases for code injection in Ghost CMS:

Adding custom styles

One of the most frequent uses of code injection is to add custom styles to your Ghost site. While Ghost themes provide a great starting point, there are times when you need to fine-tune the design to align with your brand's identity or specific aesthetic preferences. By injecting CSS code, you can make these adjustments seamlessly.

For example, if you want to change the background color of your site’s header or adjust the font size of your blog titles, you can easily achieve this with custom CSS. In such a case, you should add the code in the header section, between <style></style> tags, for example:

<style>
  .header { background-color: blue; }
  .post-hero-title { font-size: 4rem; }
</style>

By adding this code in the Code Injection section under Settings > Code Injection > Site Header, you ensure that these styles are applied across all pages of your site.

Adding third party integrations

Another common use case for code injection is integrating third-party services. Whether you want to add analytics, social media widgets, or marketing tools, code injection provides a straightforward way to embed these services into your Ghost site.

For instance, if you want to integrate Google Analytics to track your website traffic, you can do so by injecting the necessary script into the Site Footer section:

<script async src="https://www.googletagmanager.com/gtag/js?id={your_id}"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());
  gtag('config', '{your_id}');
</script>

These integrations generally provide the code you need to add in Code Injection.

Beyond styles and integrations, code injection allows you to add extra features to your Ghost site that enhance functionality and user experience. This could include custom JavaScript to create interactive elements, or add new widgets.

For example, you might want to add a custom JavaScript feature that opens all external links in a new tab. Here’s a simple example of how you could achieve this:

<script>
document.querySelectorAll('a').forEach(link => {
  if(link.hostname != window.location.hostname) 
    link.target = '_blank';
});
</script>

This script looks for all <a> elements checking for the hostname and if it's different from yours, it will add a target="_blank" attribute which in turn makes sure that when links are clicked they are opened in a new window.


In conclusion, the potential use cases for Ghost CMS Code Injection are wide-ranging. From aesthetic customization and third-party integrations to advanced dynamic content and interactive features, it's a powerful tool that can greatly enhance the functionality, appearance, and user experience of a Ghost CMS website.