Wordpress options not saving - wordpress

I'm trying to build a theme options page and have been successful until i added a tabs menu. Since adding the tabs the options no longer save.
Here is my code.
<?php
function mb_admin_menu() {
$page = add_theme_page( 'Theme Options', 'Theme Options', 'edit_theme_options', 'mb-theme-options', 'mb_theme_options' );
add_action( 'admin_print_styles-' . $page, 'mb_admin_scripts' );
}
add_action( 'admin_menu', 'mb_admin_menu' );
function mb_admin_scripts() {
wp_enqueue_style( 'farbtastic' );
wp_enqueue_script( 'farbtastic' );
wp_enqueue_script( 'my-theme-options', get_template_directory_uri() . '/js/theme-options.js', array( 'farbtastic', 'jquery' ) );
}
function mb_theme_options( $active_tab = '' ) {
?>
<div class="wrap">
<div id="icon-themes" class="icon32" ><br></div>
<h2>My Theme Options</h2>
<?php
if( isset( $_GET[ 'tab' ] ) ) {
$active_tab = $_GET[ 'tab' ];
} // end if
?>
<?php if( isset( $_GET[ 'tab' ] ) ) {
$active_tab = $_GET[ 'tab' ];
} else if( $active_tab == 'general-options' ) {
$active_tab = 'general-options';
} else {
$active_tab = 'color-options';
} // end if/else ?>
<h2 class="nav-tab-wrapper">
Color Options
General Options
</h2>
<form method="post" action="options.php">
<?php wp_nonce_field( 'update-options' ); ?>
<?php if( $active_tab == 'color-options' ) {
settings_fields( 'mb-color-options' );
do_settings_sections( 'mb-color-options' );
} else {
settings_fields( 'mb-general-options' );
do_settings_sections( 'mb-general-options' );
} // end if/else
submit_button(); ?>
</form>
</div>
<?php
}
function mb_admin_init() {
register_setting( 'mb-color-options', 'mb-color-options' );
add_settings_section( 'section_colors', 'Color Settings', 'mb_section_colors', 'mb-color-options' );
add_settings_field( 'link_color', 'Link Color', 'mb_setting_color', 'mb-color-options', 'section_colors' );
add_settings_field( 'link_hover_color', 'Link Hover Color', 'mb_hover_setting_color', 'mb-color-options', 'section_colors' );
}
add_action( 'admin_init', 'mb_admin_init' );
function mb_section_colors() {
_e( 'The general section description goes here.' );
}
function mb_setting_color() {
$options = get_option( 'mb-color-options' );
?>
<input class="link_color" type="text" name="mb-theme-options[link_color]" value="<?php echo esc_attr( $options['link_color'] ); ?>" />
<input type='button' class='select_color button-secondary' value='Select Color'>
<div class='color_picker' style='z-index: 100; background:#f1f1f1; position:absolute; display:none;'></div>
<input type='button' class='reset_color button-secondary' value='Reset'>
<?php
}
function mb_hover_setting_color() {
$options = get_option( 'mb-color-options' );
?>
<input class="link_color" type="text" name="mb-theme-options[link_hover_color]" value="<?php echo esc_attr( $options['link_hover_color'] ); ?>" />
<input type='button' class='select_color button-secondary' value='Select Color'>
<div class='color_picker' style='z-index: 100; background:#f1f1f1; position:absolute; display:none;'></div>
<input type='button' class='reset_color button-secondary' value='Reset'>
<?php
}
function mb_link_color() {
$options = get_option( 'mb-theme-options' );
$link_color = $options['link_color'];
$link_hover_color = $options['link_hover_color'];
echo "<style> a { color: $link_color; } a:hover { color: $link_hover_color; } </style>";
}
add_action( 'wp_enqueue_scripts', 'mb_link_color' );
?>
Is anyone able to point out why the options are not saving since adding the tabs ?

