AdSense Enzymes

AdSense Enzymes are very simple.

At the simplest level of abstraction, I can directly trasclude the custom field which I’ve stored the ad code into. And I can do it either by means of a statement into the content of a post or a page, or by means of a call to the metabolize function (available in Enzymes 1.1) into the php code of a WordPress template file.

The former method is useful when I want to place an ad unit in a particular/variable position inside the content of a post or a page; the latter method is useful when I want to place an ad unit in a general/constant position inside the blog.

For example, if I put the statement {[1.ad001]} here, it would reproduce by itself the ad unit right here, because in the first post I’ve stored the ad code in a custom field called ad001. But to make the ad unit appear before any post, I need to find the line in the index.php file of my default theme that reads

<?php if (have_posts()) : ?>

and replace it with this line

<?php metabolize( "{[1.ad001]}" ); if (have_posts()) : ?>

Taking the abstraction one step further, I’d like to store the string 1.ad001 in a new home field, so that I can provide a separation layer that makes it possible for me to replace the ad code simply by changing the content of a field, rather than having to change the php file again.

Indirect transclusion is not directly available in Enzymes, but it can be easily achieved by means of a simple enzyme like this

preg_match( '/'.$this->e['substrate'].'/', $this->substrate, $matches );
return $this->item( $matches['sub_id'], $matches['sub_key'] );

I’ve called this enzyme get, and I’ve put it into the first post. So the Enzymes statement becomes {[1.get(1.home)]} and the edited line for the index.php file becomes

<?php metabolize( "{[1.get(1.home)]}" ); if (have_posts()) : ?>

I’m currently using the latter for my blog home, so I don’t have to worry about placing ads every time I post a new log, and the former for my pages, so that I can place the ads insdide the content, in a position that I hope will fit better.

Why does Google limit to three the number of ad units per page? Is it a technical reason?

Enzymes 1.1 Released Today – updated

Changes

  • the title and the excerpt are now directly supported, together with the content

  • any location of a WordPress theme is now supported

    EXAMPLE

    • open the php file you are interested in (eg: sidebar.php)

    • select the location where you want the enzyme’s result to appear (eg: before the last closing ‘div’)

    • paste there a code like this

      <?php metabolize( "{[456.say(123.hello)]}" ); ?>

    • save and test

  • generic custom field keys are now supported. Just wrap your international, multiword key in a pair of ‘=’ and you are done (any ‘=’ your key may include must be escaped by single quotes)

    EXAMPLE

    • write it like this: {[.=水=]}

    • to see it like this: 水 is read MIZU and means WATER

  • no bug fixes, hence no need to upgrade, except for getting the new features

  • backward compatibility preserved, hence no reason not to upgrade 🙂

Entity enzyme, or The pacman effect strikes back

This is so much a basic enzyme, as it is also necessary for what it does.

If you want to represent an HTML entity in a post, you’re going to struggle with the escaping / unescaping issue.

For example, to write that an ampersand is represented by an ampersand followed by ‘amp;’ is not as visually appealing as it is to show it right away, like & = &amp;

You begin this adventure by writing & = &amp; and after a Save and Continue Editing (SCE) roundtrip you get & = &, which is certainly true but not what you meant.

You think, well it’s obvious, I should have used that HTML trick in the first place. Do you remember that trick, don’t you? You have to escape the ampersand twice: it works in a plain HTML file. You think that the WordPress editor should have done it by itself: it’s simple enough! But you want things done, you’re not interested in polemical discussions with your beloved blogging tool. So you write & = &amp;amp; and go for another SCE roundtrip and & = &amp; shows up.

Wow! You’ve done it, pal. Time for a reward. Go for a coffee and back soon. You’re inspired now, not a good idea to waste too much time.

Just before the last molecules of coffee disappear from your tongue, you want to check that all is OK for a Publish trip, and go back trough your post. Suddenly you see something that makes you feel guilty for a simple coffee, whose taste was still there, just because. You saw it right, it was & = &amp; but now it is & = & again!! How can it be?

Soon you’ll discover that that old trick is useless. It only delayed the problem twice. Yes, each time you Save your post, an &amp; of your escaped entity gets unescaped automatically, that is & eats amp; up. So &amp;amp; will be &amp; after the first new Save, and & after just the second one. Thus, & = pacman.

Well, you only lose a couple of battles. You recently installed that new plugin that promised miracles about transclusions, don’t you? Yeah, Enzymes was its name.

OK, lets add a custom field called ‘amp’ with &amp; as its value. Then write & = {[.amp]} and save and continue editing. Try it now, you’ll see that this solves the issue.

Done? Isn’t it wonderful?

What? What are you saying? It doen’t work?!?! My Enzymes is BS? Well, I’m sorry. I put many hours into it, and it’s a bit ungrateful on your side, considering that I didn’t charge you a dime!

Are you sure you did exactly what I said? Please check again. Have you found the problem? Yes, that is the problem. The pacman effect strikes back! The & in your ‘amp’ custom field ate its ‘amp;’ up.

Eh man, stop joking now! Tell me the solution please, I’m exhausted!

The solution is:

add a custom field called ‘entity’ with ‘echo htmlentities( htmlentities( $this->substrate ) );’ as its value (without quotes). Then write & = {[.entity(.amp)]}

Remember this post pal! You’re going to use it over and over: {[123.entity(123.amp)]}, if the ID of your post is 123.