Ready to get started?Download WordPress


How Can One Parse The Post Contents For A Tag? (5 posts)

  1. saphod
    Posted 6 years ago #


    I am curious about how I can parse the contents of a WordPress post, replace a tag with code and then write the changed contents back into the post.

    My concern is that I would like to write a plugin that can embed a certain online service (viewat.org). Parameters would be width, height, an ID and (optinal) the language.

    The tag within the post could look somewhat like this:
    [embed_viewat width=600 height=400 id=1234 lang=de]

    What I need is to

    a) find this tag within the post,
    b) extract the correct parameter values,

    ... and here it becomes tricky:

    c) The way the .swf-files are used on that website is not XHTML valid. I want to use the KIMILI Flashembed plugin to produce valid SWF object embedding. This means that I want to replace MY tag with the KIMILI Flashembed tag syntax using the extracted parameters. But: this means I would have to put MY plugin before the KIMILI plugin is executed. How on earth do I do THAT?

    For finding the tag and extracting the values, I bet I will have to use regular expressions, right? No idea how... never really used them, although I looked up a few resources.

    What is better: ereg or preg?

    I took a look at the source code of wpvideo, and the preg used there seems VERY complex...

    So many questions...
    Help would be greatly appreciated!
    Thanks! :-)

  2. saphod
    Posted 6 years ago #

    I have a theory how to bypass that problem with calling my plugin before the KIMILI Flashembed: I could use a filter/hook that is used when one saves the post, meaning that I change it to the Kimili-call and write it directly into the post in the database.


  3. saphod
    Posted 6 years ago #

    I tried the following with RegExr:

    /\[embed_viewat (width=[0-9]+)|(height=[0-9]+)|(id=[0-9]+)|(lang=[a-z]+)\]/gi

    First of all, it does not find the whole string in between the whole "[embed_viewat....]" as match, but just e.g. "[embed_viewat width=200".

    Second of, if I change the position of the options (e.g. "height" before "width"), it does not find "width" any longer, but it finds height.

    "id" is supposed to be a must, all others should be optional.

    What do I have to do to recognize those parameters regardless of their position? What did I miss within the expression?

    Hm. Maybe it is easier to look for
    /\[embed_viewat.*\]/gi first, because that matches the whole string.

    Then, I could use a different RegEx on that match.

    OK, I am looking for something like

    a) "See if there is a [embed_viewat....] tag."
    b) "Get all parameters and put them in an array."
    c) "If the ID is missing, show an error message."
    d) "Otherwise, insert the [KML_flashembed...] tag using the parameters."
    e) Done.

    Help would still be appreciated.

  4. saphod
    Posted 6 years ago #

    Another question:

    When I use the "post_save" hook to pre-process the contents before it is written to the database, how do I write it back to the database? And how do I prevent this updating of the post to create a loop because it calls post_save itself again?

    post_save and the wp_post_update function create a loop?

  5. saphod
    Posted 6 years ago #

    I think I am gonna go with the "the_content" filter and embed the SWFObject JS myself instead of using Kimili.

    Another question:
    Let's say I want to "scan" the contents of a post after you hit "save" or "publish". I search for the use of a tag. If it is used in the wrong way, I wanna go back to "edit post" AND SHOW A FADE OUT MESSAGE, like all the plugins do... how would that be done?

    It is not an very urgent question, but it is of high interest to me.

    Maybe one could write a "syntax check" plugin that checks the correct use of other plugins, like "wpvideo" or "kimili" or whatever is used via [tag][/tag]...

    Just an idea.


Topic Closed

This topic has been closed to new replies.

About this Topic