Is your problem solved ?? If not then try this one !! It is working for me.
I just changed the way you are registering the settings. Don't know what that was but it worked. Might work for you as well.
<?php
function mb_admin_menu() {
$page = add_theme_page( 'Theme Options', 'Theme Options', 'edit_theme_options', 'mb-theme-options', 'mb_theme_options' );
add_action( 'admin_print_styles-' . $page, 'mb_admin_scripts' );
}
add_action( 'admin_menu', 'mb_admin_menu' );
function mb_admin_scripts() {
wp_enqueue_style( 'farbtastic' );
wp_enqueue_script( 'farbtastic' );
wp_enqueue_script( 'my-theme-options', get_template_directory_uri() . '/js/theme-options.js', array( 'farbtastic', 'jquery' ) );
}
function mb_theme_options( $active_tab = '' ) {
?>
<div class="wrap">
<div id="icon-themes" class="icon32" ><br></div>
<h2>My Theme Options</h2>
<?php
if( isset( $_GET[ 'tab' ] ) ) {
$active_tab = $_GET[ 'tab' ];
}
?>
<h2 class="nav-tab-wrapper">
Color Options
General Options
</h2>
<form method="post" action="options.php">
<?php wp_nonce_field( 'update-options' ); ?>
<?php if( isset( $_GET[ 'tab' ] ) ) {
$active_tab = $_GET[ 'tab' ];
switch($active_tab) {
case 'color-options' :
echo $active_tab;
settings_fields('mb-color-options');
do_settings_sections('mb-color-options');
break;
case 'general-options' :
echo $active_tab;
settings_fields('mb-general-options');
do_settings_sections('mb-general-options');
break;
}
submit_button();
}
?>
</form>
</div>
<?php
}
function mb_admin_init() {
register_setting( 'mb-color-options', 'link_color' );
register_setting( 'mb-color-options', 'link_hover_color' );
register_setting( 'mb-general-options', 'title' );
add_settings_section( 'section_colors', 'Color Settings', null, 'mb-color-options' );
add_settings_field( 'link_color', 'Link Color', 'mb_setting_color', 'mb-color-options', 'section_colors' );
add_settings_field( 'link_hover_color', 'Link Hover Color', 'mb_hover_setting_color', 'mb-color-options', 'section_colors' );
add_settings_section( 'section_options', 'General Options', null, 'mb-general-options' );
add_settings_field( 'title', 'Page Title', 'mb_title_option', 'mb-general-options', 'section_options' );
}
add_action( 'admin_init', 'mb_admin_init' );
function mb_general_option() {
_e( 'The general section description goes here.');
}
function mb_title_option() {
$options = get_option('title');
?>
<input type="text" name="title" id="title" value="<?php echo $options;?>"/>
<?php
}
function mb_section_colors() {
_e( 'The general section description goes here.' );
}
function mb_setting_color() {
$link_color = get_option( 'link_color' );
?>
<input class="link_color" type="text" name="link_color" value="<?php echo $link_color; ?>" />
<input type='button' class='select_color button-secondary' value='Select Color'>
<div class='color_picker' style='z-index: 100; background:#f1f1f1; position:absolute; display:none;'></div>
<input type='button' class='reset_color button-secondary' value='Reset'>
<?php
}
function mb_hover_setting_color() {
$link_hover_color = get_option( 'link_hover_color' );
?>
<input class="link_color" type="text" name="link_hover_color" value="<?php echo $link_hover_color; ?>" />
<input type='button' class='select_color button-secondary' value='Select Color'>
<div class='color_picker' style='z-index: 100; background:#f1f1f1; position:absolute; display:none;'></div>
<input type='button' class='reset_color button-secondary' value='Reset'>
<?php
}
function mb_link_color() {
$options = get_option( 'mb-theme-options' );
$link_color = $options['link_color'];
$link_hover_color = $options['link_hover_color'];
echo "<style> a { color: $link_color; } a:hover { color: $link_hover_color; } </style>";
}
add_action( 'wp_enqueue_scripts', 'mb_link_color' );

Related

Repeater Fields in Widget Form Function in Wordpress

