Met onderstaande code maak je autopost beschikbaar. Eerst voeg je de post toe met de content die je wil zien en vervolgens pak je een afbeelding / font en schrijf je de datum op een afbeelding en voegt deze toe als uitgelichte afbeelding

Post type aanmaken

register_post_type('auto_posts', // Register Custom Post Type
        array(
        'labels' => array(
            'name' => __('Automatische posts', 'wbmz'), // Rename these to suit
            'singular_name' => __('Automatische post', 'wbmz'),
            'add_new' => __('Nieuwe automatische post', 'wbmz'),
            'add_new_item' => __('Voeg nieuwe automatische post toe', 'wbmz'),
            'edit' => __('Wijzig', 'wbmz'),
            'edit_item' => __('Wijzig automatische post', 'wbmz'),
            'new_item' => __('Nieuwe automatische post', 'wbmz'),
            'view' => __('Bekijk automatische post', 'wbmz'),
            'view_item' => __('Bekijk automatische post', 'wbmz'),
            'search_items' => __('Zoek automatische posts', 'wbmz'),
            'not_found' => __('Geen automatische posts gevonden', 'wbmz'),
            'not_found_in_trash' => __('Geen automatische posts gevonden in de prullenbak', 'wbmz')
        ),
        'public' => true,
        'exclude_from_search' => true,
        'show_ui' => true,
        'show_in_nav_menus' => false,
        'show_in_menu' => true,
        'hierarchical' => true,
        'has_archive' => false,
        'menu_icon'   => 'dashicons-admin-links',
        "rewrite" => array( 'slug' => 'autpost', 'with_front'	=> false ),
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'thumbnail',
            'author'
        ),
        'can_export' => true
    ));

Function

//inplannen met cronjob op server | elke dag om 09:00u
function opdrachten_van_de_dag() {
  $timestamp = current_time( 'timestamp' );
  $today = strftime('%Y-%m-%d', $timestamp);
  $yesterday = strftime('%Y-%m-%d', strtotime('-1 day', $timestamp));
  $today_ar = date_parse($today);
  $yesterday_ar = date_parse($yesterday);
  $vandaag = date_i18n( "d F Y", $timestamp );
  $args = array(
    'post_type'		=> 'opdrachten',
    'posts_per_page'	=> -1,
    'facetwp'	=> false,
    'post_status' => 'publish',
    'year' => $yesterday_ar["year"],
    'monthnum' => $yesterday_ar["month"],
    'day' => $yesterday_ar["day"],
    'tax_query'              => array(
      array(
        'taxonomy'         => 'status_opdracht',
        'terms'            => 622,
        'field'            => 'term_id',
      ),
    )
  );

  $my_query = new WP_Query($args);
  if($my_query->have_posts()){
  $post_content = '<div class="container">';
   $post_content .= '<div class="row">';
     $post_content .= '<div class="col-xs-12">';
     $post_content .= '<p>💡Actuele interim opdrachten op KROOTZ ('.$vandaag.') 💡</p>';
     $allevakgebieden = array();
     while ( $my_query->have_posts() ): $my_query->the_post();
      $vakgebieden = get_the_terms( get_the_ID(), 'vakgebieden' );
      $soortcontract = get_the_terms( get_the_ID(), 'soort_opdrachten' );
      $loc = get_the_terms( get_the_ID(), 'locatie_opdracht' );
      $datum = get_field('uiterste_reactie_datum');
      $vandaag = strtotime(date('Ymd'));
      $str_datum = strtotime($datum);
      $startdatum = get_field('start_datum');
      $str_begin = strtotime($startdatum);
      $einddatum = get_field('eind_datum');
      $str_eindd = strtotime($einddatum);
      $startdatumzsm = get_field('start_datum_zsm');
      $publicatiedatum = get_the_date( 'd-m-Y' );
      $titel = get_the_title();
      $pieces = explode("#KRTZ", $titel);
      $post_content .= '' . $pieces[0] . ' #KRTZ' . $pieces[1] . ' | ';
      $functietitelhash = strtolower(str_replace(' ', '', $pieces[0]));
      if ( $loc && ! is_wp_error( $loc ) ) :
        $loc_arr = array();
        foreach ( $loc as $loc_single ) {
          $loc_arr[] = $loc_single->name;
        }
        $locatie = join( ", ", $loc_arr );
      $post_content .= ''.$locatie.'';
      endif;
      $post_content .= "<br/>";
      $post_content .= '✅ ';
      //$post_content .= do_shortcode('[wpbitly post_id="'.get_the_ID().'"]');
      $post_content .= get_the_permalink();
      $post_content .= "<br/><br/>";
     endwhile;
     $post_content .= 'Bekijk alle actuele opdrachten ➡️ <a href="https://krtz.nl/38hR1RV">https://krtz.nl/38hR1RV</a>';
    $post_content .= '</div>';
   $post_content .= '</div>';
  $post_content .= '</div>';
  wp_reset_query();

  $dagnl = strftime("%A");
  $dag = ucfirst($dagnl);
  $dagnummer = date("d");
  $maand = strftime("%B");
  $maandnummer = date("m");
  $jaar = date("Y");
  $datum = date("d-m-Y");
  $text = "{$dagnl} {$dagnummer} {$maand} {$jaar}";

   $post_data = array(
       'post_title' => 'Opdrachten van '.$text.'',
       'post_content' => $post_content,
       'post_status' => 'publish',
       'post_author'   => 4,
       'post_type' => 'auto_posts'
   );
   $post_id = wp_insert_post( $post_data );

   $jpg_image = imagecreatefromjpeg('/opdrachten-vd-dag/opdrachten-vd-dag.jpg');
   $white = imagecolorallocate($jpg_image, 255, 255, 255);
   $font = '/opdrachten-vd-dag/Montserrat-Medium.ttf';
   imagettftext($jpg_image, 40, 0, 128, 900, $white, $font, $text);
    // opslaan
    $pathname = '/opdrachten-vd-dag/' . date("Y") . '/' . date("m") . '/';
    if (!is_dir($pathname)) {
      mkdir($pathname, 0777, true);
    }
    $bestand_naam_pad = "{$pathname}/opdrachten-vd-dag-{$datum}.jpg";
    $url_bestand = "https://www.krootz-zzp.nl/{$bestand_naam_pad}";
    imagejpeg($jpg_image, $bestand_naam_pad, 100);
    imagedestroy($jpg_image);

    // Voeg afbeelding toe als uitgelichte afbeelding
     $upload_dir = wp_upload_dir();
     $image_data = file_get_contents($url_bestand);
     $filename = basename($url_bestand);
     if(wp_mkdir_p($upload_dir['path'])):
       $file = $upload_dir['path'] . '/' . $filename;
     else:
       $file = $upload_dir['basedir'] . '/' . $filename;
     endif;
     file_put_contents($file, $image_data);

     $wp_filetype = wp_check_filetype($filename, null );
     $attachment = array(
         'post_mime_type' => $wp_filetype['type'],
         'post_title' => sanitize_file_name($filename),
         'post_content' => '',
         'post_status' => 'inherit'
     );
     $attach_id = wp_insert_attachment( $attachment, $file, $post_id );
     require_once(ABSPATH . 'wp-admin/includes/image.php');
     $attach_data = wp_generate_attachment_metadata( $attach_id, $file );
     wp_update_attachment_metadata( $attach_id, $attach_data );
     set_post_thumbnail( $post_id, $attach_id );
 } else {
   //echo 'geen opdrachten';
 };

}
Geef een reactie 0