0

I'm implementing an inventory updater on a Wordpress product site that integrates with a 3rd-party website via their custom api. We're going to have a number of people updating the 3rd-party website, and I need to set up automatic updating on our product site so that changes made to the inventory on the 3rd-party website are reflected on our product site.

I mostly have everything working like a charm (cron job and such), but I'm having some difficulty with updating the post_title. Wordpress is sanitizing the post_title when a given post (product post type) is saved. I want to be able to update the name of a product on the 3rd-party website and then save that new value to our product site automatically. I'm using wp_update_post to accomplish this:

$updatedPostId= wp_update_post(
    array(
        'ID' => $thisId,
        'post_title' => $newTitle
    )
);

My problem comes in where I don't want to be re-saving the post_title to every single product post if I don't need to (currently, this more than doubles the runtime of my cron job). So, I'm trying to compare the $newTitle to the current title within Wordpress. I'm doing this using the php function "strcmp":

if(
    ($newTitle!='')&&
    (strcmp($newTitle, get_the_title($thisId))!=0)
)

If this statement evaluates as true, then I call wp_update_post that I showed above. My problem is that the value of the product name from the 3rd-party website isn't evaluating as being equal to the Wordpress product site post_title for a number of products (have about 2000 products on the site and this check is failing for about 100 of them). I've looked at the titles themselves and they look identical in most cases:

Eg:
    From 3rd party:  Arnold Iron Whey [2 LBS] [Cookies & Cream]
    From Wordpress:  Arnold Iron Whey [2 LBS] [Cookies & Cream]

(Note: My text editor highlights the ampersand and the space immediately after it
in the 3rd-party product name but not in the one from Wordpress) 

strcmp is not evaluating to zero though, so I know that there is something different between them that only the computer can understand. Too bad I can't read invisible characters.

So, I need to "sanitize" the product name from the 3rd-party website such that it'll evaluate as identical to the already sanitized Wordpress post_title when passed through strcmp. What do I need to do to accomplish this? I found one option (sanitize_text_field) that got rid of about 20 of these false positives, but I've been working at this now for about half a day, and I'm not getting any closer to an answer that works yet.

Right now, these ~100 products are having their post_titles saved over and over again with each run of my cron_job, which I'd much rather just avoid. If at all possible.

1
  • I've found that filtering the post_title for the db instead of just using the default [ sanitize_post_field('post_title', $newTitle, $thisId, 'db') ] gets me down to about 50 products giving me a false positive. One step closer... – Programmer Dan Mar 10 '15 at 14:37
0

Well, I never got any input on this and ended up going with the function sanitize_title which is more restrictive than I want (processes the title to be used in a URL -- essentially returns a slug for the post), but it's the best solution I've been able to find. By running both the 3rd-party product name and the post_title from Wordpress through sanitize_title and then comparing them with strcmp, I've gotten close enough to being able to compare the two strings effectively. Granted, this may miss small differences, like changes in punctuation, but it'll get the important differences (textual) while not reuiring that I continually re-save these names unnecessarily.

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.