• I am trying to add (programmatically) a custom meta tag to a post which contains new lines.

    Following the advice on https://codex.www.ads-software.com/Function_Reference/update_post_meta#Character_Escaping I tried the following:

    <?php
    $escaped_meta = "field1\\r\\nfield2\\r\\nfield3";
    update_post_meta( $id, 'double_escaped_json', wp_slash( $escaped_meta ) );
    ?>

    This stored the \\r\\n in the database. Removing the double slash in the $escaped_meta variable leads to \r\n being stored literally in the database (so it is being sent as \\r\\n). Removing the wp_slash() leads to rn being stored in the database with no escaping.

    Is there a reliable doing this such that the actual data sent to the database is: UPDATE postmeta SET value='field1\r\nfield2\r\nfield3' WHERE id='$id'?

    • This topic was modified 6 years, 9 months ago by yknivag. Reason: Clarity
    • This topic was modified 6 years, 9 months ago by yknivag.
Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    You want backslashes to be handled as a string literal instead of an escape character? First use single quotes to assign your string to $escaped_meta. That takes care of PHP, but SQL still thinks you are sending escape characters. Thus you need to double escape the backslash so it resolves down to a single backslash. Do not use wp_slash() or addslashes() or otherwise further escape the string.

    $escaped_meta = 'field1\\\r\\\nfield2\\\r\\\nfield3';
    update_post_meta( $id, 'double_escaped_json', $escaped_meta );

    If you need to sanitize the string for security, sanitize the individual elements, then assemble into a single string, perhaps with implode('\\\r\\\n', $elements ); or similar.

    Thread Starter yknivag

    (@yknivag)

    Hi @bcworkz and thank you for your reply. I tried triple slashes as you suggested but to no avail. Here are my results from this morning’s testing.

    <?php
    $test3slashes = $field1 . '\\\r\\\n' . $field2 . '\\\r\\\n' . $field3;
    add_metadata ( 'post', $id, 'test_3s', $test3slashes );
    
    //Caused field1\\r\\nfield2\\r\\nfield3 to be written to the database
    
    $test1slash = $field1 . '\r\n' . $field2 . '\r\n' . $field3;
    add_metadata ( 'post', $id, 'test_1s', $test1slash );
    
    //Caused field1rnfield2rnfield3 to be written to the database
    
    $test1slash_wp = wpslash ( $field1 . '\r\n' . $field2 . '\r\n' . $field3 );
    add_metadata ( 'post', $id, 'test_1s_wp', $test1slash_wp );
    
    //Caused field1\\r\\nfield2\\r\\nfield3 to be written to the database
    
    $test2slashes = $field1 . '\\r\\n' . $field2 . '\\r\\n' . $field3;
    add_metadata ( 'post', $id, 'test_2s', $test2slashes );
    
    //Caused field1rnfield2rnfield3 to be written to the database
    
    $test2slashes_wp = wpslash ( $field1 . '\\r\\n' . $field2 . '\\r\\n' . $field3 );
    add_metadata ( 'post', $id, 'test_2s_wp', $test2slashes_wp );
    
    //Caused field1\\r\\nfield2\\r\\nfield3 to be written to the database
    
    ?>

    It must be possible to write \r\n into the database as WP Core does this when creating enclosure tags. Maybe I will take a look at how the do_enclosure writes it’s metadata for an idea.

    Has anyone else seen this behaviour and got round it?

    Thread Starter yknivag

    (@yknivag)

    Line 623 of the core functions.php file (https://core.trac.www.ads-software.com/browser/tags/4.8/src/wp-includes/functions.php#L623) seems to suggest that:

    add_post_meta( $post_ID, 'test', "$field1\n$field2\n$field3\n" );

    may be the answer. I’ll try it and see what happens.

    Moderator bcworkz

    (@bcworkz)

    That line 623 stores an actual newline char between field values. If that’s what you want, then that’s what to do. The double quotes are significant. I thought you wanted an actual backslash and ‘n’ char stored. (the \r\n is Windows style and \n is ‘nix style in case anyone is wondering)

    If line 623 method does not work for you, what is it you need to do with the saved value? That may inform the proper assignment/storage method.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom post meta with new lines’ is closed to new replies.