I've been trying to add repeater fields in my wordpress widget form. I found two plugin examples but they don't work anymore. When I click on "Add row", nothing happens.
https://www.commoninja.com/discover/wordpress/plugin/social-repeater-widget
https://www.zealousweb.com/wordpress-plugins/repeater-entries-widget/
I noticed both use jquery in their widgets which I guess doesn't work anymore in the newer Wordpress versions?
/**
* Back-end widget form.
*
* #see WP_Widget::form()
*
* #param array $instance Previously saved values from database.
*/
public function form($instance)
{
$max_entries = get_option( 'max_entries' );
$max_entries = (empty($max_entries)) ? '5' : $max_entries;
$widget_add_id = $this->id . "-add";
$title = !empty($instance['title']) ? $instance['title'] : __('Social Repeater Widget', 'srw-widget');
$srw_html = '<p>';
$srw_html .= '<label for="'.$this->get_field_id('title').'"> '. __( 'Widget Title', 'srw-widget' ) .' :</label>';
$srw_html .= '<input id="'.$this->get_field_id('title').'" name="'.$this->get_field_name('title').'" type="text" value="'.$title.'" />';
$srw_html .= '<div class="'.$widget_add_id.'-input-containers"><div id="entries">';
for( $i =0; $i<$max_entries; $i++){
if(isset($instance['block-' . $i]) || isset($instance['social_platform-' . $i]))
{
$srw_tab_title = !empty($instance['social_platform-' . $i]) ? $instance['social_platform-' . $i] : __( 'Add Social Profile Details', 'srw-widget' );
$display = (!isset($instance['block-' . $i]) || ($instance['block-' . $i] == "")) ? 'style="display:none;"' : '';
$srw_html .= '<div id="entry'.($i+1).'" '.$display.' class="entrys"><span class="entry-title" onclick = "slider(this);"> '.$srw_tab_title.' </span>';
$srw_html .= '<div class="entry-desc cf">';
$srw_html .= '<input id="'.$this->get_field_id('block-' . $i ).'" name="'.$this->get_field_name('block-' . $i ).'" type="hidden" value="'.$instance['block-' . $i].'">';
$social_platform = esc_attr( $instance['social_platform-' . $i] );
$social_platform_link = esc_attr( $instance['social_platform_link-' . $i] );
$srw_html .= '<p class="last desc">';
$srw_html .= '<label for="'.$this->get_field_id('social_platform-' . $i).'"> '. __( 'Social Platform', 'srw-widget' ) .' :</label>';
$srw_html .= '<input class="widefat" id="'.$this->get_field_id('social_platform-' . $i).'" name="'.$this->get_field_name('social_platform-' . $i).'" type="text" value="'.$social_platform.'" placeholder="'.__( 'Enter Social Platform name', 'srw-widget' ).'" />';
$srw_html .= '</p><p>';
$srw_html .= '<label for="'.$this->get_field_id('social_platform_link-' . $i).'"> '. __('Social platform Link', 'srw-widget' ) .' :</label>';
$srw_html .= '<input class="widefat" id="'.$this->get_field_id('social_platform_link-' . $i).'" name="'.$this->get_field_name('social_platform_link-' . $i).'" type="url" value="'.$social_platform_link.'" placeholder="'.__( 'Enter Social Platform Link', 'srw-widget' ).'"/>';
$srw_html .= '</p>';
/* end wrapper with delete entry option */
$srw_html .= '<p><span class="delete-row">'. __( 'Delete Row', 'srw-widget' ) .'</span></p>';
$srw_html .= '</div></div>';
}
}
$srw_html .= '</div></div>';
$srw_html .= '<div id="message">'. __( 'Sorry, you reached to the limit of','srw-widget') .' "'.$max_entries.'" '. __( 'maximum entries', 'srw-widget' ) .'.</div>' ;
$srw_html .= '<div class="'.$widget_add_id.'" style="display:none;">' . __('Add New Platform', 'srw-widget') . '</div>';
?>
<script>
jQuery(document).ready(function(e) {
jQuery.each(jQuery(".<?php echo $widget_add_id; ?>-input-containers #entries").children(), function(){
if(jQuery(this).find('input').val() != ''){
jQuery(this).show();
}
});
jQuery(".<?php echo $widget_add_id; ?>" ).bind('click', function(e) {
var rows = 0;
jQuery.each(jQuery(".<?php echo $widget_add_id; ?>-input-containers #entries").children(), function(){
if(jQuery(this).find('input').val() == ''){
jQuery(this).find(".entry-title").addClass("active");
jQuery(this).find(".entry-desc").slideDown();
jQuery(this).find('input').first().val('0');
jQuery(this).show();
return false;
}
else{
rows++;
jQuery(this).show();
jQuery(this).find(".entry-title").removeClass("active");
jQuery(this).find(".entry-desc").slideUp();
}
});
if(rows == '<?php echo $max_entries;?>')
{
jQuery("#rew_container #message").show();
}
});
jQuery(".delete-row" ).bind('click', function(e) {
var count = 1;
var current = jQuery(this).closest('.entrys').attr('id');
jQuery.each(jQuery("#entries #"+current+" .entry-desc").children(), function(){
jQuery(this).val('');
});
jQuery.each(jQuery("#entries #"+current+" .entry-desc p").children(), function(){
jQuery(this).val('');
});
jQuery('#entries #'+current+" .entry-title").removeClass('active');
jQuery('#entries #'+current+" .entry-desc").hide();
jQuery('#entries #'+current).remove();
jQuery.each(jQuery(".<?php echo $widget_add_id; ?>-input-containers #entries").children(), function(){
if(jQuery(this).find('input').val() != ''){
jQuery(this).find('input').first().val(count);
}
count++;
});
});
});
</script>
<style>
.cf:before, .cf:after { content: ""; display: table; }
.cf:after { clear: both; }
.cf { zoom: 1; }
.clear { clear: both; }
.clearfix:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
.clearfix { display: inline-block; }
* html .clearfix { height: 1%; }
.clearfix { display: block;}
#rew_container input,select,textarea{ float: right;width: 60%;}
#rew_container label{width:40%;}
<?php echo '.'.$widget_add_id; ?>{
background: #ccc none repeat scroll 0 0;font-weight: bold;margin: 20px 0px 9px;padding: 6px;text-align: center;display:block !important; cursor:pointer;
}
.desc{height:55px;}
#entries{ padding:10px 0 0;}
#entries .entrys{ padding:0; border:1px solid #e5e5e5; margin:10px 0 0; clear:both;}
#entries .entrys:first-child{ margin:0;}
#entries .delete-row{margin-top:20px;float:right;text-decoration: underline;color:red;}
#entries .entry-title{ display:block; font-size:14px; line-height:18px; font-weight:600; background:#f1f1f1; padding:7px 5px; position:relative;}
#entries .entry-title:after{ content: '\f140'; font: 400 20px/1 dashicons; position:absolute; right:10px; top:6px; color:#a0a5aa;}
#entries .entry-title.active:after{ content: '\f142';}
#entries .entry-desc{ display:none; padding:0 10px 10px; border-top:1px solid #e5e5e5;}
#rew_container #entries p.last label{ white-space: pre-line; float:left; width:39%;}
#message{padding:6px;display:none;color:red;font-weight:bold;}
</style>
<div id="rew_container">
<?php echo $srw_html;?>
</div>
<?php
}
I'm pretty new to php and wordpress so any help would be appreciated.
Please try the below code to create a repeater field in the widget.
dd_action( 'widgets_init', 'services_widget' );
function services_widget() {
register_widget( 'services_widget' );
}
class services_widget extends WP_Widget {
public function __construct() {
$widget_ops = array(
'classname' => 'services_widget',
'description' => 'Add a service description.'
);
$control_ops = array( 'width' => 400, 'height' => 350 );
parent::__construct( 'services_widget', 'Services', $widget_ops, $control_ops );
}
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
}
if( have_rows('service', 'widget_' . $args['widget_id']) ):
echo '<ul>';
while ( have_rows('service', 'widget_' . $args['widget_id']) ) : the_row();
echo '<li class="service one-half">';
$title = get_sub_field( 'title', 'widget_' . $args['widget_id'] );
$body = get_sub_field( 'body', 'widget_' . $args['widget_id']);
$button = get_sub_field( 'button', 'widget_' . $args['widget_id'] );
$button_link = get_sub_field( 'button_link', 'widget_' . $args['widget_id'] );
if( $title ) {
echo '<h4>' . $title . '</h4>';
}
if( $body ) {
echo '<p>' . $body . '</p>';
}
if( $body ) {
echo '<a class="more" href="' . $button_link . '">' .$button . '</a>';
}
echo '</li><div class="clearfix"></div></ul>';
endwhile;
endif;
echo $args['after_widget'];
}
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
$filter = isset( $instance['filter'] ) ? $instance['filter'] : 0;
$title = sanitize_text_field( $instance['title'] );
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p>
<p><input id="<?php echo $this->get_field_id('filter'); ?>" name="<?php echo $this->get_field_name('filter'); ?>" type="checkbox"<?php checked( $filter ); ?> /> <label for="<?php echo $this->get_field_id('filter'); ?>"><?php _e('Automatically add paragraphs'); ?></label></p>
<?php
}
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
if ( current_user_can( 'unfiltered_html' ) ) {
$instance['text'] = $new_instance['text'];
} else {
$instance['text'] = wp_kses_post( $new_instance['text'] );
}
$instance['filter'] = ! empty( $new_instance['filter'] );
return $instance;
}
}

