• Resolved jszuslik

    (@jszuslik)


    Hello,

    I have created a custom meta box for a check box. The meta value is posting to the database but the value in the post is not being reflected as to what is written in the database.

    What am I doing wrong? Here is my code

    function upaf_em_meta_boxes() {
      add_meta_box('em_smartcard_box', 'Is This a SMART Card Event?', 'em_smartcard_box', EM_POST_TYPE_EVENT, 'side', 'high');
    }
    add_action('add_meta_boxes', 'upaf_em_meta_boxes');
    
    function em_smartcard_box() {
      global $EM_Event;
      $input = '';
      $input .= '<label>';
      $input .= '<input type="checkbox" name="smartcardevent" value="Yes" />';
      $input .= ' Yes</label><br>';
      echo $input;
    }
    
    add_filter('em_event_save', 'upaf_em_smartcard_box_save', 1,2);
    function upaf_em_smartcard_box_save($result,$EM_Event) {
      global $wpdb;
      if( $result && !empty($_POST['smartcardevent']) ){
        $is_sc_event = $_POST['smartcardevent'];
        $sc_event = "({$EM_Event->id}, 'sc_event', '$is_sc_event')";
        $wpdb->query("DELETE FROM ".EM_META_TABLE." WHERE object_id='{$EM_Event->id}' AND meta_key='sc_event'");
        $wpdb->query("INSERT INTO ".EM_META_TABLE." (object_id, meta_key, meta_value) VALUES ".$sc_event);
      } elseif( $result && empty($_POST['smartcardevent']) ){
        $is_sc_event = $_POST['smartcardevent'];
        $sc_event = "({$EM_Event->id}, 'sc_event', '$is_sc_event')";
        $wpdb->query("DELETE FROM ".EM_META_TABLE." WHERE object_id='{$EM_Event->id}' AND meta_key='sc_event'");
        $wpdb->query("INSERT INTO ".EM_META_TABLE." (object_id, meta_key, meta_value) VALUES ".$sc_event);
      }
      return $result;
    }
    
    function upaf_em_smartcard_box_load($EM_Event){
      global $wpdb;
      $EM_Event->sc_event = $wpdb->get_col("SELECT meta_value FROM ".EM_META_TABLE." WHERE object_id='{$EM_Event->event_id}' AND meta_key='sc_event'", 0);
    }
    add_action('em_event', 'upaf_em_smartcard_box_load');

    https://www.ads-software.com/plugins/events-manager/

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter jszuslik

    (@jszuslik)

    Basically if I save the checkbox as being checked it is not showing that it is checked when reloaded but in the database it has a value of ‘Yes’

    Plugin Support angelo_nwl

    (@angelo_nwl)

    in your EM save filter/hook, you can try to use update_post_meta() as well

    eg.

    update_post_meta($EM_Event->post_id, 'yourvarname', 'value');

    Thread Starter jszuslik

    (@jszuslik)

    That didn’t work.

    I found a solution where I added
    $checked = $wpdb->get_results("SELECTmeta_valueFROM ".EM_META_TABLE." WHERE object_id='".$EM_Event->event_id."' AND meta_key='sc_event'");

    To the em_smartcard_box function. This is my final code. I also chacnged the check boxed to radio buttons.

    function em_smartcard_box() {
      global $wpdb;
      global $EM_Event;
      $checked = $wpdb->get_results("SELECT <code>meta_value</code> FROM ".EM_META_TABLE." WHERE object_id='".$EM_Event->event_id."' AND meta_key='sc_event'");
      // var_dump($checked);
      if ($checked){
        foreach ($checked as $check) {
          if ($check->meta_value == 'Yes'){
            $input = '';
            $input .= '<label>';
            $input .= '<input type="radio" name="smartcardevent" value="Yes" checked />';
            $input .= ' Yes</label><br>';
            $input .= '<label>';
            $input .= '<input type="radio" name="smartcardevent" value="No" />';
            $input .= ' No</label><br>';
            echo $input;
          } elseif ($check->meta_value == 'No'){
            $input = '';
            $input .= '<label>';
            $input .= '<input type="radio" name="smartcardevent" value="Yes"/>';
            $input .= ' Yes</label><br>';
            $input .= '<label>';
            $input .= '<input type="radio" name="smartcardevent" value="No" checked  />';
            $input .= ' No</label><br>';
            echo $input;
          }
        }
      } else {
        $input = '';
        $input .= '<label>';
        $input .= '<input type="radio" name="smartcardevent" value="Yes" checked />';
        $input .= ' Yes</label><br>';
        $input .= '<label>';
        $input .= '<input type="radio" name="smartcardevent" value="No" />';
        $input .= ' No</label><br>';
        echo $input;
      }
    }

    Great, thanks for sharing your code; might help someone else.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Custom Meta Box Values’ is closed to new replies.