MKDIR (wp_mkdir_p) with "-" comes out strange - php

Using MKDIR
File Name Test – () _ <>
However the folder that is created it:
File Name Test – () _ <>
Evidently the "-" seems to cause an issue.
The code
Here are the cars I am using as well:
$web_video_location = get_the_title();
$category = get_the_category();
$catname = $category[0]->cat_name;
if (wp_mkdir_p('./wp-content/uploads/webvideos/'.$catname."/".$web_video_location)) {
echo "";
}
I am not quite sure what I am missing. The folder is created on a webserver.
update
If I make a WP title, such as:
File Name Test-() _ <>
It make a folder:
File Name Test-() _ <>
it appears it is something to do with the spaces.
Update
I also tried this:
$mkdir_cat = "$webdir$catname";
$mkdir_file = "$mkdir_cat/$web_video_location";
if (wp_mkdir_p( $mkdir_file )) {
echo "";
}
Still not joy. I think this is a little over my head.
If the cat has a - it works? I do not get it.

This is WordPress trying to be helpful - the wptexturize function is automatically run all over the place, including in the filter the_title applied by the_title()/get_the_title(). It does various "smart" transforms, including turning - (space hyphen space) into – (en-dash).
However, it does this by emitting HTML entities like –. This is not so useful when you are trying to use content for something other than HTML display.
I can think of a few workarounds:
allow WordPress to format the dash, then unescape it with html_entity_decode, e.g. $web_video_location = html_entity_decode(get_the_title());
disable wptexturize globally, e.g. using run_wptexturize, so it leaves your titles alone
grab the title a different way, e.g. accessing $post->post_title directly; you can see the source of the get_the_title function for some hints on how
use something other than the title as the filename, e.g. a Custom Field on the post

Related

Why won't my substr call work with the → character?

I'm building a WooCommerce site for a client using WordPress at the moment and I've written a piece of code that dynamically generates tables with products and product attributes in them. When I call the get_title() method, it gives me a title with the product's parent product first, the "→" sign, then the actual title of the product. I wrote this code to remove the "→" sign and everything before it.
$name_raw = $product->get_title();
$name = substr($name_raw, ($pos = strpos($name_raw, '→')) !== false ? $pos + 1 : 0);
It used to work perfectly, but not anymore. This code also works perfectly with every other character I've put in to test it, but not with that stupid little arrow. It seems like a recent update to something I'm using must've removed this method's ability to handle non-conventional characters. Can anyone recommend any possible solutions?
Since it's a WP environment title, the right arrow character probably has been entered using the WP form for a post. I think this character would be converted to it's HTML code, one of these:
→
&rightarrow;
&RightArrow;
&srarr;
&ShortRightArrow;
→
→
So I'd do something like this:
function no_right_arrow_name($name_raw) {
if (empty($name_raw))
return $name_raw;
$right_arrows = array(
'→',
'&rightarrow;',
'&RightArrow;',
'&srarr;',
'&ShortRightArrow;',
'→',
'→'
);
$name_new = $name_raw;
// removes all possible combinations of right arrows
foreach ($right_arrows as $right_arrow)
$name_new = str_replace($right_arrow, '', $name_new);
return $name_new;
}
$name_raw = $product->get_title();
$name = no_right_arrow_name($name_raw);

Convert Plain Text Address to clickable link to google maps via PHP

Pulling in data from a Filemaker Pro database field and trying to convert the plain text data from the field into a clickable link to google maps via PHP.
My first attempt doesnt display anything when called:
$Venue = '';
$Venue is then echoed into a UL via
<?php echo $Venue; ?>
I'm relatively new to PHP so I'm sure there is a much more semantic way of marking this up? Possibly a regex and replace, returning a preg_replace? Which is what I've been using for plain text URLs and email addresses.
Anything helps, thanks so much.
Casey - not sure there's enough context here to help you? For instance, forget the link wrapping, does
<?php echo $record->getField( 'Auctions::AIS_Venue'); ?>
actually echo the field contents to a page?
Also, not sure you really want to use nl2br anyway, as you probably don't want a break in your url ;-)
OK, in which case there are a couple of things to try. First - can you get it to run in a webviewer inside FileMaker? I suspect not, and it may be down to an encoding/filtering issue.
There are two approaches you could use.
Create a new calc field in FileMaker that filters out punctuation and converts spaces to '+'s, something like:
Filter( Substitute( Lower( Auctions::AIS_Venue ) ; [ ¶ ; "+"] ; [" " ; "+"]; ["++" ; "+"] ); "abcdefghijklmnopqrstuvwxyz1234567890+" )
and then use that field as in your PHP code, or;
Do the same calculation on the fly in PHP, something like:
$Venue = '';
That should do the trick ? ;-)

Importing PHP string with quotes

