Page 1 of 1

Help with str_replace + wordpress?

Posted: Fri Sep 19, 2008 12:46 am
by ronjr7
I have a wordpress site that is using a plugin which manages users' ratings of posts. The plugin adds preformatted XHTML elements (li's) to the string that gets stored in the database. Unfortunately, due to the design of the site, we must add an additional element inside of these list items. I can do a str_replace directly in the plugin's function contained in one of the plugin files, but I know for certain that this developer releases regular updates, and I'd rather not have this get blasted every time an update comes down the pipe. I am wondering if I can do a str_replace when I call a function in my template file, for instance, something like this:

Code: Select all

 
$replaceElement = get_highest_rated();
str_replace("<li>", "<li><div>", $replaceElement);
 
This particular example doesn't work, but something of this nature would be extremely helpful, as this could be managed entirely from my template file instead of a dependency which I have much less control over.

Thanks for any ideas!

Re: Help with str_replace + wordpress?

Posted: Fri Sep 19, 2008 9:34 am
by ronjr7
Bumpety bump. Any help here? Can I provide more info?

Re: Help with str_replace + wordpress?

Posted: Fri Sep 19, 2008 9:43 am
by jayshields
What happens when you run that code snippet? What does get_highest_rate() return?

Re: Help with str_replace + wordpress?

Posted: Fri Sep 19, 2008 9:52 am
by ronjr7
When I run get_highest_rated() the plugin is returning the top five rated posts in the DB. Below is the pertinent snippet from the plugin file:

Code: Select all

 
### Function: Display Highest Rated Page/Post
if(!function_exists('get_highest_rated')) {
    function get_highest_rated($mode = '', $min_votes = 0, $limit = 10, $chars = 0, $display = true) {
        global $wpdb, $post;
        $temp_post = $post;
        $ratings_image = get_option('postratings_image');
        $ratings_max = intval(get_option('postratings_max'));
        $ratings_custom = intval(get_option('postratings_customrating'));
        $where = '';
        $temp = '';
        $output = '';
        if(!empty($mode) && $mode != 'both') {
            $where = "$wpdb->posts.post_type = '$mode'";
        } else {
            $where = '1=1';
        }
        if($ratings_custom && $ratings_max == 2) {
            $order_by = 'ratings_score';
        } else {
            $order_by = 'ratings_average';
        }
        $highest_rated = $wpdb->get_results("SELECT DISTINCT $wpdb->posts.*, (t1.meta_value+0.00) AS ratings_average, (t2.meta_value+0.00) AS ratings_users, (t3.meta_value+0.00) AS ratings_score FROM $wpdb->posts LEFT JOIN $wpdb->postmeta AS t1 ON t1.post_id = $wpdb->posts.ID LEFT JOIN $wpdb->postmeta As t2 ON t1.post_id = t2.post_id LEFT JOIN $wpdb->postmeta AS t3 ON t3.post_id = $wpdb->posts.ID WHERE t1.meta_key = 'ratings_average' AND t2.meta_key = 'ratings_users' AND t3.meta_key = 'ratings_score' AND $wpdb->posts.post_password = '' AND $wpdb->posts.post_date < '".current_time('mysql')."' AND $wpdb->posts.post_status = 'publish' AND t2.meta_value >= $min_votes AND $where ORDER BY $order_by DESC, ratings_users DESC LIMIT $limit");
        if($highest_rated) {
            foreach($highest_rated as $post) {
                // Variables
                $post_ratings_users = $post->ratings_users;
                $post_ratings_images = '';
                $post_title = get_the_title();
                $post_ratings_average = $post->ratings_average;
                $post_ratings_score = $post->ratings_score;
                $post_ratings_whole = intval($post_ratings_average);
                $post_ratings = floor($post_ratings_average);
                $post_excerpt = ratings_post_excerpt($post->post_excerpt, $post->post_content, $post->post_password);
                $post_content = get_the_content();
                // Check For Half Star
                $insert_half = 0;
                $average_diff = $post_ratings_average-$post_ratings_whole;
                if($average_diff >= 0.25 && $average_diff <= 0.75) {
                    $insert_half = $post_ratings_whole+1;
                } elseif($average_diff > 0.75) {
                    $post_ratings = $post_ratings+1;
                }
                if($ratings_custom && $ratings_max == 2) {
                    if($post_ratings_score > 0) {
                        $post_ratings_score = '+'.$post_ratings_score;
                    }
                    $image_alt = $post_ratings_score.' '.__('rating', 'wp-postratings');
                } else {
                    $image_alt = $post_ratings_users.' '.__('votes', 'wp-postratings').', '.__('average', 'wp-postratings').': '.$post_ratings_average.' '.__('out of', 'wp-postratings').' '.$ratings_max;
                }
                // Display Start Of Rating Images
                if(file_exists(ABSPATH.'/wp-content/plugins/wp-postratings/images/'.$ratings_image.'/rating_start.gif')) {
                    $post_ratings_images .= '<img src="'.get_option('siteurl').'/wp-content/plugins/wp-postratings/images/'.$ratings_image.'/rating_start.gif" alt="" class="post-ratings-image" />';
                }
                if(!$ratings_custom) { 
                    for($i=1; $i <= $ratings_max; $i++) {
                        if($i <= $post_ratings) {
                            $post_ratings_images .= '<img src="'.get_option('siteurl').'/wp-content/plugins/wp-postratings/images/'.$ratings_image.'/rating_on.gif" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';     
                        } elseif($i == $insert_half) {                      
                            $post_ratings_images .= '<img src="'.get_option('siteurl').'/wp-content/plugins/wp-postratings/images/'.$ratings_image.'/rating_half.gif" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
                        } else {
                            $post_ratings_images .= '<img src="'.get_option('siteurl').'/wp-content/plugins/wp-postratings/images/'.$ratings_image.'/rating_off.gif" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
                        }
                    }
                } else {
                    for($i=1; $i <= $ratings_max; $i++) {
                        if($i <= $post_ratings) {
                            $post_ratings_images .= '<img src="'.get_option('siteurl').'/wp-content/plugins/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_on.gif" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';      
                        } elseif($i == $insert_half) {                      
                            $post_ratings_images .= '<img src="'.get_option('siteurl').'/wp-content/plugins/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_half.gif" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
                        } else {
                            $post_ratings_images .= '<img src="'.get_option('siteurl').'/wp-content/plugins/wp-postratings/images/'.$ratings_image.'/rating_'.$i.'_off.gif" alt="'.$image_alt.'" title="'.$image_alt.'" class="post-ratings-image" />';
                        }
                    }
                }
                // Display End Of Rating Image
                if(file_exists(ABSPATH.'/wp-content/plugins/wp-postratings/images/'.$ratings_image.'/rating_end.gif')) {
                    $post_ratings_images .= '<img src="'.get_option('siteurl').'/wp-content/plugins/wp-postratings/images/'.$ratings_image.'/rating_end.gif" alt="" class="post-ratings-image" />';
                }
                if($chars > 0) {
                    if($ratings_custom && $ratings_max == 2) {
                        $temp = "<li><a href=\"".get_permalink()."\">".snippet_text($post_title, $chars)."</a> ".$image_alt."</li>\n";
                    } else {
                        $temp = "<li><a href=\"".get_permalink()."\">".snippet_text($post_title, $chars)."</a> ".$post_ratings_images."</li>\n";
                    }
                } else {
                    // Display The Contents
                    $temp = stripslashes(get_option('postratings_template_highestrated'));
                    $temp = str_replace("%RATINGS_IMAGES%", $post_ratings_images, $temp);
                    $temp = str_replace("%RATINGS_MAX%", $ratings_max, $temp);
                    $temp = str_replace("%RATINGS_AVERAGE%", $post_ratings_average, $temp);
                    $temp = str_replace("%RATINGS_SCORE%", $post_ratings_score, $temp);
                    $temp = str_replace("%RATINGS_USERS%", number_format_i18n($post_ratings_users), $temp);
                    $temp = str_replace("%POST_TITLE%", $post_title, $temp);
                    $temp = str_replace("%POST_EXCERPT%", $post_excerpt, $temp);
                    $temp = str_replace("%POST_CONTENT%", $post_content, $temp);
                    $temp = str_replace("%POST_URL%", get_permalink(), $temp);
                }
                $output .= $temp;
            }
        } else {
            $output = '<li>'.__('N/A', 'wp-postratings').'</li>'."\n";
        }
        $post = $temp_post;
        if($display) {
            echo $output;
        } else {
            return $output;
        }
    }
}
 
 
And just after line 98, I have added the following to make things work the way I like:

