Großer Auftritt - kleiner Preis?Mehr erfahren

WordPress und Woocommerce Snippets für Deine Seite (Teil 1)

Kommentar Funktion deaktivieren

Manche Seiten und Shops benötigen die Kommentar Funktion von WordPress nicht. In vielen Fällen ist das einer der Hauptangriffspunkte für Hacker und Spammer. Mit diesem Skript deaktivierst Du die Kommentar Funktion komplett.

add_action('admin_init', function () {
    // Redirect any user trying to access comments page
    global $pagenow;
    
    if ($pagenow === 'edit-comments.php') {
        wp_redirect(admin_url());
        exit;
    }

    // Remove comments metabox from dashboard
    remove_meta_box('dashboard_recent_comments', 'dashboard', 'normal');

    // Disable support for comments and trackbacks in post types
    foreach (get_post_types() as $post_type) {
        if (post_type_supports($post_type, 'comments')) {
            remove_post_type_support($post_type, 'comments');
            remove_post_type_support($post_type, 'trackbacks');
        }
    }
});

// Close comments on the front-end
add_filter('comments_open', '__return_false', 20, 2);
add_filter('pings_open', '__return_false', 20, 2);

// Hide existing comments
add_filter('comments_array', '__return_empty_array', 10, 2);

// Remove comments page in menu
add_action('admin_menu', function () {
    remove_menu_page('edit-comments.php');
});

// Remove comments links from admin bar
add_action('init', function () {
    if (is_admin_bar_showing()) {
        remove_action('admin_bar_menu', 'wp_admin_bar_comments_menu', 60);
    }
});


// Removes from admin menu
add_action( 'admin_menu', 'my_remove_admin_menus' );
function my_remove_admin_menus() {
    remove_menu_page( 'edit-comments.php' );
}
// Removes from post and pages
add_action('init', 'remove_comment_support', 100);

function remove_comment_support() {
    remove_post_type_support( 'post', 'comments' );
    remove_post_type_support( 'page', 'comments' );
}
// Removes from admin bar
function mytheme_admin_bar_render() {
    global $wp_admin_bar;
    $wp_admin_bar->remove_menu('comments');
}
add_action( 'wp_before_admin_bar_render', 'mytheme_admin_bar_render' );

Woocommerce Scripte auf nicht Woocommerce Seiten deaktivieren

Woocommerce lädt standardmäßig all seine Scripte auf allen Seiten. In Punkto Seiten Performance kann das sich negativ auswirken. Mit diesem Script deaktivierst Du die Scripte.

/**
 * Callback function that returns true if the current page is a WooCommerce page or false if otherwise.
 *
 * @return boolean true for WC pages and false for non WC pages
 */
function is_wc_page() {
	return class_exists( 'WooCommerce' ) && ( is_woocommerce() || is_cart() || is_checkout() || is_account_page() );
}

add_action( 'template_redirect', 'conditionally_remove_wc_assets' );
/**
 * Remove WC stuff on non WC pages.
 */
function conditionally_remove_wc_assets() {
	// if this is a WC page, abort.
	if ( is_wc_page() ) {
		return;
	}

	// remove WC generator tag
	remove_filter( 'get_the_generator_html', 'wc_generator_tag', 10, 2 );
	remove_filter( 'get_the_generator_xhtml', 'wc_generator_tag', 10, 2 );

	// unload WC scripts
	
	remove_action( 'wp_print_scripts', [ WC_Frontend_Scripts::class, 'localize_printed_scripts' ], 5 );
	remove_action( 'wp_print_footer_scripts', [ WC_Frontend_Scripts::class, 'localize_printed_scripts' ], 5 );

	// remove "Show the gallery if JS is disabled"
	remove_action( 'wp_head', 'wc_gallery_noscript' );

	// remove WC body class
	remove_filter( 'body_class', 'wc_body_class' );
}

add_filter( 'woocommerce_enqueue_styles', 'conditionally_woocommerce_enqueue_styles' );
/**
 * Unload WC stylesheets on non WC pages
 *
 * @param array $enqueue_styles
 */
function conditionally_woocommerce_enqueue_styles( $enqueue_styles ) {
	return is_wc_page() ? $enqueue_styles : array();
}
add_action( 'wp_enqueue_scripts', 'conditionally_wp_enqueue_scripts' );
/**
 * Remove inline style on non WC pages
 */
function conditionally_wp_enqueue_scripts() {
	if ( ! is_wc_page() ) {
		wp_dequeue_style( 'woocommerce-inline' );
	}
}

// add_action( 'init', 'remove_wc_custom_action' );
function remove_wc_custom_action(){
	remove_action( 'wp_head', 'wc_gallery_noscript' );
}
add_filter( 'woocommerce_admin_disabled', '__return_true' );

Breadcrumbs anpassen

Breadcrumbs sind eine wichtige visuelle Unterstützung für User. Aber auch die Suchmaschinen lesen den Wert der Breadcrumb Navigation aus. Daher ist es wichtig, dass die Breadcrumbs auch Deinen Inhalt widerspiegeln. Mit diesem Script kannst Du das verhalten der Breadcrumbs steuern.

