Embedder Support Forum

92 Comments in 29 Topics
Author Topic: PHP support 6 comments / 6685 views
Martin

hi,

i really like your embedder plugin but would really appreciate the support of php code in the snippets. is there any way to include this feature? if no, are there alternative plugins which can handle this?

greetings, martin

moztools

Hey Martin. The main thrust of the plugin is embedding HTML, though I understand that some people would like to have direct embedding of PHP code supported too.

That’s why Embedder does actually support running any PHP code you like in an embed, by specifying a “User Function” in the settings of a global embed (see here for the details).

You do have to put the PHP function somewhere in your code base (the theme’s functions.php file or in a separate plugin — I prefer a plugin since you don’t have any work to do if you change themes), but that shouldn’t take more than a couple of minutes to set up.

The user function’s parameters give you access to the embed’s name (which means you can use the same function with multiple embeds as in the example on the referenced page), the attributes defined in the embed and their values, along with the embed template (value) itself, if it has one, and the content between the begin and end tags — in other words, everything you need.

All your code has to do is return the output you want the embed to display. It might not be quite as convenient as putting the PHP directly in the embed’s value, but it works just the same. This is exactly how I created the page links at the top and bottom of each page in this section, and the index of pages on the Embedder’s home page.

Let me know if you need any help with setting up a user function with an embed, and I would be interested to know if you think this type of PHP support meets your needs (and if not, why not).

As for other plugins, well, there is Sniplets, but I believe it might have a few issues with WP 3.0. That’s all I know of.

Cheers,

Mike

Martin

hi mike,

thanks for your explanation. i havent seen before that there is a method like you described.
i tried it out and – yes – it seems like it fits my needs quite well..

the only thing i was encountering:

when i put the following into the content of an article >

[my-embed]
text text text
text text text
[/my-embed]

i always have the problem, that linebreaks and paragraphs after the beginning tag and before the end tag are added to the final output. is there a way to avoid this?

i dont want to put in the following >
[my-embed]text text text[/my-embed]

because it doesnt look that good..

greetings, martin

moztools

Hi Martin. It sounds to me like WordPress is doing its usual trick of adding extra paragraphs to the output (There are internal functions like “wpautop” that do that, and it can be a real pain if you don’t really want it.)

I’ll take a look to confirm if this is the case, and have a think about how you might avoid the problem, and if there is anything the Embedder can do about it. I’ll post another reply here later.

Mike

moztools

I just finished playing with a couple of examples, and I can confirm that the problem is WordPress’s automatic formatting functions that add extra line-spacing and paragraphs when generating the output.

The formatting is already done before your embed user function is called (and before Embedder sees it too) so there isn’t really anything the plugin can do to stop it from happening. (And in most cases, it’s what people expect to happen, so automatically removing the formatting would not be a good thing anyway.)

The formatting rules following by the “wpautop” function that does all this is when it sees a single line break it inserts a <br /> tag, and when it sees a double line break, it adds the </p><p> tags. In the WordPress post editor, in Visual Mode, hitting the Enter key gives you a double line break (i.e. a new paragraph) and hitting Shift-Enter gives you a single line break. In HTML Mode if you hit the Enter key once you get a line break, hit it twice you get a new paragraph.

Now, there are a couple of things you can do about thi. The first one is the easiest, but does mess up your formatting a little. Instead of:

[my-embed]
text text text
text text text
[/my-embed]

You could do:

[my-embed]text text text
text text text[/my-embed]

(assuming you want that line break in the middle of the “text” content.)

If that’s not good enough, then you have to strip the tags you don’t want from the output between the tags in your embed function. There are a different ways to do this depending on what you want:

If you want to strip all the formatting from the content you can do this:

function my_em($name, $value, $attrs, $content) {
$content = str_replace(array('<br />', '<p>', '</p>'), array('', '', ''), $content);
return $content; // Note you have to return the modified content for this to work.
}

If you only want to strip the first “p” or “br” and the last “p” or “br” you have to use a regular expression:

function my_em($name, $value, $attrs, $content) {
$content = preg_replace('/^s*<brs*/>|^s*</p>|<brs*/>s*$|<p>s*$/', '', $content);
return $content; // Note you have to return the modified content for this to work.
}

(Regular expressions are complicated mess, but I have tested the one above and it does work. It looks for “br” and “p” tags only at the beginning (^) or end ($) of the content and replaces them with an empty string, and leave all the other line breaks and paragraphs alone.)

Please note, that if your embed has a value (template), like this:

<b>%content%</b>

You will have to insert the stripped content (in the $content parameter) into the template yourself and return the whole thing from your function, otherwise the embed will just take the original content. You can do that like this:

return str_replace('%content%', $content, $value);

That’s the best I can offer. Your function has to do the work to strip out those extra formatting tags, I’m afraid. The only other alternative would be to turn off “wpautop” altogether (just Google for “wpautop” to see how you can do that) but you would then have to add all the line breaks and paragraphs manually, which I doubt is what you want.

Let me know how you get on.

Mike

Martin

hi,

thanks for your explanation!
right now i managed the problem in the way that i put the following code into the content.

[my-special-div]
text text text
text text text
[end-div]

this works in my case quite well. when i encounter the need of wrapping content into a start and end tag i will try your solution. i just read about the new parser from version 1.3 – does it change anything to this odd behaviour from wpautop?

thanks for your help!

Powered by moztools Instant Forums

Leave a Reply

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