Site logo

Archived topic

Sections editor removing backslashes in content

16 replies · Started by kleintk on September 19, 2018

Viewing posts 1–15 of 17

Hi,

I'm currently using GP Premium 1.7.2 and GeneratePress 2.1.4 utilizing the built-in Sections editor. My goal is to use the Preformatted formatting to display some example code. However, as soon as I click on "Update" or "Save Draft", all backslashes are removed from my example code.
E.g.
value = b'\x0f\xfa\x34\x01' # address
value += b'\x20\x29\x00\x44' # port
Becomes:
value = b'x0fxfax34x01' # address
value += b'x20x29x00x44' # port

In the Paragraph formatting, additionally apostrophes are reformatted by this process. The first ' becomes a ’ and the second ' becomes a ′.
E.g.
value = b'\x0f\xfa\x34\x01' # address
value += b'\x20\x29\x00\x44' # port
Becomes:
value = b’x0fxfax34x01′ # address
value += b’x20x29x00x44′ # port

While the latter issue is just mildly annoying, the issue with the removed backlashes is more of a problem.
The first problem only appears in the Sections Editor, it also doesn't matter whether I use Visual or Text input option. If I don't use sections, but the default editor, everything seems to work fine.
The issue with the apostrophes also appears in the default editor.

Anyone knows a solution or workaround (ideally not involving another plugin) (at least for the backslash problem)?

Thanks!

Hi there,

Very strange - I assume you're wrapping your code in <pre> or <code> tags?

Hi Tom,

yes, I use <pre> for the highlighting. I just checked, the backslashes also get removed when using <code>.
On a second host where I installed GP Premium and GeneratePress it is the same issue. Happens also with all plugins disabled except for GP.

Hmm, not sure what's going on here.

If you're willing to test something, you can try opening:

wp-content/plugins/gp-premium/sections/functions/metaboxes/metabox-functions.php

Then find this line:

$section['content'] = sanitize_post_field( 'post_content', $data['content'], $post_id, 'db' );

And replace it with:

$section['content'] = $data['content'];

Does it fix the issue when you save?

Sadly that didn't fix the issue.
I can send (how?) you the credentials to a test instance if necessary.
Escaping with \\ doesn't work either, as in it works for the first time saving as there is only the first \ removed, but after the second time saving the second \ is also removed.

Interesting, so the backslashes aren't being stripped on save. Just to confirm, they are being stripped in the Dashboard as well? Or only on the front end?

It has several levels of behaviour.
I write them in the sections editor and close it with "Apply":

if I click on "Preview":
* backslashes are stripped from the frontend in the now opening preview tab
* however, backslashes are still there when I open the section again in the editor

if I click on "Update" (or "Publish" at a new post):
* backslashes are stripped from the frontend
* backslashes also stripped from the editor when I open the section again (I guess thats what you mean with Dashboard?)

Hello, the same thing happens to me.

In wordpress html/text editor, I write

<code>
<div>Hola mundo </div>
</code>

When I switch to the visual editor and go back to the html/txt editor:

<code></code>
<div>

Hola mundo

</div>

Weird, that change I mentioned should save the content to the database as-is - maybe there's something built into WordPress that strips backslashes. I'll have to dig into the core WP code to see if they do anything fancy with the regular editor to preserve them. I'm away from the office but will be heading back tomorrow.

David - that sounds like a different issue. Any chance you can open a new topic?

Thanks! :)

Just went deep into WP looking at what's going on.

The reason this happens in Sections and not the post content is WordPress strips them in the update_metadata() function: https://wordpress.stackexchange.com/a/53339/90661

The easiest way to handle this is to add double backslashes to your code. It's not elegant, and it would have to be done before each save, which is a pain.

Another option is to run your Section content through wp_slash(), but unfortunately we don't have a filter within the save function to do this so it's update safe. You'd have to make the change to the core file after each plugin update. Let me know if you want to go this route.

Hi Tom,

thank you for going down the rabbit hole. Is there a way that might be implemented in a future version of Sections?
Adding a double backslash would be a possible solution for posts created and seldom edited. I guess I will go that way for the "stylish" posts with page-wide rows where I use Sections for.

For the normal posts / pages with code examples I'll probably default back to the normal Wordpress editor then. Or do you know a lightweight alternative? Maybe the new Gutenberg?

Thank you :)

I'll definitely look at adding a filter, but it may not be possible due to security reasons.

The code you can add to GPP is pretty simple if you want to try, it would just need to be repeated after updates (until I can figure out a safe filter).

Yes I'd like to try that.
How often do those kind of updates happen? (GP updates or WP updates?)

GP Premium updates would overwrite this change, so you'd need to reapply it after those updates (if we can't add a filter).

To make the change, open this file:

wp-content/plugins/gp-premium/sections/functions/metaboxes/metabox-functions.php

Find this line:

$section = json_decode( stripslashes( trim($section) ), true);

And add this on the line below it:

$section = wp_slash( $section );

This archived topic is closed to new replies.