WordPress.org

Ready to get started?Download WordPress

Forums

Custom metabox - strpos not finding the "-" character in the post title (6 posts)

  1. cavka
    Member
    Posted 1 year ago #

    I was coding a simple metabox for wordpress and have a little issue when saving data.

    The meta box is in my "create article"-page and has two textfields. These are saved as post-meta, when the post is saved.

    While saving I check if the fields were filled - if they are empty I take the post title and extract the data I need. The idea is to take everything that is before the first "-". If there is no minus sign, the whole title should be saved in my custom field. Now, this fails to find "-" in the title (alltough there is one) and returns the whole title every time:

    function get_from_title($title) {
      $pos = strpos($title, '-');
    
      if ($pos) {
        return trim(substr($title, $pos));
      }
      else {
        $pos = strpos($title, '–'); //added this since two different signs could be used
    
        if ($pos) {
          return trim(substr($title, $pos));
        }
        else {
          return $title;
        }
      }
    }

    the function that is calling get_from_title is getting the title via get_the_title( $post_id ) and this works without problems.

    Is wordpress encoding the title somehow? Why can't strpos find the minus sign? What should I look for instead?

    Thanks

  2. Spencer Cameron
    Member
    Posted 1 year ago #

    Ah yes. This is a tricky one. So, why can't strpos find a hyphen in the title when clearly we can see one? Because there isn't one. hehe.

    What WordPress is doing here is converting your hyphen ( minus sign ) into an en-dash.

    This will give you diddly-squat:
    $pos = strpos( $title, '-' );

    You want this:
    $pos = strpos( $title, '& #8211;' );

    I put a space between the & and #8211; so the browser wouldn't render it. Remove that space in your code and everything should work just fine.

    Let me know how things turn out. :)

  3. cavka
    Member
    Posted 1 year ago #

    No luck. It's still giving me the full title (I have a title with both en and em dash and tried & #8211; with and without &, # and ; ans also for 8212). Just in case, this is the rest of the relevant code:

    add_action('save_post', 'mytheme_save_data');
    // Save data from meta box
    function mytheme_save_data($post_id) {
        global $meta_box;
        // verify nonce
        if (!wp_verify_nonce($_POST['mytheme_meta_box_nonce'], basename(__FILE__))) {
            return $post_id;
        }
        // check autosave
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return $post_id;
        }
        // check permissions
        if ('page' == $_POST['post_type']) {
            if (!current_user_can('edit_page', $post_id)) {
                return $post_id;
            }
        } elseif (!current_user_can('edit_post', $post_id)) {
            return $post_id;
        }
        foreach ($meta_box['fields'] as $field) {
            $old = get_post_meta($post_id, $field['id'], true);
            $new = $_POST[$field['id']];
            if ($new && $new != $old) {
                update_post_meta($post_id, $field['id'], $new);
            } elseif ('' == $new) {
    	    update_post_meta($post_id, $field['id'], get_the_title( $post_id ));
            }
        }
    }
  4. Spencer Cameron
    Member
    Posted 1 year ago #

    Did you remove the space from in between the & and #8211?

    If yes, than would you mind copy pasting the code you tried as per my instructions? I'm confident we can get a resolution to this. :)

  5. cavka
    Member
    Posted 1 year ago #

    arrrrgh. that's my second stupid mistake today. I'm not even calling get_from_title()... *facepalm*

    turns out, my code works fine if I'm actually using it.

  6. Spencer Cameron
    Member
    Posted 1 year ago #

    Awesome. I'm glad to hear you've got it working. :)

Topic Closed

This topic has been closed to new replies.

About this Topic