Woocommerce custom product slider shortcode

I am working on woocommerce custom product slider shortcode similar to snapshot but I don't know how to use slider with woocommerce. ( https://www.buywholefoodsonline.co.uk/ )
My idea is to use shortcode to call this best selling product slider from database.
I have use this code ( How can I list best selling products in woocommerce ) but did not get similar layout.
I wanted to find a way fit this code in some slider eg. owl carousel ( https://owlcarousel2.github.io/OwlCarousel2/docs/started-installation.html)
I enqueue style and scripts of owl carousel in functions.php.
function loadup_scripts()
{
wp_register_script('jquery', 'https://code.jquery.com/jquery-1.11.0.min.js', NULL, '1.11.0', false);
wp_enqueue_script( 'jquery-migrate', 'http://code.jquery.com/jquery-migrate-1.2.1.min.js', array( 'jquery' ), '', true );
wp_enqueue_script( 'slick-min-js', 'https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick.min.js', array( 'jquery' ), '', true );
wp_enqueue_style( 'slick-css', 'https://cdnjs.cloudflare.com/ajax/libs/slick-carousel/1.8.1/slick-theme.css' );
}
add_action( 'wp_enqueue_scripts', 'loadup_scripts' );
function livewellnutritionuk_action() { ?>
<script>
jQuery(document).ready(function(){
jQuery('.slider').slick({
infinite: false,
speed: 100,
slidesToShow: 4,
slidesToScroll: 4,
autoplay: true,
prevArrow: '<span class="priv_arrow"><</span>',
nextArrow: '<span class="priv_arrow">></span>',
responsive: [
{
breakpoint: 1024,
settings: {
slidesToShow: 3,
slidesToScroll: 3,
infinite: true,
dots: true
}
},
{
breakpoint: 600,
settings: {
slidesToShow: 2,
slidesToScroll: 2
}
},
{
breakpoint: 480,
settings: {
slidesToShow: 1,
slidesToScroll: 1
}
}
]
});
});
</script>
<?php
}
add_action('wp_footer', 'livewellnutritionuk_action');
But need to fit owl carousel for best selling products in this code.
<?php
$args = array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'posts_per_page' => 1,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product; ?>
<div>
<a href="<?php the_permalink(); ?>" id="id-<?php the_id(); ?>" title="<?php the_title(); ?>">
<?php if (has_post_thumbnail( $loop->post->ID ))
echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="product placeholder Image" width="65px" height="115px" />'; ?>
<h3><?php the_title(); ?></h3>
</a>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>
I was struggling to this problem and now I found solution.
function.php
function child_enqueue_styles() {
wp_enqueue_style( 'astra-child-theme-css', get_stylesheet_directory_uri() . '/style.css', array('astra-theme-css'), CHILD_THEME_ASTRA_CHILD_VERSION, 'all' );
wp_enqueue_style('owl-carousel','https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.0.0-beta.2.4/assets/owl.carousel.min.css', '', '','all');
wp_enqueue_style('owl-carousel-2.1.6','https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2/2.0.0-beta.2.4/assets/owl.theme.default.css', '', '','all');
wp_enqueue_script('owl-js','https://cdnjs.cloudflare.com/ajax/libs/OwlCarousel2//2.0.0-beta.2.4/owl.carousel.min.js',array('jquery'),'1.12.4', false);
}
add_action( 'wp_enqueue_scripts', 'child_enqueue_styles', 15 );
function livewellnutritionuk_action() { ?>
<script>
jQuery(document).ready(function(){
jQuery('.slider').slick({
infinite: false,
speed: 100,
slidesToShow: 4,
slidesToScroll: 4,
autoplay: true,
prevArrow: '<span class="priv_arrow"><</span>',
nextArrow: '<span class="priv_arrow">></span>',
responsive: [
{
breakpoint: 1024,
settings: {
slidesToShow: 3,
slidesToScroll: 3,
infinite: true,
dots: true
}
},
{
breakpoint: 600,
settings: {
slidesToShow: 2,
slidesToScroll: 2
}
},
{
breakpoint: 480,
settings: {
slidesToShow: 1,
slidesToScroll: 1
}
}
]
});
});
</script>
<?php
}
add_action('wp_footer', 'livewellnutritionuk_action');
function slider_test(){
ob_start();
get_template_part('best_product');
return ob_get_clean();
}
add_shortcode('best_seller','slider_test');
best_product.php
<link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css">
<?php
$args = array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
);
$loop = new WP_Query( $args );
echo '<div class="owl-carousel">';
while ( $loop->have_posts() ) : $loop->the_post();
global $product; ?>
<div>
<a href="<?php the_permalink(); ?>" id="id-<?php the_id(); ?>" title="<?php the_title(); ?>">
<?php if (has_post_thumbnail( $loop->post->ID ))
echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="product placeholder Image" width="65px" height="115px" />'; ?>
<h3 class="woocommerce-loop-product__title"><?php the_title();?></h3>
</a>
<h4><?php echo woocommerce_price($product->get_price());?></h4>
<div class="best_seller_button">
Add to Basket
</div>
<div class="stock_status">
<div class="status_in_stock"> <?php echo $product->get_stock_status();?></div>
<div class="star-rating"> <?php echo $product->get_average_rating();?></div>'
</div>
</div>
<?php endwhile;
echo '</div>';
?>
<?php wp_reset_query(); ?>
<style>
.owl-item > div:after {
font-family: sans-serif;
font-size: 24px;
font-weight: bold;
}
.owl-theme .owl-controls .owl-nav [class*='owl-']{
background: #8bc34a;
padding: 0px 8px;
}
</style>
<script>
jQuery(document).ready(function () {
var owl = jQuery('.owl-carousel');
owl.owlCarousel({
loop:true,
nav:true,
arrows: true,
margin:10,
navText : ["<i class='fa fa-chevron-left'></i>","<i class='fa fa-chevron-right'></i>"],
responsive:{
0:{
items:1
},
600:{
items:3
},
960:{
items:4
},
1200:{
items:4
}
}
});
owl.on('mousewheel', '.owl-stage', function (e) {
if (e.deltaY>0) {
owl.trigger('next.owl');
} else {
owl.trigger('prev.owl');
}
e.preventDefault();
});
});
</script>