So I'm importing ExpressionEngine fields into a php array. I want to display one field, called {gearboxx_body}, unless that field has more then 300 characters, in which case I want to display a field called {article_blurb}. I'm pretty sure there isn't a way to do this just in ExpressionEngine fields and conditionals, so I tried some PHP, which I'm just starting to learn:
<?php
$info = array('{gearboxx_body}','{article_blurb}');
if(mb_strlen($info[0]) <= 300)
echo($info[0]);
}
else {
echo($info[1]);
}
?>
So that works well, but there's a problem. If the tag includes any apostrophes or quote marks, it ends the string and the page won't load. So what can I do about this? I've tried to replace the quote marks in the string, but I have to have loaded the string from the fields first, and as soon as I do that the page is already broken.
Hopefully that made sense. Any suggestions?
I would recommend you handle this in an EE plugin rather than in the template:
Faster to render (because you don't need the overhead of PHP in the templates)
More secure and reliable
Faster to develop once you get the basics of EE development down which is a useful life skill
All around best-practice
The plugin I have in mind takes three parameters:
body, blurb and character limit.
Let's say you call your plugin "Blurby". In the template you would just have this:
{exp:blurby body="{gearboxx_body}" blurb="{article_blurb}" char_limit="300"}
It variably returns either of your fields based on the logic you define in the plugin itself.
See plugin developer documentation.
Alternatively you could use the dreaded HEREDOC syntax to set variables before passing them into your array:
$body = <<<EOT
{gearboxx_body}
EOT;
$blurb = <<<EOT
{article_blurb}
EOT;

PHP character encoding issue

When I grab the title from my Word Press posts in code and pass them around as email, the punctuation gets a bit mangled.
For example "TAMAGOTCHI P’S LOVE & MELODY SET" comes out as "TAMAGOTCHI P’S LOVE & MELODY SET".
Any ideas how I prevent this?
Let me know if you need to see the specific code I'm currently using. (I'm not really sure if this is a WordPress issue, or a PHP issue.
EDIT
What happens is that this title is passed to a form via the query string. Then when the form is submitted, I take the string from the form field and email it.
So I guess I need to decode the html either before I pass it into the form field, or else before I email it.
EDIT 2
Weird, so I looked closer at the code and I'm already doing a urldecode before I pass the value into the form field
jQuery('#product_name').val("<?php echo urldecode(strip_tags($_GET['pname'])); ?>
Is there some default encoding happening when you serialize (for ajax formhandler)
var dataString = $(this).serialize();
EDIT 3
OK turns out the code is more complex. Title is also passed to some kind of wordpress session before it's hits the form. I'll figure it out where exactly I need to put urldecode. Thanks!
This is one WordPress "feature" I could do without.
Here's one down-n-dirty method to get the fancy quotes (or other entities) replaced:
$title = get_the_title( get_the_ID() );
$title = str_replace( '&#8217', "'", $title );
echo $title;
We could integrate deeper, by hooking into the_title, if you want this same de-entities functionality throughout the site. This code block would belong in your theme's functions.php file.
function reform_title($title, $id) {
$title = str_replace( '&#8217', "'", $title );
return $title;
}
add_filter('the_title', 'reform_title', 10, 2);
Im not really sure about wordpress, but the issue itself its that the text its coming out as URLENCODE instead of a UTF-8 or other encode.
You have two options
When you receive the text you never turn it back to normal encoding (Which is weird as usually is de-encoded by php when you access the $_GET or $_POST variables)
You are parsing the message with the urlencode() function.

String replace the contents of a div

What I want to do:
I have a div with an id. Whenever ">" occurs I want to replace it with ">>". I also want to prefix the div with "You are here: ".
Example:
<div id="bbp-breadcrumb">Home > About > Contact</div>
Context:
My div contains breadcrumb links for bbPress but I'm trying to match its format to a site-wode bread crumb plugin that I'm using for WordPress. The div is called as function in PHP and outputted as HTML.
My question:
Do I use PHP of Javascript to replace the symbols and how do I go about calling the contents of the div in the first place?
Find the code that's generating the <, and either set the appropriate option (breadcrumb_separator or so) or modify the php code to change the separator.
Modifying supposedly static text with JavaScript is not only a maintenance nightmare, extremely brittle, and might lead to a strange rendering (as users see your site being modified if their system is slow), but will also not work in browsers without (or with disabled) JavaScript support.
You could use CSS to add the you are here text:
#bbp-breadcrumb:before {
content: "You are here: ";
}
Browser support:
http://www.quirksmode.org/css/beforeafter_content.html
You could change the > to >> with javascript:
var htmlElement = document.getElementById('bbp-breadcrumb');
htmlElement.innerHTML = htmlElement.innerHTML.split('>').join('>>').split('>').join('>>')
I don't recommend altering content like this, this is really hacky. You'd better change the ouput rendering of the breadcrumb plugin if possible. Within Wordpress this should be doable.
you can use a regex to match the breadcrumb content.. make the changes on it.. and put it back in the context..
check if this helps you:
$the_existing_html = 'somethis before<div id="bbp-breadcrumb">Home > About > Contact</div>something after'; // let's say this is your curreny html.. just added some context
echo $the_existing_html, '<hr />'; // output.. so that you can see the difference at the end
$pattern ='|<div(.*)bbp-breadcrumb(.*)>(.*)<\/div>|sU'; // find some text that is in a div that has "bbp-breadcrumb" somewhere in its atributes list
$all = preg_match_all($pattern, $the_existing_html, $matches); // match that pattern
$current_bc = $matches[3][0]; // get the text inside that div
$new_bc = 'You are here: ' . str_replace('>', '>>', $current_bc);// replace entity for > with the same thing repeated twice
$the_final_html = str_replace($current_bc, $new_bc, $the_existing_html); // replace the initial breadcrumb with the new one
echo $the_final_html; // output to see where we got

Categories