1

I'm using PHP Exec to build a widget to show a list of upcoming events:

<?php
function filter_whene($whene = '') {
    $whene .= " AND post_date >= '" . date('Y-m-d') . "' ";
return $whene;
  }
add_filter('posts_whene', 'filter_whene');
query_posts('cat=10&showposts=3&');
if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
            <li><?php the_date(); ?> <a href="<?php the_permalink(); ?>">
  <?php the_title(); ?>
  </a>  </li>
<?php endwhile;
else: ?><p>Não há eventos agendados.</p>
<?php endif; ?>
<?php remove_filter('posts_whene', 'filter_whene'); ?>
<?php wp_reset_query(); ?>

It seems to work on the front end but I get:

Fatal error: Cannot redeclare filter_whene() (previously declared in /home/content/a/c/a/acamorg/html/cea2/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()'d code:3) in /home/content/a/c/a/acamorg/html/cea2/wp-content/plugins/exec-php/includes/runtime.php(42) : eval()'d code on line 6

on the widget back-end.

Any suggestions?

Thank you.

2 Answers 2

1

Hi @user2816:

It would seem you are using that code in more than one widget? Try changing part of that code from these:

function filter_whene($whene = '') {
  $whene .= " AND post_date >= '" . date('Y-m-d') . "' ";
  return $whene;
}
add_filter('posts_whene', 'filter_whene');

To this:

if (!function_exists('filter_whene')) {
  function filter_whene($whene = '') {
    $whene .= " AND post_date >= '" . date('Y-m-d') . "' ";
    return $whene;
  }
}
add_filter('posts_whene', 'filter_whene');

Better, move as much of your code as possible to your theme's functions.php file. Wrap it in a function and then you only have to type one line of code in your PHP Exec widget.

Or better yet, get rid of that plugin completely and write your own widget; because PHP Exec is evil.

P.S. Okay, PHP Exec is not evil per se, but once you try to do anything more than trivial it creates more problems than it solves.

1
  • Thank you. It worked! I'm not using the code in more than one widget and yes, I had already looked for Exec PHP alternatives, as I'm always wary that will break something that I won't know how to fix. Your "create your own widget" suggestion is perfect, of course!
    – AnaRita
    Commented Jan 30, 2011 at 13:15
0

This doesn't directly answered your question. I use this snippet on my sidebar, to get future posts:

<ul>
<?php
query_posts('post_status=future&order=asc&orderby=date');
//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
  echo "<li>";
  echo the_date();
  echo "<br />";
  echo the_title();
  echo"</li>";
endwhile; else:
   echo "<li>there's no future post at the moment.</li>";
endif;
?>
</ul>
1
  • I already went through the "post_status=future" solution (and a meta_value Date one). But then non logged in users could not see the related post. Meta_value would not (I know from experience) be very user friendly. I now use "Future is Now" plugin (which invalidates the future posts solution, as all upcoming events will have a published state), and the above piece of code. I don't need to schedule future posts, so this is the right solution for me.
    – AnaRita
    Commented Jan 30, 2011 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.