Create a shortcode in a plugin showing the content

I want to create a shortcode in an existing Wordpress/Woocommerce plugin. Where do I put the code to create the shortcode?
The plugin displays a table with all the available variations on any given variable product on the product page.
The table reacts to what the customer selects on the attributes form.
I want to make a shortcode so that I can show this in a tab on the product page instead of before the add to cart button as is now. Where should I put the code for the shortcode?
<?php
/*
Plugin Name: Variations On Product Page
Description: Display all the available variations on the product page
Version: 1.2
*/
define( 'VARIATIONS_ON_PRODUCT_VER', '1.2' );
define( 'VARIATIONS_ON_PRODUCT_PATH', plugin_dir_path( __FILE__ ) );
define( 'VARIATIONS_ON_PRODUCT_URL', plugin_dir_url( __FILE__ ) );
require_once 'settings.php';
if ( ! isset( Variations_Product_Settings::getSettings()['enabled'] ) ) {
return;
}
add_action( 'wp_enqueue_scripts', function () {
wp_enqueue_script( 'variations-on-product', VARIATIONS_ON_PRODUCT_URL . 'assets/variations-on-product.js', [ 'jquery' ], VARIATIONS_ON_PRODUCT_VER );
wp_localize_script( 'variations-on-product', 'variations_on_product_settings', apply_filters( 'variations_on_product_js_settings', [
'options' => Variations_Product_Settings::getSettings(),
] ) );
wp_enqueue_style( 'variations-on-product', VARIATIONS_ON_PRODUCT_URL . 'assets/variations-on-product.css', [], VARIATIONS_ON_PRODUCT_VER );
} );
add_action( 'woocommerce_before_add_to_cart_button', function () {
global $product;
/* #var WC_Product $product */
if( !$product->is_type('variable') ) return;
?>
<style>
#product-variations .attachment-woocommerce_thumbnail {
max-width: 100px;
}
table#product-variations .custom_form .quantity {
float: none !important;
width: 100% !important;
margin-right: 0 !important;
}
table#product-variations .custom_form .input-text.qty {
width: 100% !important;
box-sizing: border-box;
}
table#product-variations .custom_form button.button.alt {
width: 100%;
}
table#product-variations .custom_form p.stock {
margin-bottom: 3px !important;
text-align: center;
font-size: 14px
}
</style>
<table id="product-variations">
<thead>
<tr>
<?php if( !Variations_Product_Settings::getSettings()['hide_thumbnail'] ): ?>
<td>Image</td>
<?php endif; ?>
<td>Title</td>
<td>Price</td>
<td>*</td>
</tr>
</thead>
<tbody></tbody>
</table>
<?=Variations_Product_Settings::getSettings()['load_more_text'] ?: 'Load more'?>
<?php
} );
add_filter( 'woocommerce_available_variation', function ( $default, $product, $variation ) {
/* #var WC_Product_Variation $variation */
ob_start();
?>
<tr>
<?php if( !Variations_Product_Settings::getSettings()['hide_thumbnail'] ): ?>
<td>
<?= $variation->get_image() ?>
</td>
<?php endif; ?>
<td>
<?= $variation->get_name() ?>
<p>
<small><?= $variation->get_sku() ?></small>
</p>
</td>
<td>
<?= $variation->get_price_html() ?>
</td>
<td>
<?php if ( $variation->is_in_stock() ) : ?>
<form class="custom_form" action="<?php echo esc_url( get_permalink() ); ?>" method="post" enctype='multipart/form-data'>
<?php
if( !Variations_Product_Settings::getSettings()['hide_stock_status'] ) {
if ( $variation->managing_stock() ) {
echo wc_get_stock_html( $variation );
}
}
?>
<?php
if( !Variations_Product_Settings::getSettings()['hide_qty'] ) {
woocommerce_quantity_input( [
'min_value' => apply_filters( 'woocommerce_quantity_input_min', $variation->get_min_purchase_quantity(), $variation ),
'max_value' => apply_filters( 'woocommerce_quantity_input_max', $variation->get_max_purchase_quantity(), $variation ),
'input_value' => isset( $_POST['quantity'] ) ? wc_stock_amount( $_POST['quantity'] ) : $variation->get_min_purchase_quantity(),
] );
}
?>
<button type="submit" name="add-to-cart" value="<?php echo esc_attr( $variation->get_id() ); ?>" class=" button alt"><?php echo esc_html( $variation->single_add_to_cart_text() ); ?></button>
</form>
<?php endif; ?>
</td>
</tr>
<?php
$default['c_template'] = apply_filters('vop_variation_template', ob_get_clean(), $variation);
return $default;
}, 10, 3 );
// Hide add to cart on product
if ( isset( Variations_Product_Settings::getSettings()['hide_product_addtocart'] ) ) {
add_action( 'woocommerce_before_add_to_cart_button', function () {
?>
<style>
.single_variation_wrap {
display: none !important;
}
</style>
<?php
} );
}
I tried to add the following code but did not work.
<?php
/*
Plugin Name: Variations On Product Page
Description: Display all the available variations on the product page
Version: 1.2
*/
add_shortcode( 'minkod', 'mitt_lilla_shortcode_handler' );
define( 'VARIATIONS_ON_PRODUCT_VER', '1.2' );
define( 'VARIATIONS_ON_PRODUCT_PATH', plugin_dir_path( __FILE__ ) );
define( 'VARIATIONS_ON_PRODUCT_URL', plugin_dir_url( __FILE__ ) );
I added the function:
function mitt_lilla_shortcode_handler () {
$myshortcode ='
add_action( 'woocommerce_before_add_to_cart_button', function () {
global $product;
/* #var WC_Product $product */
if( !$product->is_type('variable') ) return;
?>
And in the end:
> <style>
> .single_variation_wrap {
> display: none !important;
> } </style> <?php } ); ';} return $myshortcode; }

How to add multiple image upload metabox for a particular post type?

I am creating a theme in which I have created some custom post type products and some custom meta boxes to submit their respective custom information like its cost and expiry date.
By doing some search add_theme_support( 'post-thumbnails' );, I was able to add a featured image column to upload featured image related to a particular post. But it only allows me to add single image and I want to upload multiple images related to one particular custom post .For example different images of single product.
For this I found different solutions on internet and one of them which I found the most suitable to my problem is this, but again it only allows me to upload only one image but different than featured image.
Does WordPress has any functionality to upload multiple images like featured image upload particularly for single post or is there any plugin for this task?
<?php
add_action( 'admin_init', 'add_post_gallery_so_14445904' );
add_action( 'admin_head-post.php', 'print_scripts_so_14445904' );
add_action( 'admin_head-post-new.php', 'print_scripts_so_14445904' );
add_action( 'save_post', 'update_post_gallery_so_14445904', 10, 2 );
/**
* Add custom Meta Box to Posts post type
*/
function add_post_gallery_so_14445904()
{
add_meta_box(
'post_gallery',
'Studio Image Uploader',
'post_gallery_options_so_14445904',
'post',// here you can set post type name
'normal',
'core'
);
}
/**
* Print the Meta Box content
*/
function post_gallery_options_so_14445904()
{
global $post;
$gallery_data = get_post_meta( $post->ID, 'gallery_data', true );
// Use nonce for verification
wp_nonce_field( plugin_basename( __FILE__ ), 'noncename_so_14445904' );
?>
<div id="dynamic_form">
<div id="field_wrap">
<?php
if ( isset( $gallery_data['image_url'] ) )
{
for( $i = 0; $i < count( $gallery_data['image_url'] ); $i++ )
{
?>
<div class="field_row">
<div class="field_left">
<div class="form_field">
<label>Image URL</label>
<input type="text"
class="meta_image_url"
name="gallery[image_url][]"
value="<?php esc_html_e( $gallery_data['image_url'][$i] ); ?>"
/>
</div>
</div>
<div class="field_right image_wrap">
<img src="<?php esc_html_e( $gallery_data['image_url'][$i] ); ?>" height="48" width="48" />
</div>
<div class="field_right">
<input class="button" type="button" value="Choose File" onclick="add_image(this)" /><br />
<input class="button" type="button" value="Remove" onclick="remove_field(this)" />
</div>
<div class="clear" /></div>
</div>
<?php
} // endif
} // endforeach
?>
</div>
<div style="display:none" id="master-row">
<div class="field_row">
<div class="field_left">
<div class="form_field">
<label>Image URL</label>
<input class="meta_image_url" value="" type="text" name="gallery[image_url][]" />
</div>
</div>
<div class="field_right image_wrap">
</div>
<div class="field_right">
<input type="button" class="button" value="Choose File" onclick="add_image(this)" />
<br />
<input class="button" type="button" value="Remove" onclick="remove_field(this)" />
</div>
<div class="clear"></div>
</div>
</div>
<div id="add_field_row">
<input class="button" type="button" value="Add Field" onclick="add_field_row();" />
</div>
</div>
<?php
}
/**
* Print styles and scripts
*/
function print_scripts_so_14445904()
{
// Check for correct post_type
global $post;
if( 'post' != $post->post_type )// here you can set post type name
return;
?>
<style type="text/css">
.field_left {
float:left;
}
.field_right {
float:left;
margin-left:10px;
}
.clear {
clear:both;
}
#dynamic_form {
width:580px;
}
#dynamic_form input[type=text] {
width:300px;
}
#dynamic_form .field_row {
border:1px solid #999;
margin-bottom:10px;
padding:10px;
}
#dynamic_form label {
padding:0 6px;
}
</style>
<script type="text/javascript">
function add_image(obj) {
var parent=jQuery(obj).parent().parent('div.field_row');
var inputField = jQuery(parent).find("input.meta_image_url");
tb_show('', 'media-upload.php?TB_iframe=true');
window.send_to_editor = function(html) {
var url = jQuery(html).find('img').attr('src');
inputField.val(url);
jQuery(parent)
.find("div.image_wrap")
.html('<img src="'+url+'" height="48" width="48" />');
// inputField.closest('p').prev('.awdMetaImage').html('<img height=120 width=120 src="'+url+'"/><p>URL: '+ url + '</p>');
tb_remove();
};
return false;
}
function remove_field(obj) {
var parent=jQuery(obj).parent().parent();
//console.log(parent)
parent.remove();
}
function add_field_row() {
var row = jQuery('#master-row').html();
jQuery(row).appendTo('#field_wrap');
}
</script>
<?php
}
/**
* Save post action, process fields
*/
function update_post_gallery_so_14445904( $post_id, $post_object )
{
// Doing revision, exit earlier **can be removed**
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE )
return;
// Doing revision, exit earlier
if ( 'revision' == $post_object->post_type )
return;
// Verify authenticity
if ( !wp_verify_nonce( $_POST['noncename_so_14445904'], plugin_basename( __FILE__ ) ) )
return;
// Correct post type
if ( 'post' != $_POST['post_type'] ) // here you can set post type name
return;
if ( $_POST['gallery'] )
{
// Build array for saving post meta
$gallery_data = array();
for ($i = 0; $i < count( $_POST['gallery']['image_url'] ); $i++ )
{
if ( '' != $_POST['gallery']['image_url'][ $i ] )
{
$gallery_data['image_url'][] = $_POST['gallery']['image_url'][ $i ];
}
}
if ( $gallery_data )
update_post_meta( $post_id, 'gallery_data', $gallery_data );
else
delete_post_meta( $post_id, 'gallery_data' );
}
// Nothing received, all fields are empty, delete option
else
{
delete_post_meta( $post_id, 'gallery_data' );
}
}
?>
ooh thanks for telling me that Brasofilo.
And the right answer is one can upload multiple images through the media upload button bellow the title of the post and can display those images in a gallery on front end by using gallery shortcode for-eg:-
<?php echo do_shortcode('[gallery size="thumbnail"]'); ?>

