Can not save multiple product variation fields - wordpress

When I save the first product variation fields they are saved, but when I try to save the second, third, forth and further they do not get saved.
I am filling out the SKU for every product variation as well, so it should work. But obv it is something I am missing here? This is the latest WordPress and latest WooCommerce version.
//Display Fields
add_action( 'woocommerce_product_after_variable_attributes', 'variable_fields', 10, 3 );
//Save variation fields
add_action( 'woocommerce_save_product_variation', 'save_variable_fields', 10, 1 );
/**
* Create new fields for variations
*
*/
function variable_fields( $loop, $variation_data, $variation ) {
?>
<tr>
<td>
<?php
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea['.$loop.']',
'label' => __( 'Contains', 'woocommerce' ),
'placeholder' => '',
'description' => __( '<br />Contains', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_textarea', true ),
)
);
?>
</td>
</tr>
<tr>
<td>
<?php
// Textarea
woocommerce_wp_textarea_input(
array(
'id' => '_textarea_2['.$loop.']',
'label' => __( 'Observation message', 'woocommerce' ),
'placeholder' => '',
'description' => __( '<br />Observation message', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, '_textarea_2', true ),
)
);
?>
</td>
</tr>
<?php
}
/**
* Save new fields for variations
*
*/
function save_variable_fields( $post_id ) {
if (isset( $_POST['variable_sku'] ) ) :
$variable_sku = $_POST['variable_sku'];
$variable_post_id = $_POST['variable_post_id'];
// Textarea
$_textarea = $_POST['_textarea'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $_textarea[$i] ) ) {
update_post_meta( $variation_id, '_textarea', stripslashes( $_textarea[$i] ) );
}
endfor;
// Textarea
$_textarea_2 = $_POST['_textarea_2'];
for ( $i = 0; $i < sizeof( $variable_sku ); $i++ ) :
$variation_id = (int) $variable_post_id[$i];
if ( isset( $_textarea_2[$i] ) ) {
update_post_meta( $variation_id, '_textarea_2', stripslashes( $_textarea_2[$i] ) );
}
endfor;
endif;
}

I've modified action & function for saving:
//Save variation fields
add_action( 'woocommerce_save_product_variation', 'save_variable_fields', 10, 2 );
/**
* Save new fields for variations
*
*/
function save_variable_fields( $variation_id, $i ) {
if ( empty( $variation_id ) ) return;
if ( isset( $_POST['_textarea'][$i] ) ) {
update_post_meta( $variation_id, '_textarea', stripslashes( $_POST['_textarea'][$i] ) );
}
if ( isset( $_POST['_textarea_2'][$i] ) ) {
update_post_meta( $variation_id, '_textarea_2', stripslashes( $_POST['_textarea_2'][$i] ) );
}
}

Related

Woocommerce - add field to Check pay method

I'd like to add a field on Check Payment that allows the order processor to insert a check number...please help! Check payment orders will only be entered by staff, not the public.
Add Field Here
I've searched all over and I'm stumped.
As per my understanding of your question, I have created this snippet which will help you with your requirement.
// add a field on the check payment type
add_filter( 'woocommerce_gateway_description', 'custom_check_payment_field', 10, 2 );
function custom_check_payment_field( $description, $payment_id ) {
if ( is_user_logged_in() ) {
$user = wp_get_current_user();
$current_user_role = (array) $user->roles;
if ( 'cheque' === $payment_id && $current_user_role[0] == 'administrator') {
ob_start();
echo '<div class="cheque-field">';
woocommerce_form_field(
'customer_cheque_number',
array(
'type' => 'text',
'label' => __( 'Enter cheque number', 'woocommerce' ),
'class' => array( 'form-row-wide' ),
'required' => true,
),
''
);
echo '<div>';
$description .= ob_get_clean();
}
}
return $description;
}
// save check number in meta/database
add_action( 'woocommerce_checkout_create_order', 'save_customer_cheque_number_order_meta', 10, 2 );
function save_customer_cheque_number_order_meta( $order, $data ) {
if ( isset( $_POST['customer_cheque_number'] ) && ! empty( $_POST['customer_cheque_number'] ) ) {
$order->update_meta_data( '_customer_cheque_number', esc_attr( $_POST['customer_cheque_number'] ) );
}
}
// show check number on orders page (OPTONAL)
add_action( 'woocommerce_get_order_item_totals', 'display_customer_cheque_number_on_order_totals', 10, 3 );
function display_customer_cheque_number_on_order_totals( $total_rows, $order, $tax_display ) {
if ( $order->get_payment_method() === 'cheque' && $customer_cheque_number = $order->get_meta( '_customer_cheque_number' ) ) {
$sorted_total_rows = array();
foreach ( $total_rows as $key_row => $total_row ) {
$sorted_total_rows[ $key_row ] = $total_row;
if ( $key_row === 'payment_method' ) {
$sorted_total_rows['customer_cheque_number'] = array(
'label' => __( 'Cheque number', 'woocommerce' ),
'value' => esc_html( $customer_cheque_number ),
);
}
}
$total_rows = $sorted_total_rows;
}
return $total_rows;
}
// add meta box on orders on woocommerce backend
add_action( 'add_meta_boxes', 'add_meta_boxes_callback' );
if ( ! function_exists( 'add_meta_boxes_callback' ) ) {
function add_meta_boxes_callback() {
add_meta_box( 'shop_order_fields', __( 'Cheque number', 'woocommerce' ), 'orders_cheque_number_custom_meta', 'shop_order', 'side', 'core' );
}
}
// show meta field with entered check number
if ( ! function_exists( 'orders_cheque_number_custom_meta' ) ) {
function orders_cheque_number_custom_meta() {
global $post;
$check_number = get_post_meta( $post->ID, '_customer_cheque_number', true ) ? get_post_meta( $post->ID, '_customer_cheque_number', true ) : '';
?>
<input type="text" style="width:250px;margin-top:15px" readonly name="readonly_check_number" value="<?php echo $check_number; ?>"></p>
<?php
}
}

