<p>With your plugin you can check for a front-end request, and if so inject an <a href="https://docs.craftcms.com/v3/extend/asset-bundles.html" rel="nofollow noreferrer">asset bundle</a></p>

<pre><code>
public function init()
{
    // Check if front end request

    $request = Craft::$app-&gt;getRequest();
    if (
        $this-&gt;isInstalled
        &amp;&amp; !$request-&gt;isCpRequest
        &amp;&amp; !$request-&gt;isConsoleRequest
     ) {
        $this-&gt;registerAssetBundles();
    }
}

protected function registerAssetBundles()
{
    // Include CSS on front end
    $view = Craft::$app-&gt;getView();
    $view-&gt;registerAssetBundle('vendor\\plugin-handle\\assetbundles\\YourpluginFrontendAssets');
}

</code></pre>

<p>I'm not sure it's possible to inject html, I believe you would need to write a <a href="https://docs.craftcms.com/v3/extend/extending-twig.html#extend-the-global-craft-variable" rel="nofollow noreferrer">new variable</a> to render your html.</p>

CC BY-SA 4.0
2020-06-12T13:16:15.863Z
This worked like a charm. Thank you kind sir. :)
Small question, if I had to add attributes to my js and css files, how to do that in the asset bundle?
Rohan
CC BY-SA 4.0
2020-06-12T13:25:28.857Z
$jsOptions Figured it out :)
Rohan
CC BY-SA 4.0
2020-06-09T14:09:22.113Z
2020-06-09T14:15:36.493Z
2020-06-09T14:15:36.493Z
dmatthams
<p>You should be able to listen for the <a href="https://docs.craftcms.com/api/v3/craft-web-view.html#event-before-render-page-template" rel="nofollow noreferrer">before render event</a> on the view class.</p>

CC BY-SA 4.0
2020-06-08T21:38:00.440Z
2020-06-08T21:38:00.440Z
billythekid
<p>If you are able to place a hook tag in all templates then you could <a href="https://docs.craftcms.com/v3/extend/template-hooks.html#app" rel="nofollow noreferrer">register a new template hook method</a> in your plugin.</p>

<p>Twig:</p>

<pre><code>{% hook 'my-custom-hook-name' %}
</code></pre>

<p>Your plugin:</p>

<pre class="lang-php prettyprint-override"><code>Craft::$app-&gt;view-&gt;hook('my-custom-hook-name', function(array &amp;$context) {
    $context['foo'] = 'bar';
    return '&lt;p&gt;Hey!&lt;/p&gt;';
});
</code></pre>

<p>For injecting CSS or JS you would use </p>

<pre class="lang-php prettyprint-override"><code>\Craft::$app-&gt;getView()-&gt;registerCssFile("file.css");
\Craft::$app-&gt;getView()-&gt;registerJsFile("file.js");
</code></pre>

<p>If you can't modify the template files to place a hook tag there you could dynamically generate the markup in your JS file, depending on your use case.</p>

CC BY-SA 4.0
2020-06-08T18:22:10.100Z
2020-06-08T18:22:10.100Z
Johannes
<p>I am developing a plugin for Craft 3. And I am trying to add some content to all pages in the CMS using this plugin. How can I do that? Is there a method or class call I can use for this? I am not sure where this is mentioned in the docs either.</p>

<p>Basically I want to add a few lines of HTML, CSS and JS in all the pages of the CMS if my plugin is installed and enabled in that Craft installation. Any ideas, anyone?</p>

CC BY-SA 4.0
2020-06-08T18:22:17.100Z
Could you explain your use case for this? There could be a more suitable solution for this.
Johannes
<plugin-development><craft3><php>
How to add content to pages using a plugin?
# How to add content to pages using a plugin?

(Asked by: username_0 on 2020-06-08)

I am developing a plugin for Craft 3. And I am trying to add some content to all pages in the CMS using this plugin. How can I do that? Is there a method or class call I can use for this? I am not sure where this is mentioned in the docs either.

Basically I want to add a few lines of HTML, CSS and JS in all the pages of the CMS if my plugin is installed and enabled in that Craft installation. Any ideas, anyone?

**Tags:** plugin-development, craft3, php
> **Comments:**
> **username_1**: Could you explain your use case for this? There could be a more suitable solution for this.  

---

## 3 answers

### Answer by username_2 on 2020-06-09. Score: 1 | Accepted answer

With your plugin you can check for a front-end request, and if so inject an asset bundle

public function init()
{
    // Check if front end request

    $request = Craft::$app->getRequest();
    if (
        $this->isInstalled
        && !$request->isCpRequest
        && !$request->isConsoleRequest
     ) {
        $this->registerAssetBundles();
    }
}

protected function registerAssetBundles()
{
    // Include CSS on front end
    $view = Craft::$app->getView();
    $view->registerAssetBundle('vendor\\plugin-handle\\assetbundles\\YourpluginFrontendAssets');
}


I'm not sure it's possible to inject html, I believe you would need to write a new variable to render your html.

> **Comments:**
> **username_0**: This worked like a charm. Thank you kind sir. :) Small question, if I had to add attributes to my js and css files, how to do that in the asset bundle?  
> **username_0**: $jsOptions Figured it out :)  

---

### Answer by username_3 on 2020-06-08. Score: 1

You should be able to listen for the [before render event](https://docs.craftcms.com/api/v3/craft-web-view.html#event-before-render-page-template) on the view class.


---

### Answer by username_1 on 2020-06-08. Score: 1

If you are able to place a hook tag in all templates then you could register a new template hook method in your plugin.
Twig:
{% hook 'my-custom-hook-name' %}

Your plugin:
Craft::$app->view->hook('my-custom-hook-name', function(array &$context) {
    $context['foo'] = 'bar';
    return 'Hey!';
});

For injecting CSS or JS you would use 
\Craft::$app->getView()->registerCssFile("file.css");
\Craft::$app->getView()->registerJsFile("file.js");

If you can't modify the template files to place a hook tag there you could dynamically generate the markup in your JS file, depending on your use case.


---