Alphabetical list/index for custom tags -> even if empty - Wordpress

In wordpress (3.4) I created some code which returns an alphabetical list/index of custom tags and filters the custom posts in a grid layout. The tags are named as "tagdirectory". The custom post is named "directory".
This is the code:
<?php $list = '';
$tags = get_terms( 'tagdirectory' );
echo '<ul id="portfolio-filter">';
echo'<li>All</li>';
$groups = array();
if( $tags && is_array( $tags ) ) {
foreach( $tags as $tag ) {
$first_letter = strtoupper( $tag->name[0] );
$groups[ $first_letter ][] = $tag;}
if( !empty( $groups ) ) {
foreach( $groups as $letter => $tags ) {
$list .= "\n\t" . '<h2>' . apply_filters( 'the_title', $letter ) .'</h2>';
$list .= "\n\t" . '<ul>';
foreach( $tags as $tag ) {
$lower = strtolower($tag->name);
$name = str_replace(' ', ' ', $tag->name);
$naam = str_replace(' ', '-', $lower);
$list .= "\n\t\t" . '<li>'.$name.'</li>';
}}}}else $list .= "\n\t" . '<p>Sorry, but no tags were found</p>';print $list;
echo "</ul>";
?>
This works perfectly but I would also like for empty letters from the alphabet to be shown.
For example, now it returns:
A
Aicher Otl
Apeloig Philippe
B
Bass Saul
F
Fitszimmons Maureen
... and so on
But it doesn't show the empty letter groups because there are no tags starting with this letter. I do need it to show the capital letter for empty groups though, like so:
A
Aicher Otl
Apeloig Philippe
B
Bass Saul
C
D
E
F
Fitszimmons Maureen
G
... and so on
Can anybody help me and tell me what code I should add for this to work?
Thanks!
I have just edited this solution> http://wordpress.mcdspot.com/2010/12/03/template-to-list-posts-by-first-letter-of-title/
I got it to display a custom post type by changing the 'post-type' on line #37 to the correct custom taxonomy ('distributors' as I am using to list companies by name) and it's done the trick.
Here is the code:
<?php
/*
Template Name: A-Z Pages
A WordPress template to list page titles by first letter.
You should modify the CSS to suit your theme and place it in its proper file.
Be sure to set the $posts_per_row and $posts_per_page variables.
*/
$posts_per_row = 3;
$posts_per_page = 15;
?>
<?php get_header(); ?>
<style type="text/css">
.letter-group { width: 100%; }
.letter-cell { width: 5%; height: 2em; text-align: center; padding-top: 8px; margin-bottom: 8px; background: #e0e0e0; float: left; }
.row-cells { width: 70%; float: right; margin-right: 180px; }
.title-cell { width: 30%; float: left; overflow: hidden; margin-bottom: 8px; }
.clear { clear: both; }
</style>
<div id="main-background">
<div id="main-column">
<h1><?php the_title(); ?></h1>
<div class="margin-top"></div>
<div id="a-z">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array (
'posts_per_page' => $posts_per_page,
'post_type' => 'page',
'orderby' => 'title',
'order' => 'ASC',
'paged' => $paged
);
query_posts($args);
if ( have_posts() ) {
$in_this_row = 0;
while ( have_posts() ) {
the_post();
$first_letter = strtoupper(substr(apply_filters('the_title',$post->post_title),0,1));
if ($first_letter != $curr_letter) {
if (++$post_count > 1) {
end_prev_letter();
}
start_new_letter($first_letter);
$curr_letter = $first_letter;
}
if (++$in_this_row > $posts_per_row) {
end_prev_row();
start_new_row();
++$in_this_row; // Account for this first post
} ?>
<div class="title-cell"><?php the_title(); ?></div>
<?php }
end_prev_letter();
?>
<div class="navigation">
<div class="alignleft"><?php next_posts_link('« Higher Letters') ?></div>
<div class="alignright"><?php previous_posts_link('Lower Letters »') ?></div>
</div>
<?php } else {
echo "<h2>Sorry, no posts were found!</h2>";
}
?>
</div><!-- End id='a-z' -->
</div><!-- End class='margin-top -->
</div><!-- End id='rightcolumn' -->
<?php get_sidebar(); ?>
<?php get_footer(); ?>
<?php
function end_prev_letter() {
end_prev_row();
echo "</div><!-- End of letter-group -->\n";
echo "<div class='clear'></div>\n";
}
function start_new_letter($letter) {
echo "<div class='letter-group'>\n";
echo "\t<div class='letter-cell'>$letter</div>\n";
start_new_row($letter);
}
function end_prev_row() {
echo "\t</div><!-- End row-cells -->\n";
}
function start_new_row() {
global $in_this_row;
$in_this_row = 0;
echo "\t<div class='row-cells'>\n";
}
?>

Resources