Content Include Hook

Starting from Plesk 12, in addition to the ability to include global.js into pages (described in Global JavaScript and CSS Files), there is also the option to generate on the server side an additional HTML or JavaScript code for embedding into a page. To do this, you need to implement a class derived from pm_Hook_ContentInclude. The class must be located in the directory plib/hooks/.

The login page in Plesk does not include additional scripts that are used on other Plesk pages. To form additional content on the login page, you need to use the class pm_Hook_LoginContentInclude.

There are several ways to embed JavaScript. One of the easiest ways is to pass data from the server to the client (browser) by using the method getJsConfig:

class Modules_EmbedJs_ContentInclude extends pm_Hook_ContentInclude
    {
     public function getJsConfig()
    {
        return [
            'dynamicVar' => date(DATE_ATOM),
        ];
    }
     ...
    }

The method must return an array which will be converted to JSON and embedded into a resulting HTML page, inside the HEAD tag.

<!-- extension include: embed-js -->
    <script>
    //<![CDATA[
        Jsw.namespace('PleskExt.EmbedJs');
    //]]>
    </script>
    <script>
    //<![CDATA[
        PleskExt.EmbedJs.Config = {"dynamicVar":"2014-02-25T16:56:46+07:00"};
    //]]>
    </script>

For access to the data formed on the server side, an extension called embed-js can use an object PleskExt.EmbedJs.Config. In addition to that, the namespace PleskExt.EmbedJs will be created.

There are also methods getJsContent and getJsOnReadyContent, which allow forming blocks of JavaScript code on the server side and adding it to the resulting HTML. The method getJsOnReadyContent executes JavaScript code after a DOM tree is fully loaded.

It is also possible to embed additional HTML content at the end of the HEAD and BODY tags. To do this, the methods getHeadContent and getBodyContent can be used. The following is an example of how to use these methods:

class Modules_EmbedJs_ContentInclude extends pm_Hook_ContentInclude
    {
        public function getHeadContent()
        {
            return '<!-- additional content for head tag -->';
        }
     
        public function getBodyContent()
        {
            return '<!-- additional content for body tag -->';
        }
    }

 You can find the sample code in our repository here.

 

Leave your comments on this page

Leave your feedback or question on this documentation topic below. For technical assistance, contact your hosting service provider or submit a request to Plesk support. Suggest new features for Plesk here. Discuss general questions on the Plesk forum. All offtopic comments will be removed.