• Resolved lw5

    (@lw5)


    Hi,

    (original post)

    I’m trying to add a base64 image to a WordPress page with wp_update_post().

    The string I’m trying to insert looks like:

    <img src=”data:image/jpeg;base64,….” alt=”foo” class=”bar”>

    Now wp_update_post seems to truncate the colon and anything before it until the double quote (ie: ‘date:’), resulting in:

    <img src=”image/jpeg;base64,….” alt=”foo” class=”bar”>

    Thus breaking my image. However, this does work if I manually update the page.

    I’ve found some people having similar problems with styles caused by KSES, but I can’t seem to figure out a solution for this specific issue.

    Does anyone have a suggestion?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    I’m not sure where the “data:” gets stripped out, I’m guessing WP sees this as a bad protocol, it is expecting “http:” or “https:”. If that is indeed the case, use the ‘kses_allowed_protocols’ filter to add 'data' to the passed array of protocols.

    If someone knows where “data:” is actually stripped out, I can determine if it is possible to make this work. Without specific code to work from I’m only guessing.

    If all else fails, defining a shortcode to wrap the img tag with should bypass the kses filters. Use the shortcode like this:
    [img64]<img src="data:image/jpeg;base64,...." alt="foo" class="bar">[/img64]

    The shortcode handler simply returns anything between the shortcode tags unchanged. Untested, but might be worth a try if a “real” solution remains elusive.

    One last wacky idea. Try replacing the colon with \x3a ? Also untested, but has a chance of working.

    Thread Starter lw5

    (@lw5)

    Thanks for your reply. I’m not sure what happened, but the problem disappeared after I made some unrelated adjustments to the code. In a way this is even more frustrating than the original problem, but I’ll have to mark this as resolved ??

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘wp_update_post and base64 images’ is closed to new replies.