mardi 2 novembre 2021

wordpress create modal popup based on post id

i have a plugin which creates a modal popup on the front page. currently i am hard coding the id of the post into the details because i have struggled to get the id of that specific post. i have managed to find the id but it only finds the correct id within the modal_add_custom_box function. i need to use the value for $id in the display_modal function. any ideas?

<?php
add_theme_support( 'post_thumbnail' );
add_post_type_support( 'modal', 'thumbnail' );

// modal Custom Post Type
function modal_init() {
  // set up modal labels
  $labels = array(
      'name' => 'Modal',
      'singular_name' => 'Modal',
      'add_new' => 'Add New Modal',
      'add_new_item' => 'Add New Modal',
      'edit_item' => 'Edit Modal',
      'new_item' => 'New Modal',
      'all_items' => 'All Modals',
      'view_item' => 'View Modals',
      'not_found' =>  'No Products Found',
      'not_found_in_trash' => 'No Products found in Trash',
      'parent_item_colon' => '',
      'menu_name' => 'Modal',
  );

  // register post type
  $args = array(
      'labels' => $labels,
      'public' => true,
      'has_archive' => true,
      'show_ui' => true,
      'capability_type' => 'post',
      'hierarchical' => false,
      'query_var' => true,
      'menu_icon' => 'dashicons-randomize',
      'supports' => array(
          'title',
          'thumbnail',
          'page-attributes',
      )
  );

  register_post_type( 'Modal', $args );
}
add_action( 'init', 'Modal_init' );

register_post_type( $post_type, $args );

//add meta box

function Modal_add_custom_box() {
  $screens = [ 'Modal' ];
  foreach ( $screens as $screen ) {
      add_meta_box(
          'Modal_box',                 // Unique ID
          'Modal Info',      // Box title
          'Modal_custom_box_html',  // Content callback, must be of type callable
          $screen                            // Post type
      );
  }
}
add_action( 'add_meta_boxes', 'Modal_add_custom_box' );

function Modal_custom_box_html() {
  //check if values are in database, if they are display them in the backend form
  $headline = get_post_meta(1408, 'headline', true);
  $sub_headline = get_post_meta(1408, 'sub_headline', true );
  $button_text = get_post_meta( 1408, 'button_text', true );
  $destination_url = get_post_meta(1408, 'destination_url', true);
  $id = get_the_ID();

  if(empty($headline)){
    $headline_display = "";
  }else{
    $headline_display = $headline;
  }
  if(empty($sub_headline)){
    $sub_headline_display = "";
  }else{
    $sub_headline_display = $button_text;
  }
  if(empty($button_text)){
    $button_text_display = "";
  }else{
    $button_text__display = $button_text;
  }
  ?>
  <P>
    If fields are left empty, nothing will display on modal box for that field
  </P>


  <div class="user-cont" id="user-cont">
    <div class="input-group">
      <label for="headline_field">Headline</label>
      <input type="text" name="headline" id="headline" value="<?php echo $headline_display; ?>"></input>
    </div>
    <div class="input-group">
      <label for="sub_headline_field">Sub-Headline</label>
      <input type="text" name="sub_headline" id="sub_headline" value="<?php echo $sub_headline_display; ?>"></input>
    </div>
    <div class="input-group">
      <label for="button_text_field">Button-Text</label>
      <input type="text" name="button_text" id="button_text" value="<?php echo $button_text; ?>"></input>
    </div>
    <div class="input-group">
      <label foor="desination">Destination URL</label>
      <input type="text" name="destination_url" id="destination_url"></input>
    </div>
    <div class="input-group checkbox">
      <label for="URL_tab">Open URL in new tab?</label>
      <select name="URL_tab">
        <option name="yes" id="yes">yes</option>
        <option name="no" id="no">no</option>
      </select>
    </div>
    <div class="input-group">
      <label for="date_live">date popup to go live</label>
      <input type="date" name="date_live" id="date_live"></input>
    </div>
    <div class="input-group">
      <label for="date_close">date popup to close</label>
      <input type="date" name="date_close" id="date_close"></input>
    </div>
    <div class="input-group">
      <input type="text" name="id" id="id" value="<?php echo $id; ?>"></input>
    </div>
  </div>

  <?php
  return $id;
}

//save meta data


function Modal_save_meta_box( $post_id ) {
  if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
  if ( $parent_id = wp_is_post_revision( $post_id ) ) {
      $post_id = $parent_id;
  }




  $fields = [
      'headline',
      'sub_headline',
      'button_text',
      'date_live',
      'date_close',
      'destination_url',
      'URL_tab',
      'yes',
      'no',
      'id'
  ];

  foreach ( $fields as $field ) {
      if ( array_key_exists( $field, $_POST ) ) {
          update_post_meta( $post_id, $field, sanitize_text_field( $_POST[$field] ) );
      }
   }
}

  add_action( 'save_post', 'Modal_save_meta_box' );

//display custom field on front page

function display_modal(){
$post_id = get_the_ID();
$headline = get_post_meta(1408, 'headline', true);
$sub_headline = get_post_meta(1408, 'sub_headline', true );
$button_text = get_post_meta( 1408, 'button_text', true );
$destination_url = get_post_meta(1408, 'destination_url', true);
$selected_yes = get_post_meta(1408, 'url_tab', true);
$featured_image =  wp_get_attachment_image_src( get_post_thumbnail_id( 1408 ));


if($selected_yes == 'yes'){
  $url_target= 'target="_blank" ';
}else{
  $url_target= 'target="_self" ';
}

echo $post_id;

?>

<div class="modal style-accent-bg <?php echo $color_opacity; ?>" id="modal">
  <button class="close" id="close"></button>
  <div class="img">
    <img src="<?php echo $featured_image[0]; ?>" class="modal_image">
  </div>
  <div class="details">
    <h1><?php echo $headline; ?></h1>
    <h2><?php echo $sub_headline; ?></h2>
    <p>



    </p>
    <a href="<?php echo $destination_url ?>" <?php echo $url_target ?>class="button-link btn btn-default color-accent-color style-color-gyho-bg"><?php echo $button_text;?></a>
  </div>
</div>
<?php

}


function check_page(){
  if(is_front_page()){
    display_modal();
  }else{
    ;
  }
}
?>



Aucun commentaire:

Enregistrer un commentaire