/**
 * Replace the home link URL
 */
add_filter( 'woocommerce_breadcrumb_home_url', 'woo_custom_breadrumb_home_url' );
function woo_custom_breadrumb_home_url() {
    return '/hier-steht-dein-individueller-permalink-name/';
}

/**
 * Rename "home" in breadcrumb
 */
add_filter( 'woocommerce_breadcrumb_defaults', 'wcc_change_breadcrumb_home_text' );
function wcc_change_breadcrumb_home_text( $defaults ) {
    // Change the breadcrumb home text from 'Home' to 'Apartment'
	$defaults['home'] = 'Shop';
	return $defaults;
}

/**
 * Change the breadcrumb separator
 */
add_filter( 'woocommerce_breadcrumb_defaults', 'wcc_change_breadcrumb_delimiter' );
function wcc_change_breadcrumb_delimiter( $defaults ) {
	// Change the breadcrumb delimeter from '/' to '>'
	$defaults['delimiter'] = ' » ';
	return $defaults;
}

Excerpt (Textauszug) Länge

Wenn Du mit automatischen Textauszügen arbeitest, und dir dir Länge des Excepts nicht passt, kannst DU das mit dem Script anpassen.

add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

function custom_excerpt_length( $length ) {

    return 20; // 20 steht für 20 Worte im Textauszug

}

Automatische Cross Sells im Warenkorb

Du möchtest nicht bei jedem Produkt ein Cross-Sell Produkt angeben? Mit dem Script passiert das automatisch.

function my_cart_crosssell_ids( $cross_sells, $cart ) {

    // Set emtpy array
    $product_cats_ids = array();
    $product_cats_ids_unique = array();

    foreach ( $cart->get_cart() as $cart_item ) {       
        // Get product id
        $product_id = $cart_item['product_id'];

        // Get current product categories id(s) & add to array
        $product_cats_ids = array_merge( $product_cats_ids, wc_get_product_term_ids( $product_id, 'product_cat' ) );
    }

    // Not empty
    if ( !empty( $product_cats_ids ) ) {
        // Removes duplicate values
        $product_cats_ids_unique = array_unique( $product_cats_ids, SORT_REGULAR );

        // Get product id(s) from a certain category, by category-id
        $product_ids_from_cats_ids = get_posts( array(
            'post_type' => 'product',
            'numberposts' => 4,
            'post_status' => 'publish',
            'fields' => 'ids',
            'tax_query' => array(
                array(
                    'taxonomy' => 'product_cat',
                    'field' => 'id',
                    'terms' => $product_cats_ids_unique,
                    'operator' => 'IN',
                )
            ),
        ) );

        // Removes duplicate values
        $cross_sells = array_unique( $product_ids_from_cats_ids, SORT_REGULAR );
    }

    return $cross_sells;
}
add_filter( 'woocommerce_cart_crosssell_ids', 'my_cart_crosssell_ids', 10, 2 );

Produkt Badge "NEU"

Ja es gibt unzählige PlugIns welche Produkt Badges anbieten. Aber warum ein PlugIn installieren, wenn man es auch selbst coden und mit CSS stylen kann?

// New badge for recent products (archive)
add_action( 'woocommerce_before_shop_loop_item_title', 'new_badge', 3 );
          
function new_badge() {
   global $product;
   $newness_days = 30; // Number of days the badge is shown
   $created = strtotime( $product->get_date_created() );
   if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created ) {
      echo '<span class="ct-woo-card-extra new-badge">' . esc_html__( 'NEU', 'woocommerce' ) . '</span>';
   }
}

// New badge for recent products on single product page
add_action( 'woocommerce_single_product_summary', 'new_badge_single_product', 1 );  
function new_badge_single_product() {
   global $product;
   $newness_days = 30; // Number of days the badge is shown
   $created = strtotime( $product->get_date_created() );
   if ( ( time() - ( 60 * 60 * 24 * $newness_days ) ) < $created ) {
      echo '<span class="itsnew">' . esc_html__( 'NEU', 'woocommerce' ) . '</span>';
   }
}
WordPress und Woocommerce Snippets für Deine Seite (Teil 1) -Skripte -Boost my Shop

Badge "Gratis Versand"

Zeige eine Badge auf Deinen Produkten welche keine Versandkosten haben.
In der Produkt-Konfiguration erscheint eine neue Checkbox, bei der Du die Badge aktivieren kannst.

/ Step 1: Add checkbox 

add_action( 'woocommerce_product_options_general_product_data', 'checkbox_badge' );        
  
function checkbox_badge() {           
woocommerce_wp_checkbox( array( 
'id' => 'checkbox_badge', 
'class' => '', 
'label' => 'Display free shipping badge'
) 
);      
}

// Step 2: Save checkbox selection
  
add_action( 'save_post', 'save_checkbox_badge_selection' );
  
