where would i put this php code in my wordpress post template?

angelas111

Elite Member
Joined
Jan 4, 2009
Messages
1,648
Reaction score
1,052
i am trying to add some code to get my wordpress theme to delete any commas in my wordpress posts. can someone show me where to put it? here is the code i am trying to use:
PHP:
echo str_replace(',', ' ',
and here is my single.php file (i believe this is the right file)
PHP:
<?php get_header(); ?>
<?php if (get_option('flex_feat_display') == "no" OR get_option('flex_feat_pages') == "home only") { ?>
<?php } else { ?>
<?php if (get_option('flex_header_trans') == "yes" && get_option('flex_feat_trans') == "yes" && get_option('flex_nav_position') == "below") { ?>
	<script type="text/javascript">
	jQuery("#nav").css("border-bottom","solid <?php echo get_option('flex_blog_borderwidth'); ?>px #<?php echo get_option('flex_blog_bordercolor'); ?>");
	</script>
<?php } ?>
<div id="feature">
	<div class="topshadow">
		<?php if ( function_exists('dynamic_sidebar') && dynamic_sidebar('Top Feature') ) : else : ?>
		<?php include (TEMPLATEPATH . '/feature.php'); ?>
		<?php endif; ?>
		<div style="clear:both;"></div>
	</div>
</div>
<?php } ?>
<?php if (get_option('flex_nav_show') == "yes" && (get_option('flex_nav_position') == "below feature")) {
include (TEMPLATEPATH . '/includes/nav.inc.php');
} ?>
<div id="content">
	 <?php if (get_option('flex_adsense_loc1') == "yes" && !get_option('flex_adsense_loc1banner')) { ?>
  <div style="width:730px;height:90px;display:block;margin:5px auto 15px auto;text-align:center;">
    <?php 

	$adsize = get_option('flex_adsense_loc1size');

	$adtype = get_option('flex_adsense_loc1type');
	
	$adchannel = get_option('flex_adsense_loc1channel');

	include('adsense.php'); ?>
  </div>
  <?php } else if (get_option('flex_adsense_loc1') == "yes" && get_option('flex_adsense_loc1banner')) { ?>
   <div style="width:730px;height:90px;display:block;margin:5px auto 15px auto;text-align:center;">
   <?php echo stripslashes(get_option('flex_adsense_loc1banner'));  ?>
   </div>
   <?php } ?>
   <?php if (get_option('flex_lsb_show') == "yes" && get_option('flex_lsb_pos') == "left of post") { get_sidebar('left'); } ?>
	<div class="postwrap">
		<?php if (have_posts()) : ?>
		<?php while (have_posts()) : the_post(); ?>
		<div class="post" id="post-<?php the_ID(); ?>">
			<div class="posthead">
				<h1><?php the_title(); ?></h1>
				<?php if (get_option('flex_metadata_show') == "yes") { ?>
				<div class="postMeta">
					<div class="postdate">
						<?php the_time('l, F jS, Y') ?>
						at
						<?php the_time() ?>
						 
						<?php edit_post_link('Edit'); ?>
					</div>
					<div style="clear:both;"></div>
				</div>
				<?php } ?>
			</div>
			<div class="postcontent">
				<?php if (get_option('flex_adsense_loc4') == "yes" && !get_option('flex_adsense_loc4banner')) { ?>
				<div style="display:block; margin:10px 0 15px 0;">
					<?php 
					$adsize = get_option('flex_adsense_loc4size');
					$adtype = get_option('flex_adsense_loc4type');	
					$adchannel = get_option('flex_adsense_loc*****nel');
					include('adsense.php'); ?>
				</div>
				<?php } else if (get_option('flex_adsense_loc4') == "yes" && get_option('flex_adsense_loc4banner')) { ?>
   <div style="display:block; margin:10px 0 15px 0; text-align:center;">
   <?php echo stripslashes(get_option('flex_adsense_loc4banner'));  ?>
   </div>
   <?php } ?>
				<?php the_content('Read the rest of this entry'); ?>
				<?php wp_link_pages('before=<div id="page-links"><p style="text-align:right"><strong>Pages:</strong>&after=</p></div>'); ?>
				<?php if (get_option('flex_show_tags') == "yes") { ?>
				<p><?php the_tags('<strong>Tagged with:</strong> ',' • ',''); ?></p>
				<?php } else { ?>
				<?php } ?>
				<?php if (get_option('flex_show_cats') == "yes") { ?>
				<p><strong>Filed under: </strong><?php the_category(' • '); ?></p>
				<?php } else { ?>
				<?php } ?>
				<?php if (get_option('flex_show_like') == "yes") { ?>
				<p><strong>Like this post?</strong> <a href="<?php if (get_option('flex_email_address')) { echo get_option('flex_email_address'); } else { bloginfo_rss('rss2_url');} ?>">Subscribe to my RSS feed</a> and get loads more! </p>
				<?php } ?>
                
                <?php if (get_option('flex_adsense_loc5') == "yes" && !get_option('flex_adsense_loc5banner')) { ?>
				<div style="display:block; margin:10px 0 15px 0;">
					<?php 
					$adsize = get_option('flex_adsense_loc5size');
					$adtype = get_option('flex_adsense_loc5type');	
					$adchannel = get_option('flex_adsense_loc5channel');
					include('adsense.php'); ?>
				</div>
				<?php } else if (get_option('flex_adsense_loc5') == "yes" && get_option('flex_adsense_loc5banner')) { ?>
   <div style="display:block; margin:10px 0 15px 0; text-align:center;">
   <?php echo stripslashes(get_option('flex_adsense_loc5banner'));  ?>
   </div>
   <?php } ?>
		</div>
		<div class="clearer"></div>
			</div>
			
		<?php if (get_option('flex_metadata_comoff') == "yes") { ?>
		<ul class="Note">
			<?php if ( comments_open() ) : ?>
			<li class="NoteRss">
				<?php post_comments_feed_link( $link_text = 'Comments RSS Feed') ?>
			</li>
			<?php endif; ?>
			<?php if ( pings_open() ) : ?>
			<li class="NoteTrackBack"><a href="<?php trackback_url() ?>" rel="trackback">
				<?php _e('TrackBack <abbr title="Uniform Resource Identifier">URI</abbr>'); ?>
				</a></li>
			<?php endif; ?>
		</ul>
		<?php comments_template('', true); ?>
		<?php } ?>
		<?php endwhile; else : ?>
		<h2>
			<?php _e('Not Found'); ?>
		</h2>
		<p>
			<?php _e('Sorry, but the page you requested cannot be found.'); ?>
		</p>
		<?php endif; ?>
	</div>
    <?php if (get_option('flex_lsb_show') == "yes" && get_option('flex_lsb_pos') == "right of post") { get_sidebar('left'); } ?>
	<?php if (get_option('flex_rsb_show') == "yes" || !get_option('flex_rsb_show')) { get_sidebar(); } ?>
</div>
<?php get_footer(); ?>
 
The quickest way would be to paste this at the top of the file first:
PHP:
<?php
if ( !function_exists('byeByeCommas')) {
    function byeByeCommas($content){
        $content = str_replace (",", " ", $content);
        return $content;
    }
}
?>

Then search for this line:
PHP:
<?php the_content('Read the rest of this entry'); ?>
Replace it with:
PHP:
<?php echo byeByeCommas(the_content()); ?>
Now all commas in the post content will be replaced with a space character.

EDIT: A much cleaner way to do this is to paste this into your functions.php somewhere and leave single.php alone:
PHP:
add_filter ('the_content', 'getRidOfCommas');

function getRidOfCommas($content) {
   if(is_single()) {
      $content = str_replace (",", " ", $content);
   }
   return $content;
}
 
Last edited:
i just realized that i should actually replace them with " instead. its for photo urls that my csv contains a comma and screws it up as for displaying the photos.

so would this be right to replace it with quotation marks?
PHP:
<?php
if ( !function_exists('byeByeCommas')) {
    function byeByeCommas($content){
        $content = str_replace (",", """, $content);
        return $content;
    }
}
?>
 
Yea pretty much except use single quotes around the double like this:
PHP:
str_replace (",", '"', $content);
 
Actually you can use singles or doubles. Single quotes can actually speed up your script because it doesn't parse what's inside it for a variable while if you use double quotes it'll parse for a variable before setting the string.

http://stackoverflow.com/questions/482202/is-there-a-performance-benefit-single-quote-vs-double-quote-in-php

Of course it's not that big a deal in the small scope, but if you were doing a lot of coding, you may want to keep that in mind.
 
i just realized that i should actually replace them with " instead. its for photo urls that my csv contains a comma and screws it up as for displaying the photos.


so would this be right to replace it with quotation marks?

If you are importing those urls you should clean that data before it goes into your database.

If the urls are already in your database you should clean them using a MySQL replace command.

If you replace commas with quotes for all content the text in some of your posts are going to look odd.
 
Back
Top