0

Spent quite a while trying to figure this out. I've implemented a stop-gap function that mimics what I believe this function is supposed to be doing, but I would like to use the native libs/functions where possible, so, you know, what the heck?

<?php

phpinfo();

require_once("/data/[ignore]/[me]/wordpress-4.7.5/wp-includes/formatting.php");
require_once("/data/[ignore]/[me]/wordpress-4.7.5//wordpress-4.7.5/wp-includes/plugin.php");

$unsanitized_string = '123 qwe ASD';
$sanitized_string = sanitize_title($unsanitized_string);
echo $sanitized_string;

?>

From the documentation here I would expect the code above to return something like:

123-qwe-asd

rather, it returns:

123 qwe ASD

Also, from the code enter link description here, I see that it is doing something with filters. I'm wondering if its something about the filters that I am neglecting to consider.

Given that sanitize_title() has been around since version 1.0 or so, I find it unlikely that something so seemingly integral would be broken. Any thoughts?

I'm relatively new to PHP and Wordpress, so I'm sure you all are going to have some questions as to what my environment looks like. Rather than just dumping all of phpinfo() into this post (seems insecure), let me if you need to see any settings and I'll provide.

PHP Version => 7.0.19-1+deb.sury.org~xenial+2
0

As you can see in the source code of sanitize_title(), all the function does is 3 things

  1. apply the function remove_accents(), supposedly to remove accents
  2. apply the filter sanitize_title
  3. return either $title or $fallback_title

Since you're loading only some partial WordPress files, you'll probably be missing all the goodies of the filter.

I dug a bit through the source code, and it looks like sanitize_title_with_dashes() is doing most of the work. The function resides within /wp-includes/formatting.php as well, so in your case I'd just use this one in conjunction with remove_accents() like so

$sanitized_string = sanitize_title_with_dashes( remove_accents( $unsanitized_string ) );
  • follow up question is too long to post. Removed. – zentechinc Aug 3 '17 at 15:46
  • @zentechinc Have you tried a current WP version (4.8.1) and checked the file if the function actually is in it? – kero Aug 3 '17 at 15:48
  • Sure, I tried the sanitize_title_with_dashes last night, got errors regarding it's inability to find some dependencies: undefined constant ABSPATH and constant WPINC seems like its looking for those to load option.php. even if i explicitly declare option.php, calling sanitize_title_with_dashes requires functions.php and functions.php requires option.php according to abspath and wpinc. why didnt i have to have those constants defined for the other requires? – zentechinc Aug 3 '17 at 15:51
  • by defining abspath and wpinc, then requiring functions.php, i now have sanitize_with_dashes working, but i would like a better understanding of a) why won't sanitize_title() work; and b) why isn't it throwing warnings notifying me that its missing all the goodies from the other libs. Feels like its nodding its head and saying 'sure, i can do that', but then just waving me past without actually doing anything helpful. – zentechinc Aug 3 '17 at 15:56
  • shared hosting environment and i dont have access to update to wordpress 4.8 however, i did look into formatting.php and it's in there on line 1915. – zentechinc Aug 3 '17 at 16:00

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.