Help with a WP plugin using apply_filters

  • b_zilla
  • Novice
  • Novice
  • b_zilla
  • Posts: 15
  • Loc: Seattle, WA

Post 3+ Months Ago

Hi, I've had no luck on wordpress forums so I thought I'd try here.

Pretty new to scripting I admit, this is a plugin I'd like to modify to output post classes.

When I try using: apply_filters( 'post_class', $page->post_class ) at the bottom, it only outputs array=" ". Do I need an additional function to hook into post classes? In my research I can't tell if my syntax is wrong or I'm missing a function to "pull" in the classes for `apply_filters` to grab onto. Thanks a ton for any help!

Code: [ Select ]
    var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');


    function start_lvl(&$output, $depth = 0, $args = array()) {
      $indent = str_repeat("\t", $depth);
      $output .= "\n$indent<ul class='children'>\n";
    }


    function end_lvl(&$output, $depth = 0, $args = array()) {
      $indent = str_repeat("\t", $depth);
      $output .= "$indent</ul>\n";
    }


    function start_el(&$output, $page, $depth = 0, $args = array(), $id = 0) {
      if ( $depth )
        $indent = str_repeat("\t", $depth);
      else
        $indent = '';

      extract($args, EXTR_SKIP);

      $output .= $indent . '<li id="item_'.$page->ID.'" '.apply_filters( 'post_class', $page->post_class ).'><span>'.apply_filters( 'the_title', $page->post_title, $page->ID ).'</span>';
    }
  1.     var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');
  2.     function start_lvl(&$output, $depth = 0, $args = array()) {
  3.       $indent = str_repeat("\t", $depth);
  4.       $output .= "\n$indent<ul class='children'>\n";
  5.     }
  6.     function end_lvl(&$output, $depth = 0, $args = array()) {
  7.       $indent = str_repeat("\t", $depth);
  8.       $output .= "$indent</ul>\n";
  9.     }
  10.     function start_el(&$output, $page, $depth = 0, $args = array(), $id = 0) {
  11.       if ( $depth )
  12.         $indent = str_repeat("\t", $depth);
  13.       else
  14.         $indent = '';
  15.       extract($args, EXTR_SKIP);
  16.       $output .= $indent . '<li id="item_'.$page->ID.'" '.apply_filters( 'post_class', $page->post_class ).'><span>'.apply_filters( 'the_title', $page->post_title, $page->ID ).'</span>';
  17.     }
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9099
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

I looked over your code quickly, but it is hard to tell what the function was before you made changes.

One guess is that your $page->post_title and $page->ID are blank. Where is the function start_el being executed in your code? All you have shown me here is your function definitions, but are you sure its being called?

One way you can test is to output some text and exit and see if it displays on your screen inside the function. If it does, then it executed, if it doesn't then it is not executing.

Another thing you can do would be to do a:

PHP Code: [ Select ]
print_r($page);
exit;
  1. print_r($page);
  2. exit;


Inside of that function right before your $output is assigned to make sure that the $page variable has all of the information it needs, particularly the post_title and ID.

If it does indeed have everything then I would look more closely to the timing on when apply_filters is being called. Without running it through a debugger it is hard to tell, but I know that if you use "All in One SEO" or anything else related that they will override anything you do when you do the_title apply_filters and instead you would want to use the hook: "aioseop_title filter" instead. It really depends on your setup though, you may not be using that, and there are many variables that could affect to why this might not be working for you. So my advice here would be to check if you are using any SEO plugins, or plugins in which you think they might be modifying the title of your pages. I would recommend disabling those plugins just to see if your code works, if it does, then know you might need to find a hook inside that plugin that lets you override like with the All in One SEO plugin above I mentioned.

Hopefully this gets you going in the right direction.
  • b_zilla
  • Novice
  • Novice
  • b_zilla
  • Posts: 15
  • Loc: Seattle, WA

Post 3+ Months Ago

Thanks a ton and PM me with where I can send a Pete's coffee gift card to show my appreciation (if that's not too weird)

print_r($page) worked well...appears post_class isn't being called (see below). No change when SEO and one or two other plugins were disabled.

