Part 1: Injections of one enzyme

Custom-field Execution Enzymes

The result of a custom-field execution enzyme is always the value returned after executing the value of the referred custom field.

Custom field execution allows you to add dynamic content to your blog, in a fashion very similar to what functions do in programming languages.


Injection of an author custom field execution enzymes. (a-meta-x)

{{[ @philomycus-carolinianus/author.num_posts() ]} (a-meta-x)

Execute the code into the ´num_posts´ custom field from the author of the post with the ´philomycus-carolinianus´ slug.
Then inject the returned value.

Notes

  • Origin: Author of the post with the ´philomycus-carolinianus´ slug.
  • Form: custom field. — The custom field is ´num_posts´.
  • Kind: execution. — The execution kind is always referred to by having ´()´.

The real custom field

We want to know how to count the number of posts by author in WordPress. The first Google result seems promising: Function Reference / count user posts. The usage example shows: ´$user_post_count = count_user_posts( $userid , $post_type );´. We put that into a ´num_posts´ custom field of the author of the post with the ´philomycus-carolinianus´ slug. Then we only need to find out the value of ´$userid´.

If we wanted to make this work ASAP, we could just hard code the ID of the user into that line of code, because we are adding the custom field to her profile, so that piece of data is available. However, if we wanted to add that same custom field to other users’ profiles, this hard coding method is too prone to error. A better solution is to let Enzymes figure out that piece of data.

When executing an enzyme, Nzymes put many useful values at our disposal. In this case we need ´$this->origin_post´, which is the post object with the ´philomycus-carolinianus´ slug. The author ID is into its post_author property. Here is how the custom field would appear.

NameValue
num_posts
$userid = $this->origin_post->post_author;
$post_type = 'post';
$user_post_count = count_user_posts( $userid, $post_type );
return $user_post_count;


Injection of a post custom field execution enzymes. (p-meta-x)

{{[ .num_words() ]} (p-meta-x)

Execute the code into the ´num_words´ custom field from the current post.
Then inject the returned value.

Notes

  • Origin: Current post.
  • Form: custom field. — The custom field is ´num_words´.
  • Kind: execution. — The execution kind is always referred to by having ´()´. The result of an execution is always the value returned after executing the referred value.

The real custom field

We want to know how to count the number of words in a WordPress post. Again , the first Google result seems promising: How To Display Word Count of WordPress Posts Without a Plugin. The usage example is harder to find because this is not reference material, but after a moment of reorientation we understand that we probably need: ´str_word_count(strip_tags($post->post_content));´. We put that into a ´num_words´ custom field of the current post. Then we only need to find out the value of ´$post->post_content´.

If we wanted to count the number of words in all the content, we could use ´$this->origin_post->post_content´. But if we wanted to count the number of words before the injection, how we’d do it? Again, let me help you here about what Nzymes offer. In this case we need ´$this->new_content´, which is all the filtered content before the current enzyme. Here is how the custom field would appear.

NameValue
num_words
$result = str_word_count(strip_tags($this->new_content));
return $result;


As you see, what you put in a custom field execution enzyme is all plain PHP code, which means very powerful code. You already know it:

WITH GREAT POWER THERE MUST ALSO COME
GREAT RESPONSIBILITY!

If you are concerned about security implications (you should), there is a section below that explains everything on the subject. Nzymes roles and permissions allow you to very finely tune what users can do.


Nzymes | WordPress Plugin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.