Code: Select all

                    
$temp = str_replace("<li>", "<li><div>", $temp);
$temp = str_replace("</li>", "</div></li>", $temp);
 
That works just fine, but as my first posts states, I'd much prefer the replacing to happen at the template level, where the function is called, rather than in the function itself so updates won't nuke my code every time.

Thanks!

Re: Help with str_replace + wordpress?

Posted: Fri Sep 19, 2008 9:56 am
by jayshields
So why can't you just move the str_replace calls to the template level like your original post suggests? If that still doesn't work you could just add some CSS to style the element like you want it or even add some JS to modify the DOM on page load.

Re: Help with str_replace + wordpress?

Posted: Fri Sep 19, 2008 10:29 am
by ronjr7
Thanks, Jay, for the responses. First off, the reason I can't simply solve the problem with CSS is due to the fact that I must style the numbers from the LI's differently than the content in the LI's. This is only achievable if the content in the LI is wrapped in a different element and styled distinctly from the containing LI.

As for JS, man, I have to admit that I wish I were better at this than I am. I would be happy to consider a JS method, but honestly prefer a PHP method so the original HTML code is correct in the first place. Is my first idea not possible?

Re: Help with str_replace + wordpress?

Posted: Fri Sep 19, 2008 12:19 pm
by jayshields
Well, I think JS is overkill because I still don't understand why your original idea won't work. Post an example of what the function returns, because by the looks of things there's no reason why your original snippet isn't working.

CSS could still work. Just use selectors to grab out those LI elements and leave all the other LI elements untouched. I'm guessing they're nested differently to the elements you want to leave untouched.

Re: Help with str_replace + wordpress?

Posted: Fri Sep 19, 2008 6:34 pm
by ronjr7
Jay -

Thanks. Let me start by saying I'm still a PHP newb. Though I've gotten my hands dirty, and been able to come out ok on previous projects, I'm still far from understanding the in's and out's of PHP.

That said, I think I'm not being as clear as I should be. Let me try again. I am using wordpress. I have a plugin installed which accepts users ratings of posts, stores them in the DB, then pulls down that data when it's called (currently in the sidebar). The plugin spits out each result wrapped in List Items. (They are actually stored in the database wrapped in LI's). This is good. This is standard. This is correct. I can wrap the function in a UL, an OL, even surrounding those with a DIV or whatever. The problem isn't targeting the LI's. That part is basic CSS. The problem lies in the fact that the comp shows the following:

{Pink Bold Number (1,2,3, etc)} {Grey Bold Article Title}
{Light Grey Non-Bold Rating}
EXAMPLE:
#1 This is the Article Title
This is the rating text

What the system spits out now:
{Pink Bold Number (1,2,3, etc)} {Grey Bold Article Title}
{Pink Non-Bold Rating}
EXAMPLE:
#1 This is the Article Title
This is the rating text

UNFORTUNATELY, due to the fact that the LI AND the bullet type (in this case decimal) are considered the same element, I cannot change the color of the text independent of the bullet type. The link inside of the LI (the Article Title) is easy to target, but the rating text is impossible to control without a similar solution to what I'm already seeking.

I guess my question, ultimately, is this: Is there a way to dissect the content that a function returns when you call that function? For instance, I know the line I need is stored in the database like this: <li><a href="%POST_URL%" title="%POST_TITLE%">%POST_TITLE%</a> %RATINGS_IMAGES% (%RATINGS_AVERAGE% out of %RATINGS_MAX%)</li>. Here is how I am instructed to call the code:

<?php if (function_exists('get_highest_rated')): ?>
<ol>
<?php get_highest_rated(); ?>
</ol>
<?php endif; ?>

Can I break the info I need into pieces (and maybe store it in an array), or at least replace the <li>'s with <li><div> instead?

One last note, Jay and anyone else: When I do this:

Code: Select all

<?php $replaceElement = get_highest_rated();
            str_replace("<li>", "<li><div>", $replaceElement); ?>
I get the same thing as when I do this:

Code: Select all

<?php get_highest_rated(); ?>
Thanks!