custom-modern-theme/functions.php

187 lines
6.3 KiB
PHP
Raw Permalink Normal View History

2024-07-31 20:14:58 +02:00
<?php
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
// Load child theme setup first.
require_once trailingslashit( get_stylesheet_directory() ) . 'setup.php';
/**
* IMPORTANT:
* Do not remove the code above!
* Put your custom PHP code below.
*
* Child theme functionality:
*/
/**
* Enqueuing stylesheet file to editor.
*
* Remove this when not needed.
*/
add_action( 'init', function() {
// Add `style.css` file to editor.
add_editor_style(
esc_url_raw(
add_query_arg(
'ver',
'v' . WebManDesign\Child_Theme\Setup::$stylesheet_filemtime,
get_stylesheet_uri()
)
)
);
}, 99 );
// Cutoms Post Type 'Projekte'
// Made by Lino and Flo
function create_project_post_type() {
$labels = array(
'name' => __( 'Projekte' ),
'singular_name' => __( 'Projekt' ),
'add_new' => __( 'Neues Projekt hinzufügen' ),
'add_new_item' => __( 'Neues Projekt hinzufügen' ),
'edit_item' => __( 'Projekt bearbeiten' ),
'new_item' => __( 'Neues Projekt' ),
'view_item' => __( 'Projekt anzeigen' ),
'search_items' => __( 'Projekte durchsuchen' ),
'not_found' => __( 'Keine Projekte gefunden' ),
'not_found_in_trash' => __( 'Keine Projekte im Papierkorb gefunden' ),
);
$args = array(
'labels' => $labels,
'public' => true,
'has_archive' => true,
'menu_icon' => 'dashicons-portfolio',
'supports' => array( 'title', 'editor', 'thumbnail', 'custom-fields' ),
/*'taxonomies' => array( 'category', 'post_tag' ),*/
/*commented the line above to disable categorys and post tags. Uncomment this line if indeed needed to have tags or categories*/
'rewrite' => array( 'slug' => 'projekte' ),
'show_in_menu' => true,
'show_in_nav_menus' => true,
);
register_post_type( 'projekte', $args );
}
add_action( 'init', 'create_project_post_type' );
function add_project_meta_boxes() {
add_meta_box(
'project_details',
__( 'Projekt Details' ),
'project_details_callback',
'projekte',
'normal',
'default'
);
}
add_action( 'add_meta_boxes_projekte', 'add_project_meta_boxes' );
function project_details_callback( $post ) {
wp_nonce_field( 'project_details', 'project_details_nonce' );
$projekt_name = get_post_meta( $post->ID, '_projekt_name', true);
$projekt_website = get_post_meta( $post->ID, '_projekt_website', true );
$projekt_description = get_post_meta( $post->ID, '_projekt_description', true );
$projekt_since = get_post_meta( $post->ID, '_projekt_since', true );
$projekt_links = get_post_meta( $post->ID, '_projekt_links', true);
?>
<p>
<label for="projekt_name"><?php _e( 'Name' ); ?></label>
<input type="text" name="projekt_name" id="projekt_name" value="<?php echo esc_attr( $projekt_name ); ?>">
</p>
<p>
<label for="projekt_website"><?php _e( 'Website' ); ?></label>
<input type="url" name="projekt_website" id="projekt_website" value="<?php echo esc_attr( $projekt_website ); ?>">
</p>
<p>
<label for="projekt_description"><?php _e( 'Beschreibung' ); ?></label>
<textarea name="projekt_description" id="projekt_description"><?php echo esc_textarea( $projekt_description ); ?></textarea>
</p>
<p>
<label for="projekt_links"><?php _e( 'Weiterer Beschreibungstext' ); ?></label>
<textarea name="projekt_links" id="projekt_links"><?php echo esc_textarea( $projekt_links ); ?></textarea>
</p>
<p>
<label for="projekt_since"><?php _e( 'Dabei seit' ); ?></label>
<input type="date" name="projekt_since" id="projekt_since" value="<?php echo esc_attr( $projekt_since ); ?>">
</p>
<?php
}
function save_project_meta( $post_id ) {
if ( ! isset( $_POST['project_details_nonce'] ) || ! wp_verify_nonce( $_POST['project_details_nonce'], 'project_details' ) ) {
return;
}
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
if ( isset( $_POST['post_type'] ) && 'projekte' == $_POST['post_type'] ) {
if ( current_user_can( 'edit_post', $post_id ) ) {
if ( isset( $_POST['projekt_name'] ) ) {
update_post_meta( $post_id, '_projekt_name', sanitize_text_field( $_POST['projekt_name'] ) );
}
if ( isset( $_POST['projekt_website'] ) ) {
update_post_meta( $post_id, '_projekt_website', sanitize_text_field( $_POST['projekt_website'] ) );
}
if ( isset( $_POST['projekt_description'] ) ) {
update_post_meta( $post_id, '_projekt_description', sanitize_textarea_field( $_POST['projekt_description'] ) );
}
if ( isset( $_POST['projekt_links'] ) ) {
update_post_meta( $post_id, '_projekt_links', sanitize_textarea_field( $_POST['projekt_links'] ) );
}
if ( isset( $_POST['projekt_since'] ) ) {
update_post_meta( $post_id, '_projekt_since', sanitize_text_field( $_POST['projekt_since'] ) );
}
/*
if ( isset( $_POST['projekt_image'] ) ) {
update_post_meta( $post_id, '_image_id', $_POST['upload_image_id'] );
// update_post_meta( $post_id, '_projekt_image', sanitize_text_field( $_POST['projekt_image'] ) );
}*/
}
}
}
add_action( 'save_post_projekte', 'save_project_meta' );
/*Dieser Code Abschnitt deaktiviert den Editor (und die Felder für categories, tags und custom-fields) fuer den Custom Post Type "Projekte"*/
add_action( 'init', 'remove_editor_init' );
function remove_editor_init() {
remove_post_type_support( 'projekte', 'editor' );
remove_post_type_support( 'projekte', 'categories' );
remove_post_type_support( 'projekte', 'tags' );
remove_post_type_support( 'projekte', 'custom-fields' );
}
/*Dieser Code Abschnitt disabled das featured Image als Hintergrund*/
/*
add_action('wp_enqueue_scripts', 'custom_post_type_css');
function custom_post_type_css() {
if (is_singular('Projekte')) {
wp_enqueue_style('custom-post-type-projekte-css', get_stylesheet_directory_uri() . '/custom-post-type-projekte.css');
}
}
// oder:
add_action('after_setup_theme', 'custom_theme_setup');
function custom_theme_setup() {
add_theme_support('post-thumbnails');
add_image_size('custom_image_size', 10, 5, true); // Replace with your desired dimensions
}*/
/*
add_action('after_setup_theme', 'disable_featured_image');
function disable_featured_image() {
remove_theme_support('post-thumbnails', 'Projekte');
}*/