Quantity field input on related products (woocommerce) doesn't appear

I'm using this hook to show a quantity input on all my woocommerce_loop_add_to_cart_link, but in the related products widget, is the only place that the woocommerce_quantity_input() (input element) doesn't even appear.
Do you know why?
add_filter( 'woocommerce_loop_add_to_cart_link', function( $html, $product ) {
if ( $product && $product->is_type( 'simple' ) && $product->is_purchasable() && $product->is_in_stock() && ! $product->is_sold_individually() ) {
$html = '<form action="' . esc_url( $product->add_to_cart_url() ) . '" class="wcb2b-quantity" method="post" enctype="multipart/form-data" >';
$html .= woocommerce_quantity_input( array(), $product, false );
$html .= '<button type="submit" class="button alt">' . esc_html( $product->add_to_cart_text() ) . '</button>';
$html .= '</form>';
}
return $html;
the woocommerce_quantity_input() is defined here
function woocommerce_quantity_input( $args = array(), $product = null, $echo = true ) {
if ( is_null( $product ) ) {
$product = $GLOBALS['product'];
}
$defaults = array(
'input_id' => uniqid( 'quantity_' ),
'input_name' => 'quantity',
'input_value' => '1',
'classes' => apply_filters( 'woocommerce_quantity_input_classes', array( 'input-text', 'qty', 'text' ), $product ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', -1, $product ),
'min_value' => apply_filters( 'woocommerce_quantity_input_min', 0, $product ),
'step' => apply_filters( 'woocommerce_quantity_input_step', 1, $product ),
'pattern' => apply_filters( 'woocommerce_quantity_input_pattern', has_filter( 'woocommerce_stock_amount', 'intval' ) ? '[0-9]*' : '' ),
'inputmode' => apply_filters( 'woocommerce_quantity_input_inputmode', has_filter( 'woocommerce_stock_amount', 'intval' ) ? 'numeric' : '' ),
'product_name' => $product ? $product->get_title() : '',
'placeholder' => apply_filters( 'woocommerce_quantity_input_placeholder', '', $product ),
// When autocomplete is enabled in firefox, it will overwrite actual value with what user entered last. So we default to off.
// See #link https://github.com/woocommerce/woocommerce/issues/30733.
'autocomplete' => apply_filters( 'woocommerce_quantity_input_autocomplete', 'off', $product ),
);
$args = apply_filters( 'woocommerce_quantity_input_args', wp_parse_args( $args, $defaults ), $product );
// Apply sanity to min/max args - min cannot be lower than 0.
$args['min_value'] = max( $args['min_value'], 0 );
$args['max_value'] = 0 < $args['max_value'] ? $args['max_value'] : '';
// Max cannot be lower than min if defined.
if ( '' !== $args['max_value'] && $args['max_value'] < $args['min_value'] ) {
$args['max_value'] = $args['min_value'];
}
ob_start();
wc_get_template( 'global/quantity-input.php', $args );
if ( $echo ) {
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo ob_get_clean();
} else {
return ob_get_clean();
}
}
Related product screenshot: https://ibb.co/vw6Rt4d
Other loops screenshot: https://ibb.co/pRcpBRp

Print styled HTML as custom field output from admin meta box fields in WooCommerce

I have this code, based on Display custom field values of product variations to custom product tab in WooCommerce to display 3 custom field on WooCommerce variations.
In that fields I want to insert text with HTML tags and show it in shortcode with all HTML styling (tables etc...) but current code doesn't output styled text but raw text with printed HTML tags:
<div>some text etc...</div><a>some link</a>
Any suggestions what I did wrong here?
/* Add custom field input # Product Data > Variations > Single Variation */
add_action( 'woocommerce_variation_options', 'add_custom_field_cage_code_to_variations', 10, 3 );
function add_custom_field_cage_code_to_variations( $loop, $variation_data, $variation ) {
echo '<div class="cage_code_options_group options_group">';
woocommerce_wp_text_input( array(
'id' => 'cage_code[' . $loop . ']',
'label' => __( 'One-line description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code', true )
));
woocommerce_wp_textarea_input( array(
'id' => 'cage_code_part_number[' . $loop . ']',
'label' => __( 'Short Description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code_part_number', true )
));
woocommerce_wp_textarea_input( array(
'id' => 'cage_code_niin_nsn_number[' . $loop . ']',
'label' => __( 'Long description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code_niin_nsn_number', true )
));
echo '</div>';
}
/* Save custom field on product variation save */
add_action( 'woocommerce_save_product_variation', 'magazine_save_custom_field_variations', 10, 2 );
function magazine_save_custom_field_variations( $variation_id, $i ) {
$cage_code = $_POST['cage_code'][$i];
if ( isset( $cage_code ) ) update_post_meta( $variation_id, 'cage_code', esc_attr(
$cage_code ) );
$cage_code_part_number = $_POST['cage_code_part_number'][$i];
if ( isset( $cage_code ) ) update_post_meta( $variation_id, 'cage_code_part_number',
esc_attr( $cage_code_part_number ) );
$cage_code_niin_nsn_number = $_POST['cage_code_niin_nsn_number'][$i];
if ( isset( $cage_code_niin_nsn_number ) ) update_post_meta( $variation_id,
'cage_code_niin_nsn_number', esc_attr( $cage_code_niin_nsn_number ) );
}
function woo_cage_code_info_tab_content() {
global $product;
if ( $product->is_type( 'variable' ) ) {
// Loop through the variation IDs
foreach( $product->get_children() as $key => $variation_id ) {
// Get an instance of the WC_Product_Variation Object
$variation = wc_get_product( $variation_id );
// Get meta
$cage_code = $variation->get_meta( 'cage_code' );
$cage_code_part_number = $variation->get_meta( 'cage_code_part_number' );
$cage_code_niin_nsn_number = $variation->get_meta(
'cage_code_niin_nsn_number' );
// Output
echo '<div class="woo_cage_code_info_tab_content
woo_cage_code_info_tab_content-' . $variation_id .'">';
if ( $cage_code ) {
echo '<p>' . $cage_code . '</p>';
}
echo '</div>';
}
?>
<script>
jQuery(document).ready(function($) {
// Hide all
$( '.woo_cage_code_info_tab_content' ).css( 'display', 'none' );
// Change
$( 'input.variation_id' ).change( function() {
// Hide all
$( '.woo_cage_code_info_tab_content' ).css( 'display', 'none' );
if( $( 'input.variation_id' ).val() != '' ) {
var var_id = $( 'input.variation_id' ).val();
// Display current
$( '.woo_cage_code_info_tab_content-' + var_id ).css( 'display', 'block' );
}
});
});
</script>
<?php
}
}
add_shortcode('woo_cage_code_info_tab_content', 'woo_cage_code_info_tab_content');
Your code contains some minor mistakes. You can use woocommerce_wp_text_input() or woocommerce_wp_textarea_input() without issues.
However, do not use esc_attr() when saving because it's for escaping HTML attributes (I assume that you as admin only have access to the backend fields) OR if you do use it, convert HTML entities back to characters at the output. In this answer I have used wp_kses_post() and wp_unslash()
The woocommerce_admin_process_variation_object replaces the outdated woocommerce_save_product_variation hook
So you get:
// Add field(s)
function action_woocommerce_variation_options( $loop, $variation_data, $variation ) {
echo '<div class="cage_code_options_group options_group">';
woocommerce_wp_text_input(
array(
'id' => 'cage_code[' . $loop . ']',
'label' => __( 'One-line description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code', true )
)
);
woocommerce_wp_textarea_input(
array(
'id' => 'cage_code_part_number[' . $loop . ']',
'label' => __( 'Short Description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code_part_number', true )
)
);
woocommerce_wp_textarea_input(
array(
'id' => 'cage_code_niin_nsn_number[' . $loop . ']',
'label' => __( 'Long description', 'woocommerce' ),
'value' => get_post_meta( $variation->ID, 'cage_code_niin_nsn_number', true )
)
);
echo '</div>';
}
add_action( 'woocommerce_variation_options', 'action_woocommerce_variation_options', 10, 3 );
// Save
function action_woocommerce_admin_process_variation_object( $variation, $i ) {
// Isset
if ( isset( $_POST['cage_code'][$i] ) ) {
// Store
$cage_code = wp_kses_post( wp_unslash( $_POST['cage_code'][$i] ) );
// Update
$variation->update_meta_data( 'cage_code', $cage_code );
}
// Isset
if ( isset( $_POST['cage_code_part_number'][$i] ) ) {
// Store
$cage_code_part_number = wp_kses_post( wp_unslash( $_POST['cage_code_part_number'][$i] ) );
// Update
$variation->update_meta_data( 'cage_code_part_number', $cage_code_part_number );
}
// Isset
if ( isset( $_POST['cage_code_niin_nsn_number'][$i] ) ) {
// Store
$cage_code_niin_nsn_number = wp_kses_post( wp_unslash( $_POST['cage_code_niin_nsn_number'][$i] ) );
// Update
$variation->update_meta_data( 'cage_code_niin_nsn_number', $cage_code_niin_nsn_number );
}
}
add_action( 'woocommerce_admin_process_variation_object', 'action_woocommerce_admin_process_variation_object', 10, 2 );
Then I have no idea why you would use a shortcode versus a hook to render, like I did this in my answer. This can be any hook as long as it applies to the single product page:
// Display
function filter_woocommerce_product_tabs( $tabs ) {
// Adds the new tab
$tabs['cage_code_information_tab'] = array(
'title' => __( 'Cage Code Information', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_cage_code_info_tab_content'
);
return $tabs;
}
add_filter( 'woocommerce_product_tabs', 'filter_woocommerce_product_tabs', 100, 1 );
function woo_cage_code_info_tab_content() {
global $product;
if ( $product->is_type( 'variable' ) ) {
// Loop through the variation IDs
foreach( $product->get_children() as $key => $variation_id ) {
// Get an instance of the WC_Product_Variation Object
$variation = wc_get_product( $variation_id );
// Get meta
$cage_code = $variation->get_meta( 'cage_code' );
$cage_code_part_number = $variation->get_meta( 'cage_code_part_number' );
$cage_code_niin_nsn_number = $variation->get_meta( 'cage_code_niin_nsn_number' );
// Output
echo '<div class="woo_cage_code_info_tab_content woo_cage_code_info_tab_content-' . $variation_id .'">';
if ( $cage_code ) {
echo '<p>Cage code: ' . $cage_code . '</p>';
}
if ( $cage_code_part_number ) {
echo '<p>Cage code part number: ' . $cage_code_part_number . '</p>';
}
if ( $cage_code_niin_nsn_number ) {
echo '<p>Cage code niin nsn_number: ' . $cage_code_niin_nsn_number . '</p>';
}
echo '</div>';
}
?>
<script>
jQuery(document).ready(function($) {
// Hide all
$( '.woo_cage_code_info_tab_content' ).css( 'display', 'none' );
// Change
$( 'input.variation_id' ).change( function() {
// Hide all
$( '.woo_cage_code_info_tab_content' ).css( 'display', 'none' );
if( $( 'input.variation_id' ).val() != '' ) {
var var_id = $( 'input.variation_id' ).val();
// Display current
$( '.woo_cage_code_info_tab_content-' + var_id ).css( 'display', 'block' );
}
});
});
</script>
<?php
}
}

woocommerce checkout custom fields update order meta

based on this https://stackoverflow.com/a/49163797/7783506 by #LoicTheAztec I was able to create the custom fields on flat rate shipping method, I could find the data on MySQL, but it was not showing on the order detail, email.
How do I add this to order details and email? here's the code that i use based on that post
// Add custom fields to a specific selected shipping method
add_action( 'woocommerce_after_shipping_rate', 'carrier_custom_fields', 20, 2 );
function carrier_custom_fields( $method, $index ) {
if( ! is_checkout()) return; // Only on checkout page
$customer_carrier_method = 'flat_rate:4';
if( $method->id != $customer_carrier_method ) return; // Only display for "local_pickup"
$chosen_method_id = WC()->session->chosen_shipping_methods[ $index ];
// If the chosen shipping method is 'legacy_local_pickup' we display
if($chosen_method_id == $customer_carrier_method ):
echo '<div class="custom-carrier">';
woocommerce_form_field( 'ds_name' , array(
'type' => 'text',
'class' => array('form-row-wide ds-name'),
'required' => true,
'placeholder' => 'Nama Toko',
), WC()->checkout->get_value( 'ds_name' ));
woocommerce_form_field( 'ds_number' , array(
'type' => 'text',
'class' => array('form-row-wide ds-number'),
'required' => true,
'placeholder' => 'Kode Booking',
), WC()->checkout->get_value( 'ds_number' ));
echo '</div>';
endif;
}
// Check custom fields validation
add_action('woocommerce_checkout_process', 'carrier_checkout_process');
function carrier_checkout_process() {
if( isset( $_POST['ds_name'] ) && empty( $_POST['ds_name'] ) )
wc_add_notice( ( "Anda belum memasukkan Nama Toko" ), "error" );
if( isset( $_POST['ds_number'] ) && empty( $_POST['ds_number'] ) )
wc_add_notice( ( "Anda belum memasukkan nomor kode booking" ), "error" );
}
// Save custom fields to order meta data
add_action( 'woocommerce_checkout_update_order_meta', 'carrier_update_order_meta', 30, 1 );
function carrier_update_order_meta( $order_id ) {
if( isset( $_POST['ds_name'] ))
update_post_meta( $order_id, '_ds_name', sanitize_text_field( $_POST['ds_name'] ) );
if( isset( $_POST['ds_number'] ))
update_post_meta( $order_id, '_ds_number', sanitize_text_field( $_POST['ds_number'] ) );
}
found the solution, removing _ (underscore) on update_post_meta then it show on order edit.
// Save custom fields to order meta data
add_action( 'woocommerce_checkout_update_order_meta', 'carrier_update_order_meta', 30, 1 );
function carrier_update_order_meta( $order_id ) {
if( isset( $_POST['ds_name'] ))
update_post_meta( $order_id, 'ds_name', sanitize_text_field( $_POST['ds_name'] ) );
if( isset( $_POST['ds_number'] ))
update_post_meta( $order_id, 'ds_number', sanitize_text_field( $_POST['ds_number'] ) );
}

How to show "Published on"-date instead of "Last edited"-date in wordpress post's

I have a theme for a blog in which im trying to modify, so that it always shows the "published date" instead for "Edited on" every time I edit it...
I have nailed it down to the file -> functions.php where it displays the dates... Section "Posted On Function"
But everytime i try to modify the if/else i get a error 500 on the page :-(
Some help would be highly appreciated here...
Thanks in advance!
-Best regards
Part that I suspect is the reason :
-------------------------------------------------------------------------------------------------------
Posted On Function
-------------------------------------------------------------------------------------------------------
*/
function swell_posted_on() {
if ( get_the_modified_time() != get_the_time() ) {
printf( __( '<span class="%1$s">Last Updated:</span> %2$s', 'organic-swell' ),
'meta-prep meta-prep-author',
sprintf( '<span class="entry-date">%3$s</span>',
esc_url( get_permalink() ),
esc_attr( get_the_modified_time() ),
get_the_modified_date()
)
);
} else {
printf( __( '<span class="%1$s">Posted:</span> %2$s', 'organic-swell' ),
'meta-prep meta-prep-author',
sprintf( '<span class="entry-date">%3$s</span>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
get_the_date()
)
);
}
}
/*
This is the entire functions.php file :
/*
-------------------------------------------------------------------------------------------------------
Theme Setup
-------------------------------------------------------------------------------------------------------
*/
if ( ! function_exists( 'swell_setup' ) ) :
function swell_setup() {
// Make theme available for translation.
load_theme_textdomain( 'organic-swell', get_template_directory() . '/languages' );
// Add default posts and comments RSS feed links to head.
add_theme_support( 'automatic-feed-links' );
// Enable support for Post Thumbnails.
add_theme_support( 'post-thumbnails' );
// Enable support for site title tag.
add_theme_support( 'title-tag' );
add_image_size( 'swell-featured-large', 1800, 1200, true ); // Large Featured Image.
add_image_size( 'swell-featured-medium', 1200, 800, true ); // Medium Featured Image.
add_image_size( 'swell-featured-small', 640, 640, true ); // Small Featured Image.
// Post Formats.
add_theme_support( 'post-formats', array(
'gallery',
'link',
'image',
'audio',
'status',
'quote',
'video',
)
);
// Create Menus.
register_nav_menus( array(
'fixed-menu' => esc_html__( 'Fixed Menu', 'organic-swell' ),
'main-menu' => esc_html__( 'Main Menu', 'organic-swell' ),
'social-menu' => esc_html__( 'Social Menu', 'organic-swell' ),
));
// Custom Header.
register_default_headers( array(
'default' => array(
'url' => get_template_directory_uri() . '/images/default-header.jpg',
'thumbnail_url' => get_template_directory_uri() . '/images/default-header.jpg',
'description' => esc_html__( 'Default Custom Header', 'organic-swell' ),
),
));
$defaults = array(
'width' => 1800,
'height' => 480,
'flex-height' => true,
'flex-width' => true,
'default-text-color' => 'ffffff',
'default-image' => get_template_directory_uri() . '/images/default-header.jpg',
'header-text' => false,
'uploads' => true,
);
add_theme_support( 'custom-header', $defaults );
// Custom Background.
$defaults = array(
'default-color' => 'eeeeee',
);
add_theme_support( 'custom-background', $defaults );
}
endif; // swell_setup
add_action( 'after_setup_theme', 'swell_setup' );
/*
-------------------------------------------------------------------------------------------------------
Theme Updater
-------------------------------------------------------------------------------------------------------
*/
function swell_theme_updater() {
require( get_template_directory() . '/updater/theme-updater.php' );
}
add_action( 'after_setup_theme', 'swell_theme_updater' );
/*
-------------------------------------------------------------------------------------------------------
Category ID to Name
-------------------------------------------------------------------------------------------------------
*/
function swell_cat_id_to_name( $id ) {
$cat = get_category( $id );
if ( is_wp_error( $cat ) ) {
return false; }
return $cat->cat_name;
}
/*
-------------------------------------------------------------------------------------------------------
Register Scripts
-------------------------------------------------------------------------------------------------------
*/
if ( ! function_exists( 'swell_enqueue_scripts' ) ) {
function swell_enqueue_scripts() {
// Enqueue Styles.
wp_enqueue_style( 'swell-style', get_stylesheet_uri() );
wp_enqueue_style( 'swell-style-mobile', get_template_directory_uri() . '/css/style-mobile.css', array( 'swell-style' ), '1.0' );
// Resgister Scripts.
wp_register_script( 'swell-fitvids', get_template_directory_uri() . '/js/jquery.fitvids.js', array( 'jquery' ), '20130729' );
wp_register_script( 'swell-hover', get_template_directory_uri() . '/js/hoverIntent.js', array( 'jquery' ), '20130729' );
wp_register_script( 'swell-superfish', get_template_directory_uri() . '/js/superfish.js', array( 'jquery', 'swell-hover' ), '20130729' );
// Enqueue Scripts.
wp_enqueue_script( 'swell-custom', get_template_directory_uri() . '/js/jquery.custom.js', array( 'jquery', 'swell-superfish', 'swell-fitvids', 'masonry' ), '20130729', true );
wp_enqueue_script( 'swell-navigation', get_template_directory_uri() . '/js/navigation.js', array(), '20130729', true );
// Load Flexslider on front page and slideshow page template.
if ( is_home() || is_front_page() || is_single() || is_page_template( 'template-slideshow.php' ) || is_page_template( 'template-featured-content.php' ) ) {
wp_enqueue_script( 'swell-flexslider', get_template_directory_uri() . '/js/jquery.flexslider.js', array( 'jquery' ), '20130729' );
}
// Load single scripts only on single pages.
if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
wp_enqueue_script( 'comment-reply' );
}
}
}
add_action( 'wp_enqueue_scripts', 'swell_enqueue_scripts' );
/*
-------------------------------------------------------------------------------------------------------
Register Sidebars
-------------------------------------------------------------------------------------------------------
*/
function swell_widgets_init() {
register_sidebar(array(
'name' => esc_html__( 'Default Sidebar', 'organic-swell' ),
'id' => 'default-sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h6 class="title">',
'after_title' => '</h6>',
));
register_sidebar(array(
'name' => esc_html__( 'Blog Sidebar', 'organic-swell' ),
'id' => 'blog-sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h6 class="title">',
'after_title' => '</h6>',
));
register_sidebar(array(
'name' => esc_html__( 'Left Sidebar', 'organic-swell' ),
'id' => 'left-sidebar',
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h6 class="title">',
'after_title' => '</h6>',
));
register_sidebar(array(
'name' => esc_html__( 'Footer Widgets', 'organic-swell' ),
'id' => 'footer',
'before_widget' => '<div id="%1$s" class="widget %2$s"><div class="footer-widget">',
'after_widget' => '</div></div>',
'before_title' => '<h6 class="title">',
'after_title' => '</h6>',
));
}
add_action( 'widgets_init', 'swell_widgets_init' );
/*
-------------------------------------------------------------------------------------------------------
Add Stylesheet To Visual Editor
-------------------------------------------------------------------------------------------------------
*/
add_action( 'widgets_init', 'swell_add_editor_styles' );
/**
* Apply theme's stylesheet to the visual editor.
*
* #uses add_editor_style() Links a stylesheet to visual editor
* #uses get_stylesheet_uri() Returns URI of theme stylesheet
*/
function swell_add_editor_styles() {
add_editor_style( 'css/style-editor.css' );
}
/*
-------------------------------------------------------------------------------------------------------
Posted On Function
-------------------------------------------------------------------------------------------------------
*/
function swell_posted_on() {
if ( get_the_modified_time() != get_the_time() ) {
printf( __( '<span class="%1$s">Last Updated:</span> %2$s', 'organic-swell' ),
'meta-prep meta-prep-author',
sprintf( '<span class="entry-date">%3$s</span>',
esc_url( get_permalink() ),
esc_attr( get_the_modified_time() ),
get_the_modified_date()
)
);
} else {
printf( __( '<span class="%1$s">Posted:</span> %2$s', 'organic-swell' ),
'meta-prep meta-prep-author',
sprintf( '<span class="entry-date">%3$s</span>',
esc_url( get_permalink() ),
esc_attr( get_the_time() ),
get_the_date()
)
);
}
}
/*
-------------------------------------------------------------------------------------------------------
Post Format Meta Boxes
-------------------------------------------------------------------------------------------------------
*/
add_action( 'admin_init', 'create_metaboxes' );
add_action( 'save_post', 'save_metaboxes' );
$metaboxes = array(
'link_url' => array(
'title' => esc_html__( 'Link Information', 'organic-swell' ),
'applicableto' => 'post',
'location' => 'side',
'display_condition' => 'post-format-link',
'priority' => 'default',
'fields' => array(
'l_url' => array(
'title' => esc_html__( 'Link URL: ', 'organic-swell' ),
'type' => 'text',
'description' => '',
'size' => 20,
),
),
),
'quote_author' => array(
'title' => esc_html__( 'Quote Author', 'organic-swell' ),
'applicableto' => 'post',
'location' => 'side',
'display_condition' => 'post-format-quote',
'priority' => 'default',
'fields' => array(
'q_author' => array(
'title' => esc_html__( 'Author: ', 'organic-swell' ),
'type' => 'text',
'description' => '',
'size' => 20,
),
),
),
);
function create_metaboxes() {
global $metaboxes;
if ( ! empty( $metaboxes ) ) {
foreach ( $metaboxes as $id => $metabox ) {
add_meta_box( $id, $metabox['title'], 'show_metaboxes', $metabox['applicableto'], $metabox['location'], $metabox['priority'], $id );
}
}
}
function show_metaboxes( $post, $args ) {
global $metaboxes;
$custom = get_post_custom( $post->ID );
$fields = $tabs = $metaboxes[ $args['id'] ]['fields'];
/** Nonce */
$output = '<input type="hidden" name="post_format_meta_box_nonce" value="' . wp_create_nonce( basename( __FILE__ ) ) . '" />';
if ( sizeof( $fields ) ) {
foreach ( $fields as $id => $field ) {
switch ( $field['type'] ) {
default:
case 'text':
if ( isset( $custom[ $id ] ) ) {
$output .= '<label for="' . esc_attr( $id ) . '">' . $field['title'] . '</label><input id="' . esc_attr( $id ) . '" type="text" name="' . esc_attr( $id ) . '" value="' . $custom[ $id ][0] . '" size="' . $field['size'] . '" />';
} else {
$output .= '<label for="' . esc_attr( $id ) . '">' . $field['title'] . '</label><input id="' . esc_attr( $id ) . '" type="text" name="' . esc_attr( $id ) . '" value="" size="' . $field['size'] . '" />';
}
break;
}
}
}
echo $output;
}
function save_metaboxes( $post_id ) {
global $metaboxes;
// Verify nonce.
if ( isset( $_POST['post_format_meta_box_nonce'] ) && ! wp_verify_nonce( $_POST['post_format_meta_box_nonce'], basename( __FILE__ ) ) ) {
return $post_id; }
// Check autosave.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id; }
// Check permissions.
if ( 'page' == isset( $_POST['post_type'] ) ) {
if ( ! current_user_can( 'edit_page', $post_id ) ) {
return $post_id; }
} elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
return $post_id;
}
$post_type = get_post_type();
// Loop through fields and save the data.
foreach ( $metaboxes as $id => $metabox ) {
// Check if metabox is applicable for current post type.
if ( $metabox['applicableto'] == $post_type ) {
$fields = $metaboxes[ $id ]['fields'];
foreach ( $fields as $id => $field ) {
$old = get_post_meta( $post_id, $id, true );
$new = $_POST[ $id ];
if ( $new && $new != $old ) {
update_post_meta( $post_id, $id, $new );
} elseif ( '' == $new && $old || ! isset( $_POST[ $id ] ) ) {
delete_post_meta( $post_id, $id, $old );
}
}
}
}
}
add_action( 'admin_print_scripts', 'display_metaboxes', 1000 );
function display_metaboxes() {
global $metaboxes;
if ( get_post_type() == 'post' ) :
?>
<script type="text/javascript"> // <![CDATA[
$ = jQuery;
<?php
$formats = $ids = array();
foreach ( $metaboxes as $id => $metabox ) {
array_push( $formats, "'" . $metabox['display_condition'] . "': '" . $id . "'" );
array_push( $ids, '#' . $id );
}
?>
var formats = { <?php echo implode( ',', $formats );?> };
var ids = "<?php echo implode( ',', $ids ); ?>";
function displayMetaboxes() {
// Hide all post format metaboxes.
$(ids).hide();
// Get current post format.
var selectedElt = $("input[name='post_format']:checked").attr("id");
// If exists, fade in current post format metabox.
if ( formats[selectedElt] )
$("#" + formats[selectedElt]).fadeIn();
}
$(function() {
// Show/hide metaboxes on page load
displayMetaboxes();
// Show/hide metaboxes on change event
$("input[name='post_format']").change(function() {
displayMetaboxes();
});
});
// ]]></script>
<?php
endif;
}
/*
-------------------------------------------------------------------------------------------------------
Content Width
-------------------------------------------------------------------------------------------------------
*/
if ( ! isset( $content_width ) ) {
$content_width = 640; }
/**
* Adjust content_width value based on the presence of widgets
*/
function swell_content_width() {
if ( ! is_active_sidebar( 'post-sidebar' ) || is_active_sidebar( 'page-sidebar' ) || is_active_sidebar( 'blog-sidebar' ) ) {
global $content_width;
$content_width = 960;
}
}
add_action( 'template_redirect', 'swell_content_width' );
/*
-------------------------------------------------------------------------------------------------------
Comments Function
-------------------------------------------------------------------------------------------------------
*/
if ( ! function_exists( 'swell_comment' ) ) :
function swell_comment( $comment, $args, $depth ) {
$GLOBALS['comment'] = $comment;
switch ( $comment->comment_type ) :
case 'pingback' :
case 'trackback' :
?>
<li class="post pingback">
<p><?php esc_html_e( 'Pingback:', 'organic-swell' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( esc_html__( 'Edit', 'organic-swell' ), '<span class="edit-link">', '</span>' ); ?></p>
<?php
break;
default :
?>
<li <?php comment_class(); ?> id="<?php echo esc_attr( 'li-comment-' . get_comment_ID() ); ?>">
<article id="<?php echo esc_attr( 'comment-' . get_comment_ID() ); ?>" class="comment">
<footer class="comment-meta">
<div class="comment-author vcard">
<?php
$avatar_size = 72;
if ( '0' != $comment->comment_parent ) {
$avatar_size = 48; }
echo get_avatar( $comment, $avatar_size );
/* translators: 1: comment author, 2: date and time */
printf( __( '%1$s <br/> %2$s <br/>', 'organic-swell' ),
sprintf( '<span class="fn">%s</span>', wp_kses_post( get_comment_author_link() ) ),
sprintf( '<time pubdate datetime="%2$s">%3$s</time>',
esc_url( get_comment_link( $comment->comment_ID ) ),
get_comment_time( 'c' ),
/* translators: 1: date, 2: time */
sprintf( __( '%1$s', 'organic-swell' ), get_comment_date(), get_comment_time() )
)
);
?>
</div><!-- .comment-author .vcard -->
</footer>
<div class="comment-content">
<?php if ( $comment->comment_approved == '0' ) : ?>
<em class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'organic-swell' ); ?></em>
<br />
<?php endif; ?>
<?php comment_text(); ?>
<div class="reply">
<?php comment_reply_link( array_merge( $args, array( 'reply_text' => esc_html__( 'Reply', 'organic-swell' ), 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
</div><!-- .reply -->
<?php edit_comment_link( esc_html__( 'Edit', 'organic-swell' ), '<span class="edit-link">', '</span>' ); ?>
</div>
</article><!-- #comment-## -->
<?php
break;
endswitch;
}
endif; // Ends check for swell_comment().
/*
-------------------------------------------------------------------------------------------------------
Disable Comments On Pages Default
-------------------------------------------------------------------------------------------------------
*/
function swell_default_comments_off( $data ) {
if ( $data['post_type'] == 'page' && $data['post_status'] == 'auto-draft' ) {
$data['comment_status'] = 0;
}
return $data;
}
add_filter( 'wp_insert_post_data', 'swell_default_comments_off' );
/*
-------------------------------------------------------------------------------------------------------
Custom Excerpt Length
-------------------------------------------------------------------------------------------------------
*/
function swell_excerpt_length( $length ) {
return 38;
}
add_filter( 'excerpt_length', 'swell_excerpt_length', 999 );
function swell_excerpt_more( $more ) {
return '... <a class="read-more" href="'. get_permalink( get_the_ID() ) . '">'. esc_html__( 'Read More', 'organic-swell' ) .'</a>';
}
add_filter( 'excerpt_more', 'swell_excerpt_more' );
/*
-------------------------------------------------------------------------------------------------------
Add Excerpt To Pages
-------------------------------------------------------------------------------------------------------
*/
add_action( 'widgets_init', 'swell_add_excerpts_to_pages' );
function swell_add_excerpts_to_pages() {
add_post_type_support( 'page', 'excerpt' );
}
/*
-------------------------------------------------------------------------------------------------------
Custom Page Links
-------------------------------------------------------------------------------------------------------
*/
function swell_wp_link_pages_args_prevnext_add( $args ) {
global $page, $numpages, $more, $pagenow;
if ( ! $args['next_or_number'] == 'next_and_number' ) {
return $args; }
$args['next_or_number'] = 'number'; // Keep numbering for the main part
if ( ! $more ) {
return $args; }
if ( $page -1 ) { // There is a previous page
$args['before'] .= _wp_link_page( $page -1 )
. $args['link_before']. $args['previouspagelink'] . $args['link_after'] . '</a>'; }
if ( $page < $numpages ) { // There is a next page
$args['after'] = _wp_link_page( $page + 1 )
. $args['link_before'] . $args['nextpagelink'] . $args['link_after'] . '</a>'
. $args['after']; }
return $args;
}
add_filter( 'wp_link_pages_args', 'swell_wp_link_pages_args_prevnext_add' );
/*
-------------------------------------------------------------------------------------------------------
Featured Video Meta Box
-------------------------------------------------------------------------------------------------------
*/
add_action( 'admin_init', 'admin_init_featurevid' );
add_action( 'save_post', 'save_featurevid' );
function admin_init_featurevid() {
add_meta_box( 'featurevid-meta', esc_html__( 'Featured Video Embed Code', 'organic-swell' ), 'meta_options_featurevid', 'post', 'normal', 'high' );
}
function meta_options_featurevid() {
global $post;
$custom = get_post_custom( $post->ID );
$featurevid = isset( $custom['featurevid'] ) ? esc_attr( $custom['featurevid'][0] ) : '';
echo '<textarea name="featurevid" cols="60" rows="4" style="width:97.6%" />'.$featurevid.'</textarea>';
}
function save_featurevid( $post_id ) {
global $post;
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return $post_id;
}
if ( isset( $_POST['featurevid'] ) ) {
update_post_meta( $post->ID, 'featurevid', $_POST['featurevid'] );
}
}
/*
-------------------------------------------------------------------------------------------------------
Remove First Gallery
-------------------------------------------------------------------------------------------------------
*/
function swell_remove_gallery( $content ) {
if ( is_page_template( 'template-slideshow.php' ) || has_post_format( 'gallery' ) || ( is_singular( 'jetpack-portfolio' ) && get_theme_mod( 'display_project_slideshow', false ) ) ) {
$content = preg_replace( '/\[gallery(.*?)ids=[^\]]+\]/', '', $content, 1 );
}
return $content;
}
add_filter( 'the_content', 'swell_remove_gallery' );
/*
-------------------------------------------------------------------------------------------------------
Home Link In Custom Menu
-------------------------------------------------------------------------------------------------------
*/
function home_page_menu_args( $args ) {
$args['show_home'] = true;
return $args;
}
add_filter( 'wp_page_menu_args', 'home_page_menu_args' );
/*
-------------------------------------------------------------------------------------------------------
Body Class
-------------------------------------------------------------------------------------------------------
*/
function swell_body_class( $classes ) {
if ( is_singular() ) {
$classes[] = 'swell-singular'; }
if ( is_active_sidebar( 'right-sidebar' ) ) {
$classes[] = 'swell-right-sidebar'; }
if ( '' != get_theme_mod( 'background_image' ) ) {
// This class will render when a background image is set
// regardless of whether the user has set a color as well.
$classes[] = 'swell-background-image';
} else if ( ! in_array( get_background_color(), array( '', get_theme_support( 'custom-background', 'default-color' ) ) ) ) {
// This class will render when a background color is set
// but no image is set. In the case the content text will
// Adjust relative to the background color.
$classes[] = 'swell-relative-text';
}
return $classes;
}
add_action( 'body_class', 'swell_body_class' );
/*
-------------------------------------------------------------------------------------------------------
Includes
-------------------------------------------------------------------------------------------------------
*/
require_once( get_template_directory() . '/includes/jetpack.php' );
require_once( get_template_directory() . '/includes/customizer.php' );
require_once( get_template_directory() . '/includes/typefaces.php' );
require_once( get_template_directory() . '/includes/woocommerce-setup.php' );
require_once( get_template_directory() . '/includes/plugin-activation.php' );
require_once( get_template_directory() . '/includes/plugin-activation-class.php' );
Change this line
if ( get_the_modified_time() != get_the_time() ) {
to
if ( get_the_modified_time() != get_the_time() && false ) {

Resources