0

I am saving titles of posts in a separate table for a specific use. The titles are saved to the database when a post is published. code is:

add_action('future_to_publish', 'send_emails_on_new_event');
function send_emails_on_new_event($post)
    {
        global $post;
        global $wpdb;

        $table_name = 'tbl_name';   
        $site_post_url = get_permalink();
        $site_post_title = get_the_title();
        $author_id = $post->post_author;
        $author = get_the_author_meta( 'display_name', $author_id );    
        $wpdb->insert( $table_name, array(
                                                    'site_post_url' => $site_post_url,
                                                    'site_post_title' => $site_post_title,                                          
                                                    'Author' => $author                                         
                                                ) );

    }

The above code insert the data to proper fields in the table called 'tbl_name' all fine except site_post_title if the site_post_title has dash - it is automatically converted to hyphen code and saved.

Let's say my title is - Dash - test - -ok?

when I check the record in the table tbl_name it is - Dash – test – -ok?

tbl Why those two dashes are saved like that?

how can I save dashes as just dashes in the database table?

UPDATE

title number sign # double quotes " slash / colon : is saved as number sign # double quotes ” slash / colon :

  • A dash is a symbol. A symbol is (depending on your charset) defined as a Unicode. So what you see is a unicode of a symbol. – Interactive Nov 27 '15 at 12:20
  • my charset is UTF-8 and why only two dashes from this title are converted to unicode of a symbol ? how can I fix this? – Foolish Coder Nov 27 '15 at 12:23
  • why other symbols like : or / are NOT converted as unicode of a symbol – Foolish Coder Nov 27 '15 at 12:25
  • 2
    This is probably wptexturize() on the post title through the the_title filter from get_the_title(). To test, try e.g. $post->post_title instead – birgire Nov 27 '15 at 12:27
  • 1
    I don't understand the question as the title do not match the snippet. Is your problem is with inserting dash to the DB or having the title normalized? If the former it will be very surprising, with the later then the answer will probably be "ok, that is how it titles are designed to work". So what is exactly the question, is it to explain why it is removed or to look for a workaround? – Mark Kaplun Nov 27 '15 at 13:51
1

When we call get_the_title(), then the post title is taken through the wptexturize() function via the the_title filter:

add_filter( 'the_title', 'wptexturize' );

The en-dash and em-dash are replaced with

/* translators: en dash */
$en_dash = _x( '–', 'en dash' );
/* translators: em dash */
$em_dash = _x( '—', 'em dash' );

A simple workaround to keep the en/em dashes when you store it to your custom table, could be:

$site_post_title = str_replace( 
    [ '–', '—' ], 
    [ '–', '—' ],
    get_the_title()
);

or just use the raw $post->post_title if that's makes more sense for your setup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.