Importing XML deletes reference to PHP (16 posts)

  1. wasana
    Posted 3 years ago #

    My page/post content includes things like <?php the_title(); ?> (I have php execution plugin) -- in the past I had no trouble importing WP WXR XML files with this in the content.

    on a brand new installation the import is deleting all references to <?php blahblahblah ?>, it is deleted from the text.

    I tested on a manual post and there is no problem with it.

    Any advice on where there might be a setting or solution to deleting/editing the import?


  2. Birgir Erlendsson (birgire)
    Posted 3 years ago #

    The importer script is using wp_insert_post


    and it is using sanitize_post to do some filtering.

    Here are some ideas:

    Mabye you could replace

    <?php with fx ((?php

    and ?> with ?))

    in the WXR XML import file (fx in your editor) and then after the import you could replace it back in fx phpmyadmin.

    Another method is to replace

    <?php with fx [php]

    and ?> with [/php]

    and write a shortcode function that can be used like this in the wordpress editor:


    which would give you the ouput <?php the_title();?>.

    Third method would be to try to change the post_content_filtered.

    Fourth method (last resort) is to modify the importer script:


    ps: Just remember to have backup of everything before trying!

    Hope this helps.

  3. wasana
    Posted 3 years ago #

    Thank you for the comprehensive reply.

    What a pain... it used to work with no hassle...

    1) find and replace - too many posts, trickled in over time
    2) [php] shortcodes - probably the best option, but ticks me off to have to do this - and I'm not entirely sure how
    3) post_content_filtered - no clue about what that means or how to do it - and need a permanent fix for all types of posting (autoblogs etc)
    4) importer script, same as 3

    thanks -- but I'm still a bit lost --

  4. wasana
    Posted 3 years ago #

    so in my content I want to replace <?php with [redact] and ?> with [/redact]

    so I came up with this edit for my functions.php:

    function redacted() {
        return '<?php';
    add_shortcode('redact', 'redacted');
    function slashredacted() {
        return '?>';
    add_shortcode('/redact', 'slashredacted');

    but that is causing errors --

    Warning: preg_replace_callback() [function.preg-replace-callback]: Unknown modifier 'r' in/home/tvi1/public_html/wp-includes/shortcodes.php on line 151

    any idea what I am doing wrong... ??

  5. Birgir Erlendsson (birgire)
    Posted 3 years ago #

    Here is a shortcode function example

    function my_redact_shortcode( $atts, $content = null ) {
       return '<?php ' . $content . ' ?>';
    add_shortcode( 'redact', 'my_redact_shortcode' );

    (I'm using the html special char code for < and > but it is not showing in this forum post) so when you write


    in the WordPress editor it will display

    <?php the_title(); ?>

    on the blog-page

    So you will have to open the wxr xml file and do "replace all" in your editor:

    <?php => [redact]
    ?> => [/redact]

    before importing but watch out for the first line, it contains ?>:

    <?xml version="1.0" encoding="UTF-8"?>

  6. wasana
    Posted 3 years ago #

    Thanks for another helpful response, and yes I did replace the first line ?> ha ha...

    however it still isn't working...

    • [redact] bloginfo('name'); [/redact] displays in the WP page editor, but is blank in the browser
    • if I remove [redact] [/redact] then bloginfo('name'); does display as text
    • other shortcodes work
    • <?php blahblah ?> works when entered manually

    any ideas? thanks

  7. Birgir Erlendsson (birgire)
    Posted 3 years ago #

    you must use the html version of the less-than (<) and greater-than (>) symbols, in the shortcode function.

    you can fx find them here:


    (I used them in the example above, but it doesn't show in the forum post)

  8. Birgir Erlendsson (birgire)
    Posted 3 years ago #

    by using the shortcode method, you can then play with the formatting, for example:

    function my_redact_shortcode( $atts, $content = null ) {
       return '<strong><?php</strong> <pre>' . $content . '</pre> <strong>?></strong>';
    add_shortcode( 'redact', 'my_redact_shortcode' );

    and so on.

  9. Birgir Erlendsson (birgire)
    Posted 3 years ago #

    or perhaps

    function my_redact_shortcode( $atts, $content = null ) {
       return '<pre class="redact"><?php' . $content . '?></pre>';
    add_shortcode( 'redact', 'my_redact_shortcode' );

    and then style it in your style.css with the line:


  10. wasana
    Posted 3 years ago #

    thank you for the great info, but while we're closer we're not there yet.

    the [redact] is converting to <?php in the browser, but it isn't triggering the php call.

    I used both < and <; and to test the formatting I copied the browser text and pasted in WP edit and updated the page and it did work to call the php and generate the blog name (using bloginfo('name'); as an example).

    so maybe there seems to be something about the timing of the php calls - one shortcode to generate the <?php but too late to also make that trigger the call to pull up the blogname.

    Your advice has been great, hope we can clear this last hurdle...

  11. Birgir Erlendsson (birgire)
    Posted 3 years ago #

    aha, so you want to evaluate the php code inside a blog post content?

    I thought you just wanted to show the code ;-)

    Here is a basic version of a shortcode function to evaluate the php code inside the blog post content:

    function my_redact_shortcode( $atts, $content = null ) {
            $text = ob_get_contents();
            return $text;
    add_shortcode( 'redact', 'my_redact_shortcode' );

    so this code inside the post content is beeing evaluated:

    [redact]echo time();[/redact]
    [redact] $a=10;$a++;echo $a; [/redact]

    ps: you should be very careful using this and running php code from inside the post content, fx. it is not recommended if you have other users.

  12. wasana
    Posted 3 years ago #

    Thank You !

    we're good to go

    Also thanks for the reminder/advisory about enabling PHP execution in pages/posts as being very dangerous, and only suitable for blogs with controlled set of users.

    Very much appreciate the help

  13. wasana
    Posted 3 years ago #

    well... I think I spoke too soon...

    I'm getting php errors and some shortcodes are now not working...

    I think I'm going to have to abandon the shortcode workaround...

    If you can offer a suggestion on how to correct/edit the importer filtering I will truly be indebted to you, thanks

  14. Birgir Erlendsson (birgire)
    Posted 3 years ago #

    ok strange, no errors on my WP 3.4.1 install.

    Maybe this plugin will work for you (it uses [php] instead of [redact]):


    It works on my WP install, fx.

    [php] echo get_bloginfo("url"); [/php]

  15. wasana
    Posted 3 years ago #

    birgire: thanks for help and advice.

    I have started a new post import-is-stripping-tags-i-need-howwhat-to-edit-to-filtersanitizing?replies=1#post-3063998 to approach this from the start of the problem and figure out how to edit wp_insert_post to stop stripping the php tags.

  16. wasana
    Posted 3 years ago #

    I have no idea what is going to happen, but in wp-includes/post.php I commented out

    //$postarr = sanitize_post($postarr, 'db');

    and the import is now working... yeah!

    If anyone knows what risks are associated with this I would appreciate hearing back.

Topic Closed

This topic has been closed to new replies.

About this Topic


No tags yet.