add_filter( 'comment_text', 'mh_commenttaglink' , 50 );
function mh_commenttaglink( $text ) {
// RegEx to find #tag, #hyphen-tag with letters and numbers
$mh_regex = "/\#[a-zA-Z0-9-]+/";
// Use that RegEx and populate the hits into an array
preg_match_all( $mh_regex , $text , $mh_matches );
// If there's any hits then loop though those and replace those hits with a link
for ( $mh_count = 0; $mh_count < count( $mh_matches[0] ); $mh_count++ )
{
$mh_old = $mh_matches[0][$mh_count];
$mh_old_lesshash = str_replace( '#' , ' ' , $mh_old );
$mh_new = str_replace( $mh_old , '' . $mh_old . '' , $mh_matches[0][$mh_count] );
$text = str_replace( $mh_old , $mh_new , $text );
}
// Return any substitutions
return $text;
}
I have a small custom plugin for wordpress here that converts all hashtagged terms in post comments into searchable links.
I know this plugin is causing the issue as it only buggers off after deactivation, I just cant see where in this code I need to edit something out or add somethign in to prevent it.
More experienced eyes would be appreciated.
Related
I am looking to clean whitespace and hyphen characters from the start/end of a string.
I have the following test code:
$sample_titles = array(
'— — -Daisy Chain –',
'Ocarina for a Mermaid –',
' –—Another for a Sailor - '
);
foreach ( $sample_titles as $title ) {
$updated_title = ltrim( $title, ' -–—' );
$updated_title = rtrim( $updated_title, ' -–—' );
echo $updated_title . '<br/>';
}
This correctly outputs:
Daisy Chain
Ocarina for a Mermaid
Another for a Sailor
However, when I apply the same ltrim/rtrim logic in a foreach loop (over post titles, I'm "cleaning" imported data, the rest of the code is irrelevant) like this:
foreach ( $product_ids as $key => $product_id ) {
$title = get_the_title( $product_id );
$updated_title = ltrim( $title, ' -–—' );
$updated_title = rtrim( $updated_title, ' -–—' );
echo $updated_title . '<br/>';
};
I still end up with the hyphens/dashes/whitespace like this:
Orb 3 –
Ocarina for a Mermaid –
Mini Marina –
Any ideas why this works in one context but not the other?
Simply wrapping the title grab function in html_entity_decode() fixed the issue I was having:
$product_title = html_entity_decode( get_the_title( $product_id ) );
Trying to remove the whitespace before the , if the $middle field is empty. This is the code I have
function show_update_postdata( $value, $post_id, $field ) {
// Get values from POST
$first = $_POST['acf']['field_5b8536ef3839f'];
$middle = $_POST['acf']['field_5b853701383a0'];
$last = $_POST['acf']['field_5b8536e53839e'];
$creds= $_POST['acf']['field_5b853717383a1'];
// Custom post title
$title = $last . ', ' . $first . ' '. $middle .', ' . $creds;
$slug = sanitize_title( $title );
$postdata = array(
'ID' => $post_id,
'post_title' => $title,
'post_type' => 'physicians',
'post_name' => $slug
);
wp_update_post( $postdata );
return $value;
}
add_filter('acf/update_value/name=first_name', 'show_update_postdata', 10, 3);
add_filter('acf/update_value/name=middle_name_initial', 'show_update_postdata', 10, 3);
add_filter('acf/update_value/name=last_name', 'show_update_postdata', 10, 3);
add_filter('acf/update_value/name=credentials', 'show_update_postdata', 10, 3);
Currently the output is Doe, John D., MD if the $middle has a value, but if the $middle has no value I am getting this Doe, John , MD but it should be Doe, John, MD
Any help would be much appreciated.
but if the $middle has no value I am getting this Doe, John , MD
Just because $middle is empty, the space character inserted before it does not automatically disappear with it.
So check if the variable is empty, and only if not, insert the space and the value:
$title = $last . ', ' . $first . ( $middle != '' ? ' '.$middle : '' ) .', ' . $creds;
You can use trim() function to...
Strip whitespace (or other characters) from the beginning and end of a
string
if(empty($middle)) $middle = trim($middle);
Use rtrim function instead of trim to validate every single variable and url.
$var=rtrim($variable);
$var=ltrim($var);
otherwise empty() check is the best solution
if(empty($variable))
at first: No, this is not a duplicate. I know that there are some possibilities to search for elements in a HTML-page, but this is not really my problem.
I will outline my problem:
My PHP-code is for reasons I can not change called 2-3 times on every page-rendering.
My code crawls the html-content for specific words and replaces them with a link.
To archive this I am using https://github.com/sunra/php-simple-html-dom-parser .
This is my source:
foreach ( $dom->find( 'text' ) as $element ) {
//$config['exclusions'] is an array like ['a', 'img']
if ( !in_array( $element->parent()->tag, $config[ 'exclusions' ] ) ) {
foreach ( $markers as $marker ) {
$text = $marker[ 'text' ];
$url = $marker[ 'url' ];
$tip = strip_tags( $marker[ 'excerpt' ] );
$tooltip = ( $tooltip ? "data-uk-tooltip title='$tip'" : "" );
$tmpval = "tmpval-$i";
$element->innertext = preg_replace(
'/\b' . preg_quote( $text, "/" ) . '\b/i',
"<a href='$url' $hrefclass target='$target' $tmpval>\$0</a>",
$element->innertext,
1
);
$element->innertext = str_replace( $tmpval, $tooltip, $element->innertext );
$i++;
}
}
}
The problem is: If the $tooltip contains a word that matches a marker, this word is being replaced. So the result is <a href='foo.html' target='_self' data-uk-tooltip title='<a href='bar.html'...'>\$0</a> which destroys the markup of the page.
So my question: How can I prevent this?
Use lookbehind:
$element->innertext = preg_replace(
'(?<!\w=['"])\b' . preg_quote( $text, "/" ) . '\b/ig',
"<a href='$url' $hrefclass target='$target' $tmpval>\$0</a>",
$element->innertext,
1
);
I just want to display the categories for my blog posts, but to SOME of the categories (and especially if they stand alone, the last bit get's trimmed away ~ "Music" becomes "Mu", and "Adventure" becomes "Adventur" ... any help? Please!
// Category boxes :P
function showcatz() {
global $post;
echo '<div class="categz_wrapper"><div class="categz">';
// get the category IDs assigned to post
$categories = wp_get_post_categories( $post->ID, array( 'fields' => 'ids' ) );
// separator between links
$separator = '</div><div class="categz"> ';
if ( $categories ) {
// List categories
$cat_ids = implode( ',' , $categories );
// Remove ONE category from the list
$kill = array("411,", "411");
$killit = str_replace($kill, "", $cat_ids);
$cats = wp_list_categories( 'title_li=&style=none&echo=0&include=' . $killit);
$cats = rtrim( trim( str_replace( '<br />', $separator, $cats ) ), $separator );
// Only show categories if there is any
if ( $killit ) { echo $cats; }
}
echo '</div></div>';
}
your passing a parameter to rtrim called $separator which has the value </div><div class="categz"> so when the following statement is executed it will remove the following chars from your string. div<>clastegz
rtrim( str_replace( '<br />', $separator, $cats ) ), $separator );
Solution, remove the second parameter to rtrim
I have a bbcode plugin for wordpress.
But for some reason, if I post something like
[i]v497212he2x2MfMi[/i] the "X" character is outputted as ×, which is some other sort of X. How can I fix this?
Plugin code is below:
class BBCode {
// Plugin initialization
function BBCode() {
// This version only supports WP 2.5+ (learn to upgrade please!)
if ( !function_exists('add_shortcode') ) return;
// Register the shortcodes
add_shortcode( 'b' , array(&$this, 'shortcode_bold') );
add_shortcode( 'i' , array(&$this, 'shortcode_italics') );
}
// No-name attribute fixing
function attributefix( $atts = array() ) {
if ( empty($atts[0]) ) return $atts;
if ( 0 !== preg_match( '#=("|\')(.*?)("|\')#', $atts[0], $match ) )
$atts[0] = $match[2];
return $atts;
}
// Bold shortcode
function shortcode_bold( $atts = array(), $content = NULL ) {
if ( NULL === $content ) return '';
return '<strong>' . do_shortcode( $content ) . '</strong>';
}
// Italics shortcode
function shortcode_italics( $atts = array(), $content = NULL ) {
if ( NULL === $content ) return '';
return '<em>' . do_shortcode( $content ) . '</em>';
}
}
// Start this plugin once all other plugins are fully loaded
add_action( 'plugins_loaded', create_function( '', 'global $BBCode; $BBCode = new BBCode();' ) );
This transformation is taking place because of Wordpress's wptexturize() function that returns given text with transformations of quotes to smart quotes, apostrophes, dashes, ellipses, the trademark symbol, and the multiplication symbol.
This is from WP 3.2.1 wp-includes/formatting.php line 55:
$dynamic_characters = array('/\'(\d\d(?:’|\')?s)/', '/\'(\d)/', '/(\s|\A|[([{<]|")\'/', '/(\d)"/', '/(\d)\'/', '/(\S)\'([^\'\s])/', '/(\s|\A|[([{<])"(?!\s)/', '/"(\s|\S|\Z)/', '/\'([\s.]|\Z)/', '/\b(\d+)x(\d+)\b/');
$dynamic_replacements = array('’$1','’$1', '$1‘', '$1″', '$1′', '$1’$2', '$1' . $opening_quote . '$2', $closing_quote . '$1', '’$1', '$1×$2');
The last regex in that $dynamic_characters array is the one turning the "X" into ×
As stated on the function page for wptexturize... "[t]ext enclosed in the tags <pre>, <code>, <kbd>, <style>, <script>, <tt>, and [code] will be skipped.", you can fix this by putting that bbcode in one of those tags, or use a plugin that can disable wptexturize, such as InScript or Disabler or Disable wptexturize.