I'm getting this error with my custom post metabox. The custom post and metabox works fine, but if I have debug on and when I try to save any other page or post, I will get an error:
Notice: Undefined index: iam_video_select in C:\xampp\htdocs\Iam\wp-content\themes\Iam\inc\classes\Custom_Meta_Boxes.class.php on line 149
Notice: Undefined index: iam_video_url in C:\xampp\htdocs\Iam\wp-content\themes\Iam\inc\classes\Custom_Meta_Boxes.class.php on line 149
Notice: Undefined index: iam_link in C:\xampp\htdocs\Iam\wp-content\themes\Iam\inc\classes\Custom_Meta_Boxes.class.php on line 149
My code:
<?php
$meta_boxes = array();
//Video meta box
$meta_boxes[] = array(
'id' => 'video-meta-box',
'title' => __( 'Video', 'iamtheme' ),
'pages' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Select Video Host',
'id' => PTHEME . '_video_select',
'type' => 'select',
'options' => array( 'Youtube', 'Vimeo', 'Self hosted' )
),
array(
'name' => 'URL',
'desc' => 'Enter video url here.',
'id' => PTHEME . '_video_url',
'std' => 'Default value here.',
'type' => 'text'
)
)
);
//Link meta box
$meta_boxes[] = array(
'id' => 'link-meta-box',
'title' => __( 'Link', 'iamtheme' ),
'pages' => 'post',
'context' => 'normal',
'priority' => 'high',
'fields' => array(
array(
'name' => 'Link',
'desc' => 'Enter your url here.',
'id' => PTHEME . '_link',
'std' => 'Default value here.',
'type' => 'text'
)
)
);
/**
*
*/
class Custom_Meta_Boxes{
public $_meta_box;
public function __construct( $meta_box ){
$this->_meta_box = $meta_box;
add_action( 'add_meta_boxes', array( $this, 'iam_add_meta_box' ) );
add_action( 'save_post', array( $this, 'iam_save_meta_box_data' ) );
}
/**
* Adds a meta box to the post editing screen
*/
public function iam_add_meta_box(){
add_meta_box(
$this->_meta_box['id'],
$this->_meta_box['title'],
array( &$this, 'iam_display_custom_meta_box' ),
$this->_meta_box['pages'],
$this->_meta_box['context'],
$this->_meta_box['priority']
);
}
/**
* Render Meta Box content.
*/
public function iam_display_custom_meta_box() {
global $post;
// Add an nonce field so we can check for it later.
wp_nonce_field( 'iam_nonce_check', 'iam_nonce_check_value' );
echo '<div class="metabox-wrapper">';
foreach ( $this->_meta_box['fields'] as $field) {
// get current post meta data
$meta = get_post_meta( $post->ID, $field['id'], true );
echo '<div class="metabox-fields metabox_' , $field['type'] , '">';
echo '<label for="', $field['id'] , '">', $field['name'] , '</label>';
switch ( $field['type'] ) {
case 'text':
echo '<input type="text" name="', $field['id'] , '" id="', $field['id'] , '" value="', $meta , '" />';
echo '<p class="meta-desc">' , $field['desc'] , '</p>';
break;
case 'textarea':
echo '<textarea name="', $field['id'], '" id="', $field['id'], '" cols="60" rows="4" style="width:97%">', $meta , '</textarea>';
echo '<p class="meta-desc">' , $field['desc'] , '</p>';
break;
case 'select':
echo '<select name="', $field['id'], '" id="', $field['id'], '">';
foreach ( $field['options'] as $option ) {
echo '<option', $meta == $option ? ' selected="selected"' : '', '>', $option, '</option>';
}
echo '</select>';
break;
case 'radio':
foreach ( $field['options'] as $option ) {
echo '<input type="radio" name="', $field['id'], '" value="', $option['value'], '"', $meta == $option['value'] ? ' checked="checked"' : '', ' />', $option['name'];
}
break;
case 'checkbox':
echo '<input type="checkbox" name="', $field['id'], '" id="', $field['id'], '"', $meta ? ' checked="checked"' : '', ' />';
break;
}
echo '</div>';
}
echo '</div>';
}
/**
* Save the meta when the post is saved.
*/
public function iam_save_meta_box_data( $post_id ){
if( $this->iam_user_can_save( $post_id, 'iam_nonce_check_value' ) ) {
// Checks for input and sanitizes/saves if needed
foreach ( $this->_meta_box['fields'] as $field ) {
$old = get_post_meta( $post_id, $field['id'], true );
$new = sanitize_text_field( $_POST[ $field['id'] ] );
if ( $new && $new != $old ) {
update_post_meta( $post_id, $field['id'], $new );
} elseif ( '' == $new && $old ) {
delete_post_meta( $post_id, $field['id'], $old );
}
}
}
}
/**
* Determines whether or not the current user has the ability to save meta
* data associated with this post.
*
* #param int $post_id The ID of the post being save
* #param bool Whether or not the user has the ability to save this post.
*/
public function iam_user_can_save( $post_id, $nonce ){
// Checks save status
$is_autosave = wp_is_post_autosave( $post_id );
$is_revision = wp_is_post_revision( $post_id );
$is_valid_nonce = ( isset( $_POST[ $nonce ] ) && wp_verify_nonce( $_POST[ $nonce ], 'iam_nonce_check' ) ) ? 'true' : 'false';
// Return true if the user is able to save; otherwise, false.
return ! ( $is_autosave || $is_revision ) && $is_valid_nonce;
}
}
// Instantiate theme
if ( class_exists( 'Custom_Meta_Boxes' ) ){
foreach ( $meta_boxes as $meta_box ) {
$my_box = new Custom_Meta_Boxes( $meta_box );
}
}
?>
Thanks for any helps.
Related
I am creating a multi-vendor site using WCFM at the moment. I'm trying to recreate the UI from a plugin I saw somewhere but integrating the metabox I made with WooCommerce is the challenging part.
Essentially I want the vendors to just enter the prices for variations than to do it manually by adding the attribute then save it as a variation etc. I have all the metabox plugins. This is the code I have for the recreated metabox:
<?php
add_filter( 'rwmb_meta_boxes', 'yb_flower_price_box' );
function yb_flower_price_box( $meta_boxes ) {
$prefix = '';
$meta_boxes[] = [
'title' => __( 'Flower Price by Grams', 'yb' ),
'id' => 'flower-price-by-grams',
'post_types' => ['flower', 'product', 'product_variation'],
'storage_type' => 'custom_table',
'table' => 'Flower price',
'fields' => [
[
'name' => __( '1 gram', 'yb' ),
'id' => $prefix . '1_sm7qy7uyf5',
'type' => 'text',
'size' => 3,
'columns' => 1,
],
[
'name' => __( '2 grams ', 'yb' ),
'id' => $prefix . '2_btw3zbak44j',
'type' => 'text',
'size' => 3,
'columns' => 1,
],
[
'name' => __( '3.5 grams', 'yb' ),
'id' => $prefix . '3.5',
'type' => 'text',
'size' => 3,
'columns' => 1,
],
[
'name' => __( '7 grams', 'yb' ),
'id' => $prefix . '7g',
'type' => 'text',
'size' => 3,
'columns' => 1,
],
[
'name' => __( '14 grams', 'yb' ),
'id' => $prefix . '14_q6tlu8qi78',
'type' => 'text',
'size' => 3,
'columns' => 1,
],
[
'name' => __( '28 grams ', 'yb' ),
'id' => $prefix . '28_ufjrtw5u8oc',
'type' => 'text',
'size' => 3,
'columns' => 1,
],
],
'validation' => [
'rules' => [
$prefix . '2_btw3zbak44j' => [
'number' => true,
],
$prefix . '3' => [
'number' => true,
],
],
],
];
return $meta_boxes;
}
This is one of the code snippets from the plugin I'm trying to recreate:
/**
* Product Prices metabox
*
* Adds a product prices metabox to all of the above custom post types
*
* #since 1.0.0
*/
function wp_dispensary_product_prices_metabox() {
// Add metabox.
add_meta_box(
'wp_dispensary_product_prices',
__( 'Product prices', 'wp-dispensary' ),
'wp_dispensary_product_prices_metabox_content',
'products',
'normal',
'default'
);
}
add_action( 'add_meta_boxes', 'wp_dispensary_product_prices_metabox' );
/**
* Product Prices
*
* #return void
*/
function wp_dispensary_product_prices_metabox_content() {
global $post;
/** Noncename needed to verify where the data originated */
$string = '<input type="hidden" name="wpd_product_prices_meta_noncename" id="wpd_product_prices_meta_noncename" value="' .
wp_create_nonce( plugin_basename( __FILE__ ) ) . '" />';
// Get product prices.
$product_prices = wpd_product_prices();
// Loop through product prices.
foreach ( $product_prices as $key=>$value ) {
$string .= '<div class="input-field product-price">';
$string .= '<p>' . $value . '</p>';
$string .= '<input type="text" name="' . $key . '" value="' . get_post_meta( $post->ID, $key, true ) . '" class="widefat" />';
$string .= '</div>';
}
// Get concentrates prices.
$concentrates_prices = wpd_product_prices( 'concentrates' );
// Loop through concentrates prices.
foreach ( $concentrates_prices as $key=>$value ) {
$string .= '<div class="input-field concentrates-price">';
$string .= '<p>' . $value . '</p>';
$string .= '<input type="text" name="' . $key . '" value="' . get_post_meta( $post->ID, $key, true ) . '" class="widefat" />';
$string .= '</div>';
}
// Get flower prices.
$flower_prices = wpd_product_prices( 'flowers' );
// Loop through flower prices.
foreach ( $flower_prices as $key=>$value ) {
$string .= '<div class="input-field flower-price">';
$string .= '<p>' . $value . '</p>';
$string .= '<input type="text" name="' . $key . '" value="' . get_post_meta( $post->ID, $key, true ) . '" class="widefat" />';
$string .= '</div>';
}
echo $string;
}
/**
* Save the Metabox Data
*
* #param int $post_id
* #param object $post
* #return void
*/
function wp_dispensary_product_prices_metabox_save( $post_id, $post ) {
/**
* Verify this came from the our screen and with proper authorization,
* because save_post can be triggered at other times
*/
if (
! isset( $_POST['wpd_product_prices_meta_noncename'] ) ||
! wp_verify_nonce( $_POST['wpd_product_prices_meta_noncename'], plugin_basename( __FILE__ ) )
) {
return $post->ID;
}
/** Is the user allowed to edit the post or page? */
if ( ! current_user_can( 'edit_post', $post->ID ) ) {
return $post->ID;
}
/**
* OK, we're authenticated: we need to find and save the data
* We'll put it into an array to make it easier to loop though.
*/
$prices_meta = array();
if ( '' != filter_input( INPUT_POST, 'price_each' ) ) {
$prices_meta['price_each'] = esc_html( $_POST['price_each'] );
}
if ( '' != filter_input( INPUT_POST, 'price_per_pack' ) ) {
$prices_meta['price_per_pack'] = esc_html( $_POST['price_per_pack'] );
}
if ( '' != filter_input( INPUT_POST, 'units_per_pack' ) ) {
$prices_meta['units_per_pack'] = esc_html( $_POST['units_per_pack'] );
}
if ( '' != filter_input( INPUT_POST, 'price_half_gram' ) ) {
$prices_meta['price_half_gram'] = esc_html( $_POST['price_half_gram'] );
}
if ( '' != filter_input( INPUT_POST, 'price_gram' ) ) {
$prices_meta['price_gram'] = esc_html( $_POST['price_gram'] );
}
if ( '' != filter_input( INPUT_POST, 'price_two_grams' ) ) {
$prices_meta['price_two_grams'] = esc_html( $_POST['price_two_grams'] );
}
if ( '' != filter_input( INPUT_POST, 'price_eighth' ) ) {
$prices_meta['price_eighth'] = esc_html( $_POST['price_eighth'] );
}
if ( '' != filter_input( INPUT_POST, 'price_five_grams' ) ) {
$prices_meta['price_five_grams'] = esc_html( $_POST['price_five_grams'] );
}
if ( '' != filter_input( INPUT_POST, 'price_quarter_ounce' ) ) {
$prices_meta['price_quarter_ounce'] = esc_html( $_POST['price_quarter_ounce'] );
}
if ( '' != filter_input( INPUT_POST, 'price_half_ounce' ) ) {
$prices_meta['price_half_ounce'] = esc_html( $_POST['price_half_ounce'] );
}
if ( '' != filter_input( INPUT_POST, 'price_ounce' ) ) {
$prices_meta['price_ounce'] = esc_html( $_POST['price_ounce'] );
}
// Save $prices_meta as metadata.
foreach ( $prices_meta as $key => $value ) {
// Bail on post revisions.
if ( 'revision' === $post->post_type ) {
return;
}
$value = implode( ',', (array) $value );
// Check for meta value and either update or add the metadata.
if ( get_post_meta( $post->ID, $key, false ) ) {
update_post_meta( $post->ID, $key, $value );
} else {
add_post_meta( $post->ID, $key, $value );
}
// Delete the metavalue if blank.
if ( ! $value ) {
delete_post_meta( $post->ID, $key );
}
}
}
add_action( 'save_post', 'wp_dispensary_product_prices_metabox_save', 1, 2 );
I am a newbie and I would greatly appreciate any help or a point in the right direction. Thank you.
I am developing an online store that offers local delivery days according to zip code. I have limited the zip codes with a selection using this snippet
add_filter ('woocommerce_default_address_fields', 'custom_override_default_postcode_field');
function custom_override_default_postcode_field ($address_fields) {
// Zip codes
$postcode_array = array (
'28001' => '28001',
'28002' => '28002',
'28923' => '28923'
);
$address_fields['postcode']['type'] = 'select';
$address_fields['postcode']['options'] = $postcode_array;
return $address_fields;
}
I am using the plugin woocommerce checkout field editor and I've added field "delivery days", which is a select field with the values Monday, Tuesday, Wednesday ...
Now, I want to make the second field dependent on the first, according to my client's specifications, for example:
Zip code 28001 / Delivery Days Monday and Wednesday
Zip code 28002 / Delivery days Tuesday and Thursday
Zip code 28923 / Delivery Days Friday and Sunday
That is, when the buyer chooses a postal code, it only shows him to choose the corresponding days.
If there is a way to do that without the plugin I think it will be better
Thanks a lot for your help,
Luis
This required a lots of customization. Try the below code. code goes to your active theme functions.php file.
add_filter ('woocommerce_default_address_fields', 'custom_override_default_postcode_field');
function custom_override_default_postcode_field ($address_fields) {
// Zip codes
$postcode_array = array (
'28001' => '28001',
'28002' => '28002',
'28923' => '28923'
);
$address_fields['postcode']['type'] = 'select';
$address_fields['postcode']['options'] = $postcode_array;
return $address_fields;
}
You can add a new dropdown field Delivery days using the woocommerce_checkout_fields filter hook.
add_filter( 'woocommerce_checkout_fields' , 'custom_delivery_days_woocommerce_billing_fields' );
function custom_delivery_days_woocommerce_billing_fields( $fields ) {
$fields['billing']['delivery_days'] = array(
'label' => __('Delivery days', 'woocommerce'),
'placeholder' => _x('dropdown', 'placeholder', 'woocommerce'),
'required' => true,
'class' => array('form-row-wide'),
'clear' => true,
'priority' => 95,
'type' => 'custom_select',
'options' => array(
'monday' => array(
'label' => __('Monday', 'woocommerce' ),
'data_attr_name' => 'post-code',
'data_attr_value' => '28001',
),
'tuesday' => array(
'label' => __('Tuesday', 'woocommerce' ),
'data_attr_name' => 'post-code',
'data_attr_value' => '28002',
),
'wednesday' => array(
'label' => __('Wednesday', 'woocommerce' ),
'data_attr_name' => 'post-code',
'data_attr_value' => '28001',
),
'thursday' => array(
'label' => __('Thursday', 'woocommerce' ),
'data_attr_name' => 'post-code',
'data_attr_value' => '28002',
),
'friday' => array(
'label' => __('Friday', 'woocommerce' ),
'data_attr_name' => 'post-code',
'data_attr_value' => '28923',
),
'sunday' => array(
'label' => __('Sunday', 'woocommerce' ),
'data_attr_name' => 'post-code',
'data_attr_value' => '28923',
)
)
);
return $fields;
}
Now as per your requirement I will use data attr for show hide select option based on another select option. but in woocommerce default select dropdown, there is now a way to add data attr to select. so for this, I created the custom_select type so we can add data attr. so as you can see in above woocommerce_checkout_fields filter hook i set 'type' => 'custom_select',.
You can add new your custom field type using the woocommerce_form_field filter hook.
add_filter( 'woocommerce_form_field', 'add_custom_select', 10 , 4 );
function add_custom_select( $field, $key, $args, $value ){
if( $args['type'] == 'custom_select' ){
if ( $args['required'] ) {
$args['class'][] = 'validate-required';
$required = ' <abbr class="required" title="' . esc_attr__( 'required', 'woocommerce' ) . '">*</abbr>';
} else {
$required = ' <span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
}
$field = '';
$options = '';
if ( ! empty( $args['options'] ) ) {
foreach ( $args['options'] as $option_key => $option_text ) {
if ( '' === $option_key ) {
// If we have a blank option, select2 needs a placeholder.
if ( empty( $args['placeholder'] ) ) {
$args['placeholder'] = $option_text['label'] ? $option_text['label'] : __( 'Choose an option', 'woocommerce' );
}
$custom_attributes[] = 'data-allow_clear="true"';
}
$options .= '<option value="' . esc_attr( $option_key ) . '" ' . selected( $value, $option_key, false ) . ' data-'.$option_text['data_attr_name'].'="'.$option_text['data_attr_value'].'" >' . esc_html( $option_text['label'] ) . '</option>';
}
$field .= '<select name="' . esc_attr( $key ) . '" id="' . esc_attr( $args['id'] ) . '" class="select ' . esc_attr( implode( ' ', $args['input_class'] ) ) . '" ' . implode( ' ', $custom_attributes ) . ' data-placeholder="' . esc_attr( $args['placeholder'] ) . '">
' . $options . '
</select>';
}
$field_container = '<p class="form-row %1$s" id="%2$s" data-priority="' . esc_attr( $sort ) . '">%3$s</p>';
if ( ! empty( $field ) ) {
$field_html = '';
if ( $args['label'] && 'checkbox' !== $args['type'] ) {
$field_html .= '<label for="' . esc_attr( $label_id ) . '" class="' . esc_attr( implode( ' ', $args['label_class'] ) ) . '">' . wp_kses_post( $args['label'] ) . $required . '</label>';
}
$field_html .= '<span class="woocommerce-input-wrapper">' . $field;
if ( $args['description'] ) {
$field_html .= '<span class="description" id="' . esc_attr( $args['id'] ) . '-description" aria-hidden="true">' . wp_kses_post( $args['description'] ) . '</span>';
}
$field_html .= '</span>';
$container_class = esc_attr( implode( ' ', $args['class'] ) );
$container_id = esc_attr( $args['id'] ) . '_field';
$field = sprintf( $field_container, $container_class, $container_id, $field_html );
}
return $field;
}
return $field;
}
Now on the billing_postcode change, you have to get the billing_postcode value that you need to compare with delivery_days dropdown option data-post-code and based on that you can show hide.
function add_custom_js(){
?>
<script type="text/javascript">
(function($){
$('#billing_postcode').on('change', function(){
var postcode = $(this).val();
var days = $('#delivery_days');
var i = 1;
$('option', days).filter(function(){
if ( $(this).attr('data-post-code') === postcode ) {
$(this).show();
if( i == 1 ){
$("#delivery_days").val($(this).val());
}
i++;
} else {
$(this).hide();
}
});
});
$('#billing_postcode').trigger('change');
})(jQuery);
</script>
<?php
}
add_action( 'wp_footer', 'add_custom_js', 10, 1 );
Tested and works.
I used this code below which is very useful in wordpress function.php for contact form 7 to select images as checkbox but its not receiving in email, please help me in this matter, thanks
Here is the code
function add_shortcode_imageradio() {
wpcf7_add_shortcode( 'imageradio', 'imageradio_handler', true );
}
add_action( 'wpcf7_init', 'add_shortcode_imageradio' );
function imageradio_handler( $tag ){
$tag = new WPCF7_FormTag( $tag );
$atts = array(
'type' => 'radio',
'name' => $tag->name,
'list' => $tag->name . '-options' );
$input = sprintf(
'<input %s />',
wpcf7_format_atts( $atts ) );
$datalist = '';
$datalist .= '<div class="imgradio">';
foreach ( $tag->values as $val ) {
list($radiovalue,$imagepath) = explode("!", $val
);
$datalist .= sprintf(
'<label><input type="radio" name="%s" value="%s" class="hideradio" /><img src="%s"></label>', $tag->name, $radiovalue, $imagepath
);
}
$datalist .= '</div>';
return $datalist;
}
Here is the page I am working on:
https://www.commforceintl.com/lost-and-found-english/
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 ) {
I'm trying to change the default +/- quantity selector into a drop down menu with a given amount of number options (i.e. 1-10).
Anyone know how to accomplish this?
Let me know if you'd like me to post any of the code related to this.
I'd like to do this too. So far I found that the quantity markup is generated in woocommerce/templates/single-product/add-to-cart/quantity.php. You could make a copy of this file in a minimal mirror of the woocommerce/templates directory structure in your theme folder, e.g. in this case copy it to yourtheme/woocommerce/single-product/add-to-cart. There you can edit it without altering the plug-in and risk it being overwritten when the plug-in is updated.
There is a plugin that will do this for you called WooCommerce Advanced Product Quantities, it's free and will allow you to set Minimum, Maximum and Step values for all product quantity inputs. Set rules on a per product, per category/tag or site wide.
http://wordpress.org/plugins/woocommerce-incremental-product-quantities/
It also works with WooCommerce Thumbnail Input Quantities which will put those quantity boxes in all of your product thumbnail boxes.
http://wordpress.org/plugins/woocommerce-thumbnail-input-quantities/
Enjoy! Full disclosure, I'm the plugin author.
I have not tried this, but I found this code http://bastutor.blogspot.ca/2014/01/woocommerce-change-input-quantity-to-dropdown.html
/* Change Product Quantity Input to Dropdown */
function woocommerce_quantity_input() {
global $product;
$defaults = array(
'input_name' => 'quantity',
'input_value' => '1',
'max_value' => apply_filters( 'woocommerce_quantity_input_max', '', $product ),
'min_value' => apply_filters( 'woocommerce_quantity_input_min', '', $product ),
'step' => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
'style' => apply_filters( 'woocommerce_quantity_style', 'float:left; margin-right:10px;', $product )
);
if (!empty($defaults['min_value']))
$min = $defaults['min_value'];
else $min = 1;
if (!empty($defaults['max_value']))
$max = $defaults['max_value'];
else $max = 20;
if (!empty($defaults['step']))
$step = $defaults['step'];
else $step = 1;
$options = '';
for($count = $min;$count <= $max;$count = $count+$step){
$options .= '<option value="' . $count . '">' . $count . '</option>';
}
echo '<div class="quantity_select" style="' . $defaults['style'] . '"><select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product quantity input tooltip', 'woocommerce' ) . '" class="qty">' . $options . '</select></div>';
}
You need to override the template "quantity-input.php" to do so add a file with the name as "quantity-input.php" in your-theme/woocommerce/global/ folder then you can make the changes in that file, now wordpress will use your file to display quantity input html.
Paste this in your function.php file
The dropdown list works both on the product page and in the shopping cart.
Image
function woocommerce_quantity_input($args = array(), $product = null, $echo = true) {
if (is_null($product)) {
$product = $GLOBALS['product'];
}
// Default values
$defaults = array(
'input_name' => 'quantity',
'input_value' => '1',
'max_value' => apply_filters('woocommerce_quantity_input_max', -1, $product),
'min_value' => apply_filters('woocommerce_quantity_input_min', 0, $product),
'step' => 1,
);
$args = apply_filters('woocommerce_quantity_input_args', wp_parse_args($args, $defaults), $product);
$args['min_value'] = max($args['min_value'], 0);
$args['max_value'] = 0 < $args['max_value'] ? $args['max_value'] : 10;
if (
'' !== $args['max_value'] && $args['max_value'] < $args['min_value']
) {
$args['max_value'] = $args['min_value'];
}
$options = '';
// Add loop
for ($count = $args['min_value']; $count <= $args['max_value']; $count = $count + $args['step']) {
// Cart item quantity defined?
if ('' !== $args['input_value'] && $args['input_value'] >= 1 && $count == $args['input_value']) {
$selected = 'selected';
} else {
$selected = '';
}
$options .= '<option value="' . $count . '"' . $selected . '>' . $count . '</option>';
}
$html = '<div><div class="quantity form-select-wrapper"><select class="form- select" name="' . $args['input_name'] . '">' . $options . '</select></div><!--/.form-select-wrapper --></div>';
if ($echo) {
echo $html;
} else {
return $html;
}
}
Hi paste this in your function.php file
function woocommerce_quantity_input($data = null) {
global $product;
if (!$data) {
$defaults = array(
'input_name' => 'quantity',
'input_value' => '1',
'max_value' => apply_filters( 'woocommerce_quantity_input_max', '', $product ),
'min_value' => apply_filters( 'woocommerce_quantity_input_min', '', $product ),
'step' => apply_filters( 'woocommerce_quantity_input_step', '1', $product ),
'style' => apply_filters( 'woocommerce_quantity_style', 'float:left;', $product )
);
} else {
$defaults = array(
'input_name' => $data['input_name'],
'input_value' => $data['input_value'],
'step' => apply_filters( 'cw_woocommerce_quantity_input_step', '1', $product ),
'max_value' => apply_filters( 'cw_woocommerce_quantity_input_max', '', $product ),
'min_value' => apply_filters( 'cw_woocommerce_quantity_input_min', '', $product ),
'style' => apply_filters( 'cw_woocommerce_quantity_style', 'float:left;', $product )
);
}
if ( ! empty( $defaults['min_value'] ) )
$min = $defaults['min_value'];
else $min = 1;
if ( ! empty( $defaults['max_value'] ) )
$max = $defaults['max_value'];
else $max = 15;
if ( ! empty( $defaults['step'] ) )
$step = $defaults['step'];
else $step = 1;
$options = '';
for ( $count = $min; $count <= $max; $count = $count+$step ) {
$selected = $count === $defaults['input_value'] ? ' selected' : '';
$options .= '<option value="' . $count . '"'.$selected.'>' . $count . '</option>';
}
echo '<div class="cw_quantity_select" style="' . $defaults['style'] . '"><select name="' . esc_attr( $defaults['input_name'] ) . '" title="' . _x( 'Qty', 'Product Description', 'woocommerce' ) . '" class="cw_qty">' . $options . '</select></div>';
}