-
☆ A M B ☆
- 1,231 Posts
Hey folks,
I was wondering how I can do the following...
I would like to create a plugin that will input a piece of html code in to a specific area on a page without the need to code it in to the template. For example, I would like to put a piece of code in to the footer of my page, just before the </body> tag no matter what is there in the template. Another example of what I mean is the Google Analytic Plugin that puts the code in each page just before the </body>.
How can I put a piece of code just before the </body> or in the <head> using a plugin?
-
☆ A M B ☆
- 1,231 Posts
Thanks a lot! I couldn’t find that when I searched.
-
☆ A M B ☆
- 1,231 Posts
Back to this...
None of the examples seem to work.
Example 1
$src = "assets/js/prototype.js";
$modx->regClientStartupScript($src);
This will produce this line of code in the head of the document:
<script type="text/javascript" url="assets/js/prototype.js"></script>
[edit]
Example 2
You can also pass a complete block of javascript code:
$src2 = "<script type=’text/javascript’>
function getHTML()
{
var url = ’testing.php’;
var pars = ’return=test’;
var myAjax = new Ajax.Updater( {success: ’placeholder’}, url, {method: ’get’, parameters: pars});
}
</script>";
$modx->regClientStartupScript($src2);
I’m using 9.6.3 and nothing is showing up in the source code of the site. I tried a couple system events as well to see if that helped and it didn’t work. Any idea why?
EDIT * OnLoadWebDocument seems to make it work.
-
☆ A M B ☆
- 1,231 Posts
I checked the API docs for inserting code in to elements of a page but I couldn’t find if I can do the following. I want to put a piece of text on to a page but I want to choose where it goes exactly. How could I put the echo "mytag"; value in to say a div id= or do I create a placeholder like [+mytag+]? I want to be able to echo a value in the document.body but in a predefined place.
If your plugin has access to the page content just before it’s rendered, you should be able to just use your own custom placeholder (e.g. ###my_placeholder### )and replace it with str_replace() in the plugin. The placeholder could go at the top of the footer chunk.
You might also be able to use $modx->setPlaceholder() with a standard MODx placeholder if your plugin executes early enough in the sequence to fire before the placeholders are processed.
-
☆ A M B ☆
- 24,524 Posts
Your plugin would insert the content in the documentOutput using the onWebPagePrerender event; this will give you the whole HTML of the finished page to scan for your specific location. You wouldn’t need a placeholder if you have a specific HTML tag in the template where you want to insert your content.