Need Help With Adjusting Related Posts Wordpress Plugin

Discussion in 'PHP' started by Hens00, Sep 20, 2008.

  1. #1
    Hello

    I have a site here, which I would like to use a Related Posts plugin.

    The plugin is great, but when activated it gives related posts both on the home page as well as the indidual post pages - I only want the plugin to work for indivual post pages.

    Can someone show me what to remove/edit from the plugin code?:)

    The code is:

    <?php
    /*
    Plugin Name:  Aizatto's Related Posts
    Plugin URI:   http://blendworx.com/aizattos-related-post-plugin/
    Description:  Returns a list of the related entries based on active/passive keyword matches. 
    Author:       Ezwan Aizat Bin Abdullah Faiz
    */
    
    function aizatto_related_posts_css() {
      if (is_feed())
        return '';
      echo <<<END
    <!-- Stylesheet for the 'aizatto' related post plugin' -->
    <style type="text/css">
    .aizatto_related_posts_header {
      font-size: 1.5em;
    }
    
    .aizatto_related_posts ul {
      margin: 0;
    }
    
    .aizatto_related_posts li {
      list-style-type: none;
      margin-left: 2px;
    }
    
    .aizatto_related_posts_title {
      font-size: 11px;
      font-weight: bold;
    }
    
    .aizatto_related_posts_title a {
      text-decoration: underline;
    }
    
    .aizatto_related_posts_excerpt {
      font-size: 11px;
    }
    </style>
    END;
    }
    
    function aizatto_related_posts_install_fulltext() {
      global $table_prefix, $wpdb;
      $sql      = 'SHOW INDEX FROM `'.$table_prefix.'posts` WHERE Key_name = "post_related"';
      if (count($wpdb->get_results($sql)) > 1)
        return;
    
      $sql      = 'ALTER TABLE `'.$table_prefix.'posts` ADD FULLTEXT `post_related` ( `post_name` , `post_content` )';
      @$wpdb->get_results($sql);
    
      $options  = get_option('aizatto_related_posts');
      $options['fulltext_index_installed'] = true;
      update_option('aizatto_related_posts', $options);
    }
    
    function aizatto_related_posts() {
      global $wpdb, $post;
    
      $options = get_option('aizatto_related_posts');
      if ($options['show_rss'] == false &&
        is_feed()) {
        return '';
      }
    
      $limit          = $options['limit']           ? $options['limit'] : 3;
      $show_excerpt   = $options['show_excerpt']    ? true : false;
      $show_pass_post = $options['show_pass_post']  ? true : false;
    
    	// Get option values from the options page
      $limit          = $options['limit'];
      $len            = $options['excerpt_length'];
    
      $before_title   = '<span class="aizatto_related_posts_title" >';
      $after_title    = '</span>';
      $before_excerpt = '<div class="aizatto_related_posts_excerpt">';
      $after_excerpt  = '</div>';
      $before_related = '<li>';
      $after_related  = '</li>';
    	
    	// Fetch keywords
      $postcustom = get_post_custom_values('keyword');
      if (!empty($postcustom)) {
        $values = array_map('trim', $postcustom);
        $terms = implode($values, ' ');
      } else {
        $terms = str_replace('-', ' ', $post->post_name);
      }
    
    	// Make sure the post is not from the future
    	$time_difference = get_settings('gmt_offset');
    	$now = gmdate("Y-m-d H:i:s",(time()+($time_difference*3600)));
    	
    	if (!$limit) $limit = '0,3'; // Quick fix for "no defaults" bug 28.9
    	
    	// Primary SQL query
      $sql = "SELECT ID, post_title, post_content,"
           . "MATCH (post_name, post_content) "
           . "AGAINST ('$terms') AS score "
           . "FROM $wpdb->posts WHERE "
           . "MATCH (post_name, post_content) "
           . "AGAINST ('$terms') "
           . "AND post_date <= '$now' "
           . "AND (post_status IN ( 'publish',  'static' ) && ID != '$post->ID') ";
      if ($show_pass_post=='false') { $sql .= "AND post_password ='' "; }
      $sql .= "ORDER BY score DESC LIMIT $limit";
      $results = $wpdb->get_results($sql);
    
      if (! $results) {
        if($return_bool)
          return false;
    
        return $before_related . $before_title . 'No related posts' . $after_title . $after_related;
      }
    
      foreach ($results as $result) {
        $title        = stripslashes(apply_filters('the_title', $result->post_title));
        $permalink    = get_permalink($result->ID);
        if ($options['show_excerpt']) {
          $post_content = preg_replace('/<.*?>/', ' ', $post_content);
          $post_content = strip_tags($result->post_content);
          $post_content = stripslashes($post_content);
          $ze           = substr($post_content, 0, $len);
          $ze           = $ze . '...';
        }
        $link         = strstr($permalink, '://');
        $link         = substr($link, 3, strlen($link));
    
        $output      .= $before_related;
        $output      .= $before_title .'<a href="'. $permalink .'" rel="bookmark" title="Permanent Link: ' . $title . '" >' . $title . '</a>' . $after_title;
        $output      .= $before_excerpt . $ze;
        if (!is_feed()) {
    #      $output      .= '<br /><a href="'.$permalink.'" rel="bookmark" title="Permanent Link: ' . $title . '" >' . $link . '</a>';
        }
        $output      .= $after_excerpt;
        $output      .= $after_related;
    //					$words=split(" ",$post_content);
    //					$post_strip = join(" ", array_slice($words,0,$len));
    //					$output .= $before_post . $post_strip . $after_post;
      }
    
      return $output;
    }
    
    function aizatto_related_posts_pop($content) {
      return $content . '<div class="aizatto_related_posts"><span class="aizatto_related_posts_header" >Related Posts</span><ul>' . aizatto_related_posts() . '</ul></div>';
    }
    
    // End Related Posts Options
    function aizatto_related_posts_admin_menu() {
      if (function_exists('add_submenu_page')) {
        add_options_page('Aizatto\'s Related Posts', 'Aizatto\'s Related Posts', 8, basename(__FILE__), 'aizatto_related_posts_options');
      }
    }
    
    function aizatto_related_posts_install() {
      if ($options['fulltext_index_installed'] == false) {
        aizatto_related_posts_install_fulltext();
      }
      $options = get_option('aizatto_related_posts');
      $options['excerpt_length'] ? $options['excerpt_length'] : 50;
      $options['limit']          ? $options['limit'] : 3;
      $options['show_excerpt']   ? true : false;
      $options['show_rss']       ? true : false;
      $options['show_pass_post'] = false;
      update_option('aizatto_related_posts', $options);
    }
    
    function aizatto_related_posts_options() {
      if ($_POST['install_arp_fulltext']) {
        aizatto_related_posts_install_fulltext(); ?>
    <div class="wrap">
      <h2>Force Install Successful</h2>
    </div>
    <?php
      }
    
      if ($_POST['update_arp']) {
        $options['limit']          = $_POST['limit'];
        $options['excerpt_length'] = $_POST['excerpt_length'];
        $options['show_excerpt']   = $_POST['show_excerpt']   == 'true';
    //    $options['show_pass_post'] = $_POST['show_pass_post'] == 'true';
        $options['show_rss']       = $_POST['show_rss'] == 'true';
        update_option('aizatto_related_posts', $options);
      }
    
      $options        = get_option('aizatto_related_posts');
      $excerpt_length = $options['excerpt_length']     ? $options['excerpt_length'] : 100;
      $limit          = $options['limit']           ? $options['limit'] : 3;
      $show_excerpt   = $options['show_excerpt']    ? true : false;
      $show_pass_post = $options['show_pass_post']  ? true : false;
      $show_rss       = $options['show_rss']        ? true : false;
    ?>
    <div class="wrap">
      <h2>Aizatto's Related Post</h2>
      <form method="post"> 
        <p> Aizatto's Related Posts modifies some of your tables such that they can be searchable. This is needed for the plugin to work, don't worry though.  Its not destructive. </p>
        <p> If you are having problems with your 'related posts', maybe you need to force install it? </p>
        <p>
        <div class="submit"><input type="submit" name="install_arp_fulltext" value="Force Install"  style="font-weight:bold;" /></div></p>
    		<fieldset class="options">
          <table>
            <tr>
              <td><label for="limit">How many related posts would you like to show?</label>:</td>
              <td><input name="limit" type="text" id="limit" value="<?php echo get_option('limit'); ?>" size="3" /></td>
            </tr>
            <tr>
              <td>Show excerpt?</td>
              <td>
                <select name="show_excerpt" id="show_excerpt">
                  <option <?php if($show_excerpt == false) { echo 'selected'; } ?> value="false">False</option>
                  <option <?php if($show_excerpt == true) { echo 'selected'; } ?> value="true">True</option>
                </select>
              </td> 
            </tr>  
            <tr>
              <td><label for="excerpt_length">Excerpt length (No. of words):</label></td>
              <td><input name="excerpt_length" type="text" id="len" value="<?php echo $excerpt_length; ?>" size="3" /> 
            </tr>
    <!--
            <tr>
              <td><label for="show_pass_post">Show password protected posts?</label></td>
              <td>
                    <select name="show_pass_post" id="show_pass_post">
                      <option <?php if($show_pass_post == false) { echo 'selected'; } ?> value="false">False</option>
                      <option <?php if($show_pass_post == true) { echo 'selected'; } ?> value="true">True</option>
                    </select> 
              </td>
            </tr>
    -->
            <tr>
              <td><label for="show_rss">Show in RSS feeds?</label></td>
              <td>
                    <select name="show_rss" id="show_rss">
                      <option <?php if($show_rss == false) { echo 'selected'; } ?> value="false">False</option>
                      <option <?php if($show_rss == true) { echo 'selected'; } ?> value="true">True</option>
                    </select> 
              </td>
            </tr>
          </table>
        </fieldset>
    		<p><div class="submit"><input type="submit" name="update_arp" value="<?php _e('Save!', 'update_arp') ?>"  style="font-weight:bold;" /></div></p>
        <p>This is plugin is based off the original <a href="http://www.w-a-s-a-b-i.com/archives/2006/02/02/wordpress-related-entries-20/"> Related Posts</a> Plugin </p>
      </form>
    </div>
    <?php
    }
    
    add_action('activate_aizatto_related_posts.php', 'aizatto_related_posts_install');
    add_filter('wp_head', 'aizatto_related_posts_css');
    add_filter('the_content', 'aizatto_related_posts_pop');
    add_action('admin_menu', 'aizatto_related_posts_admin_menu');
    
    
    Code (markup):
     
    Hens00, Sep 20, 2008 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this
    add_action('activate_aizatto_related_posts.php', 'aizatto_related_posts_install');
    if(!is_home())
    {
    	add_filter('wp_head', 'aizatto_related_posts_css');
    	add_filter('the_content', 'aizatto_related_posts_pop');
    }
    add_action('admin_menu', 'aizatto_related_posts_admin_menu');
    PHP:
    (put it in place of the code at the end of the plugin)
     
    JAY6390, Sep 20, 2008 IP
  3. Hens00

    Hens00 Active Member

    Messages:
    177
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Thank you Jay, I tried adding the code but unfortunately the related posts still appear on the home page as well as in the post pages?
     
    Hens00, Sep 20, 2008 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try changing the
    if(!is_home())
    to
    if(!is_front_page())
     
    JAY6390, Sep 20, 2008 IP
  5. Hens00

    Hens00 Active Member

    Messages:
    177
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #5
    Sorry dude, when I changed it, I get an error message:


    Fatal error: Call to undefined function s_front_page() in /home/choosemo/public_html/thingsaboutphones/wp-content/plugins/aizatto_related_posts.php on line 256
     
    Hens00, Sep 20, 2008 IP
  6. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #6
    That's because you have put s_front_page instead of is_front_page (note the I before the function name)
     
    JAY6390, Sep 20, 2008 IP
  7. Hens00

    Hens00 Active Member

    Messages:
    177
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #7
    Ok I tried again using the correct code....and no luck. The plugin activates this time, but the related posts still appear.

    I have been looking around for more help and have tried changing the 'frontpage' tag to 'single', and that doesnt work either :confused:
     
    Hens00, Sep 20, 2008 IP
  8. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You tried
    if(is_single()) not if(!is_single()) I hope. If that doesn't work, I'm not sure what's wrong sorry
     
    JAY6390, Sep 20, 2008 IP
  9. Hens00

    Hens00 Active Member

    Messages:
    177
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #9
    Ahh man I tried it with both the ! included and also without...and its the same :(

    Thanks for you help anyway dude.
     
    Hens00, Sep 20, 2008 IP
  10. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #10
    No probs. While I know my php, I'm not too familiar with the way wordpress implements its plugins so it was more guesswork than actual know how with the suggestions
     
    JAY6390, Sep 20, 2008 IP
  11. juust

    juust Peon

    Messages:
    214
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #11
    juust, Sep 20, 2008 IP