0

I would appreciate any criticism for the big chunk of code below. What I'm trying to do is query a set of custom posts and return them in a table based on custom fields. It works ... the table is being generated, the right posts are being returned, but after executing a query, all other dashboard pages are blank.

//add menu option
add_action( 'admin_menu', 'my_plugin_menu' );

function my_plugin_menu() {
    add_options_page( 'Limitless Options', 'Limitless', 'manage_options', 'limitless', 'my_plugin_options' );
}

function my_plugin_options() {
        if ( !current_user_can( 'manage_options' ) )  {
            wp_die( __( 'You do not have sufficient permissions to access this page.' ) );
        }
if( isset($_GET['set']) ) {

//variables
$checkk = array();
$meta = NULL;
$date = NULL;

    //validate
    $start = ($_GET['start'] != NULL ? $_GET['start'] : false);
    $end = ($_GET['end'] != NULL ? $_GET['end'] : false);
    $question = ($_GET['question'] != NULL ? $_GET['question'] : false);
    $answer = ($_GET['answer'] != NULL ? $_GET['answer'] : false);
    $greutate_actuala = ($_GET['greutate_actuala'] != NULL ? $_GET['greutate_actuala'] : false);
    $greutate_dorita = ($_GET['greutate_dorita'] != NULL ? $_GET['greutate_dorita'] : false);
    $check = ($_GET['check'] != NULL ? $_GET['check'] : false);

    $startEx = explode("-", $start);
    $endEx = explode("-", $end);

    if($greutate_actuala) {
      $greutate_a = array(
                 'key' => 'greutate_actuala',
                 'value' => $greutate_actuala,      
      );
    }else{
      $greutate_a = NULL;
    }

    if($greutate_dorita) {
      $greutate_d = array(
                 'key' => 'greutate_dorita',
                 'value' => $greutate_dorita,
      );
    }else{
      $greutate_d = NULL;
    }

    if($check) {
      $checkk = array(
                  'key' => 'check',
                  'value' => 1,
      );
    }

    if($question && $answer) {
      $meta = array(
                'key' => $question,
                'value' => $answer,
            );
    }
    if($question && !$answer) {
      $meta = array(
                'key' => $question,
            );
    }
    if(!$question && $answer){
        $meta = array(
                'value' => $answer,
            );
    }

    if($start && $end) {

    echo "start and end";

        $date = array(
                'after' => array(
                'year'  => $startEx[0],
                'month' => $startEx[1],
                'day'   => $startEx[2],
            ),

                'before' => array(
                'year'  => $endEx[0],
                'month' => $endEx[1],
                'day'   => $endEx[2],
            ),

            'inclusive' => true,
        );

    }
    if($start && !$end) {

    echo "start not end";

        $date = array(
            'year'  => $startEx[0],
            'month' => $startEx[1],
            'day'   => $startEx[2],
        );

    }

    $args = array(

        'post_type' => 'quiz',
        'meta_query' => array(
            $meta,
            $greutate_a,
            $greutate_d,
            $checkk,
        ),
        'date_query' => array(
            $date
        ),

    );

    $loop = new WP_Query( $args );

    echo '<table border="1" class="table">
            <tr>
              <td>Nume</td>
              <td>Email</td>
         ';

     for($i=1; $i<=10; $i++) {
        echo '<td '.(($_GET['question'] === 'question_'.$i.'') ? 'class="active"' : "").'>Question '.$i.'</td>';
     }

    echo '<td '.(($greutate_actuala) ? 'class="active"' : "").'>GA</td>
          <td '.(($greutate_dorita) ? 'class="active"' : "").'>GD</td>
          <td '.(($check) ? 'class="active"' : "").'>Check</td>
          </tr>';

    while ( $loop->have_posts() ) : $loop->the_post();
      echo '<tr>';
        echo "<td>".get_post_meta(get_the_ID(), 'name', true)."</td>";
        echo "<td>".get_post_meta(get_the_ID(), 'email', true)."</td>";

        for($i=1; $i <= 10; $i++) {
            echo "<td>".get_post_meta(get_the_ID(), 'question_'.$i.'', true)."</td>";
        }

        echo "<td>".get_post_meta(get_the_ID(), 'greutate_actuala', true)."</td>";
        echo "<td>".get_post_meta(get_the_ID(), 'greutate_dorita', true)."</td>";
        echo "<td>".get_post_meta(get_the_ID(), 'check', true)."</td>";
      echo '</tr>';

    $idArr[] = get_the_ID();

    endwhile;

    echo '</table>';

    if(isset($_POST['csv'])) {
      csvDownload($idArr);
    }

    if(isset($_POST['dl-csv'])) {
      email_csv();
    }

    echo '<form method="post" action="">
              <input type="submit" name="csv" value="csv">
          </form>
         ';

    if(isset($_POST['csv'])) {

        echo '<form method="post" action="">
              <p>CSV to: '.get_option( 'admin_email' ).' Add more: <input type="email" name="email"> ex: [email protected], [email protected]</p>
              <input type="submit" name="dl-csv" value="Send CSV">
              </form>
         ';
    }

}
8
  • Where are you executing this code?
    – Welcher
    Commented Dec 17, 2014 at 21:07
  • I edited my question. I m creating an admin menu. I want the administrator to do everything in the dashboard.
    – Ciprian
    Commented Dec 17, 2014 at 21:16
  • Forgot to mention ... after the blank dashboard, if I visit a page ... let's say the home page and access the dashboard from there, it works.
    – Ciprian
    Commented Dec 17, 2014 at 21:27
  • are you seeing any errors in a debug_log? Honestly - this could be a bunch of things. You're not calling wp_reset_postdata() after your WP_QUERY loop for starters.
    – Welcher
    Commented Dec 17, 2014 at 21:29
  • It's not my server ... I m actually trying to enable error logging as we speak ... define('WP_DEBUG', true); define('WP_DEBUG_LOG', true); in wp-config.php ... but no error log.
    – Ciprian
    Commented Dec 17, 2014 at 21:36

1 Answer 1

0

Not sure if this is the actual answer for this but on line 252 of the pastebin link change:

 <form action="'.site_url().'/wp-admin/admin.php/options-general.php" method="get">

to

 <form action="'.admin_url('options-general.php').'" method="get">

When I submitted the form in the settings it giving me a not found error.

4
  • If you see anything that can be improved ... please let me know.
    – Ciprian
    Commented Dec 17, 2014 at 22:08
  • Glad to help. You may also want to do some sanity checks on your arrays. Wrapping a them in isset() will allow you to be sure the index exists before trying to use it. Otherwise, you debug log will fill up with pho notices.
    – Welcher
    Commented Dec 17, 2014 at 23:49
  • I will. Thank you. I've still got a problem ... where should I generate the csv file? fopen is giving a failed to open stream: Permission denied message. I need to generate the csv in a folder where it won't be public, and email it whenever someone wishes.
    – Ciprian
    Commented Dec 18, 2014 at 5:19
  • Is start a new question for that.
    – Welcher
    Commented Dec 18, 2014 at 14:01

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.