Below is the un-altered plugin script.
start_el is just before where I'd like to output post_class: immediately after '.$page->ID.' Looking fw to learning how to properly call it and get the output syntax correct. Any help would be greatly appreciated. Will research in the meantime! Thanks for the speedy/generous response.


Code: [ Select ]
class Post_Types_Walker extends Walker
  {

    var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');


    function start_lvl(&$output, $depth = 0, $args = array()) {
      $indent = str_repeat("\t", $depth);
      $output .= "\n$indent<ul class='children'>\n";
    }


    function end_lvl(&$output, $depth = 0, $args = array()) {
      $indent = str_repeat("\t", $depth);
      $output .= "$indent</ul>\n";
    }


    function start_el(&$output, $page, $depth = 0, $args = array(), $id = 0) {
      if ( $depth )
        $indent = str_repeat("\t", $depth);
      else
        $indent = '';

      extract($args, EXTR_SKIP);

      $output .= $indent . '<li id="item_'.$page->ID.'"><span>'.apply_filters( 'the_title', $page->post_title, $page->ID ).'</span>';
    }


    function end_el(&$output, $page, $depth = 0, $args = array()) {
      $output .= "</li>\n";
    }

  }
  1. class Post_Types_Walker extends Walker
  2.   {
  3.     var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID');
  4.     function start_lvl(&$output, $depth = 0, $args = array()) {
  5.       $indent = str_repeat("\t", $depth);
  6.       $output .= "\n$indent<ul class='children'>\n";
  7.     }
  8.     function end_lvl(&$output, $depth = 0, $args = array()) {
  9.       $indent = str_repeat("\t", $depth);
  10.       $output .= "$indent</ul>\n";
  11.     }
  12.     function start_el(&$output, $page, $depth = 0, $args = array(), $id = 0) {
  13.       if ( $depth )
  14.         $indent = str_repeat("\t", $depth);
  15.       else
  16.         $indent = '';
  17.       extract($args, EXTR_SKIP);
  18.       $output .= $indent . '<li id="item_'.$page->ID.'"><span>'.apply_filters( 'the_title', $page->post_title, $page->ID ).'</span>';
  19.     }
  20.     function end_el(&$output, $page, $depth = 0, $args = array()) {
  21.       $output .= "</li>\n";
  22.     }
  23.   }



Code: [ Select ]
WP_Post Object ( [ID] => 685 [post_author] => 1 [post_date] => 2014-03-08 21:28:33 [post_date_gmt] => 0000-00-00 00:00:00 [post_content] => [post_title] => testty [post_excerpt] => [post_status] => draft [comment_status] => closed [ping_status] => open [post_password] => [post_name] => [to_ping] => [pinged] => [post_modified] => 2014-03-08 21:28:33 [post_modified_gmt] => 0000-00-00 00:00:00 [post_content_filtered] => [post_parent] => 0 [guid] => ?post_type=portfolio&p=685 [menu_order] => 0 [post_type] => portfolio [post_mime_type] => [comment_count] => 0 [filter] => raw )
  • b_zilla
  • Novice
  • Novice
  • b_zilla
  • Posts: 15
  • Loc: Seattle, WA

Post 3+ Months Ago

In case it's useful to someone, I solved it with a great deal of help on the wordpress support page by using join()...

Code: [ Select ]
    function start_el(&$output, $page, $depth = 0, $args = array(), $id = 0) {
      if ( $depth )
        $indent = str_repeat("\t", $depth);
      else
        $indent = '';

      extract($args, EXTR_SKIP);

      $output .= $indent . '<li id="item_'.$page->ID.'" class="'.join( " ", get_post_class( "", $page->ID )).'"><span>'.apply_filters( 'the_title', $page->post_title, $page->ID ).'</span>';
    }
  1.     function start_el(&$output, $page, $depth = 0, $args = array(), $id = 0) {
  2.       if ( $depth )
  3.         $indent = str_repeat("\t", $depth);
  4.       else
  5.         $indent = '';
  6.       extract($args, EXTR_SKIP);
  7.       $output .= $indent . '<li id="item_'.$page->ID.'" class="'.join( " ", get_post_class( "", $page->ID )).'"><span>'.apply_filters( 'the_title', $page->post_title, $page->ID ).'</span>';
  8.     }


Thanks for your previous help!

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 68 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.