• Does anyone know how to make code highlighting in WordPress keep the structure of a PHP code snippet in tact? Right now I can post PHP code (and any other code) and it will highlight fine, but WordPress always adds a space between the opening bracket (<) and the question mark(?) making the code snippet unusable without editing.

    I know this is more of a WordPress issue, but there seem to be no answers for this question (which has been posted for several different versions of WordPress). I can use the htmlentity for the tags, but they get left as their entity when highlighted. If I write them the way they are to appear, the get spaced.

    A demo can be seen here.

    I would really love to be able to post highlighted PHP code without users having to edit every single < ?php in the post.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    Look at the actual post in the database using phpMyAdmin or something. Does the post_content actually contain this space? We need to figure out if it happens pre or post saving of the post.

    its in the database – ive experienced a similar problem. My solution has aways been to just go in and edit it right there.

    Its only a mere minor inconvenience for me.

    Moderator Samuel Wood (Otto)

    (@otto42)

    www.remarpro.com Admin

    Maybe it’s the TinyMCE editor doing it then? Try turning off the rich text editor, see if that makes it stop.

    I dont use that, fwiw. Never have.

    Thread Starter everah

    (@everah)

    The entry in the database contains the space. Here is the entry taken straight from the DB:

    < ?php
    // this is code
    ?>

    <pre lang=”php”>
    < ?php
    // this is in the hightlighter
    ?>

    A sample of the output san be seen here: https://www.robert-gonzalez.com/2007/08/14/test-post/

    I removed the spaces in the DB to test what would happen if I removed them at the data level, and in the highlighter (wp-syntax) everything works fine, but in straight context the opening tag gets buggered because the greater than sign (tag opener) is interpreted by the browser as an HTML tag (naturally).

    After removing the spaces, I opened the post in the editor (I don’t use the tinyMCE editor) and the spaces were not there on fresh load. But as soon as I posted the post (using save or publish) the spaces reappeared, so I suspect that the spaces are being added somewhere in the save routine.

    Thread Starter everah

    (@everah)

    I think I found the problem…

    The balanceTags filtering function in wp-includes/formatting.php is called as a filter from wp_insert_post (wp-includes/post.php). This filtering functions attempts to balnace and straighten the markup that is posted in the content. The regular expression used in the balanceTags function is looking for all words within < and > symbols. As it does this, anything between the opening and closing tag is treated as a single HTML tag and filtered like a non-closing HTML tag (like a BR or HR)

    I changed the regular expression used in the balanceTags function from:
    "/<(\/?\w*)\s*([^>]*)>/"

    to:
    "/<(\/?[^?]\w*)\s*([^>]*)>/"

    And it seems to do what I want it to. Tests are still available at the link I posted previously.

    Something to note… the text strings “<?php” and “?>” seem to not be run through an entity-izer of any sort, so if you use them in plain text, the browser interprets the opening tag as an HTML tag and will not show anything that is on that line.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘WP-Syntax: Opening PHP tags’ is closed to new replies.