function save_checkbox_badge_selection( $product_id ) {
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
        return;
    if ( isset( $_POST['checkbox_badge'] ) ) {
            update_post_meta( $product_id, 'checkbox_badge', $_POST['checkbox_badge'] );
    } else delete_post_meta( $product_id, 'checkbox_badge' );
}

// Step 3: Display custom badge at single product page
  
add_action( 'woocommerce_before_add_to_cart_form', 'display_checkbox_badge', 7 );
  
function display_checkbox_badge() {
    global $product;     
    if ( get_post_meta( $product->get_id(), 'checkbox_badge', true ) ) {
        echo '
<div class="custom-badge-1">Gratis Versand</div>'; // Change this text if needed
    }
}

Badge mit freiem Text

Mit diesem Script wird ein neues Feld im Produkt Konfigurator angezeigt. Du kannst bei jedem Produkt einen Text nach Wahl eingeben, der beim entsprechenden Produkt erscheint. Großartig für wichtige Hinweise.

// Display Fields
add_action( 'woocommerce_product_options_general_product_data', 'woo_add_custom_fields' );

// Save Fields
add_action( 'woocommerce_process_product_meta', 'woo_add_custom_fields_save' );

function woo_add_custom_fields() {

  global $woocommerce, $post;
  
  echo '<div class="options_group">';
  
 // Display Woocommerce product badge box in product edit page
woocommerce_wp_text_input( 
	array( 
		'id'          => '_text_field', 
		'label'       => __( 'Badge text', 'woocommerce' ), 
		'placeholder' => 'Enter your badge text here',
		'desc_tip'    => 'true',
		'description' => __( 'Enter your badge text here', 'woocommerce' ) 
	)
); 
  echo '</div>';	
}
function woo_add_custom_fields_save( $post_id ){
	// Text Field
	$woocommerce_text_field = $_POST['_text_field'];
	if( !empty( $woocommerce_text_field ) )
		update_post_meta( $post_id, '_text_field', esc_attr( $woocommerce_text_field ) );
}

// Display Woocommerce product badge with custom text in single product page
add_action('woocommerce_before_add_to_cart_form', 'display_custom_field_value', 7 );
function display_custom_field_value(){
  $value = get_post_meta( get_the_ID(), '_text_field', true);
  if(strlen($value) != null && strlen($value) > 0) {
	echo '<div class="custom-badge">'.get_post_meta( get_the_ID(), '_text_field', true ).'</div>';
  }
}
WordPress und Woocommerce Snippets für Deine Seite (Teil 1) -Skripte -Boost my Shop

CSS für den Admin Bereich

Du möchtest den Admin Bereich selbst gestalten? Kein Problem mit diesem Script kannst Du CSS in den Admin Bereich laden und ausführen.

add_action('admin_head', 'my_custom_admin_css');

function my_custom_admin_css() {
  echo '<style>
  Dein CSS Code hier!


 </style>';
}

Last Update

Wenn Du automatisierte Produkte hast, oder eine Webseite an der mehrere Leute arbeiten, ist es nützlich zu sehen wann das Produkt oder der Post das letzte mal verändert wurde. Dieses Script fügt bei der Übersicht eine neue Spalte hinzu.

add_action( 'admin_init', 'show_modified_date_in_admin_lists_language' );
function show_modified_date_in_admin_lists_language() {
	load_plugin_textdomain( 'show-modified-date-in-admin-lists', false,  dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}

// Register Modified Date Column for both posts & pages
function modified_column_register( $columns ) {
	$columns['Modified'] = __( 'Modified Date', 'show-modified-date-in-admin-lists' );
	return $columns;
}
add_filter( 'manage_posts_columns', 'modified_column_register' );
add_filter( 'manage_pages_columns', 'modified_column_register' );

function modified_column_display( $column_name, $post_id ) {
	switch ( $column_name ) {
	case 'Modified':
		global $post; 
	       	echo '<p class="mod-date">';
	       	echo '<em>'.get_the_modified_date().' '.get_the_modified_time().'</em><br />';
			if ( !empty( get_the_modified_author() ) ) {
				echo '<small>' . esc_html__( 'by', 'show-modified-date-in-admin-lists' ) . ' <strong>'.get_the_modified_author().'<strong></small>';
			} else {
				echo '<small>' . esc_html__( 'by', 'show-modified-date-in-admin-lists' ) . ' <strong>' . esc_html__( 'Wordpress', 'show-modified-date-in-admin-lists' ) . '<strong></small>';
			}
			echo '</p>';
		break; // end all case breaks
	}
}
add_action( 'manage_posts_custom_column', 'modified_column_display', 10, 2 );
add_action( 'manage_pages_custom_column', 'modified_column_display', 10, 2 );

function modified_column_register_sortable( $columns ) {
	$columns['Modified'] = 'modified';
	return $columns;
}
add_filter( 'manage_edit-post_sortable_columns', 'modified_column_register_sortable' );
add_filter( 'manage_edit-page_sortable_columns', 'modified_column_register_sortable' );
WordPress und Woocommerce Snippets für Deine Seite (Teil 1) -Skripte -Boost my Shop

SEOM Logo