I'm working with elementor plugin, and I code plugin(shortcode) to get posts data. I want to create plugin with swiper slider but it is not working correctly. It show's data but the_title() and other functions not loading data into slider only before slider. Inside console everything is ok.
This is class
class MRSolutionsSliderWoocommerce
{
function register()
{
add_action('wp_enqueue_scripts', array($this, 'enqueue'));
add_action( 'init', array($this, 'create_slider_posttype'));
add_action( 'init', array($this, 'create_slider_location_tax'));
add_action('wp_insert_post', array($this, 'set_default_slidermeta'));
add_shortcode('simpleslider', array($this, 'simple_slider_shortcode'));
}
function activate()
{
flush_rewrite_rules();
}
function deactivate()
{
flush_rewrite_rules();
}
function enqueue()
{
wp_enqueue_style('swiperCss', plugins_url('/assets/css/swiper.min.css', __FILE__));
wp_enqueue_script('swiperJs', plugins_url('/assets/js/swiper.min.js', __FILE__));
wp_enqueue_style('mrSliderWoocommerceStyle', plugins_url('/assets/css/style.css', __FILE__));
wp_enqueue_script('mrSliderWoocommerceScript', plugins_url('/assets/js/scripts.js', __FILE__));
}
function simple_slider_shortcode($atts = null) {
$no_products = 'Brak produktów na wyprzedaży';
$ss_atts = shortcode_atts(
array(
'slider_type' => '',
'limit' => 6,
'on_sale' => 0
), $atts, 'simpleslider'
);
$slides = array();
$args = array(
'post_type' => 'product',
'meta_key' => $ss_atts['total_sales'],
'orderby' => 'meta_value_num',
'posts_per_page' => $ss_atts['limit'],
'meta_query' => array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
if($ss_atts['on_sale'] == 1) {
$args = array(
'post_type' => 'product',
'posts_per_page' => $ss_atts['limit'],
'meta_query' => array(
'relation' => 'OR',
array( // Simple products type
'key' => '_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
),
array( // Variable products type
'key' => '_min_variation_sale_price',
'value' => 0,
'compare' => '>',
'type' => 'numeric'
)
)
);
} else {
$args = array(
'post_type' => 'product',
'meta_key' => $ss_atts['total_sales'],
'orderby' => 'meta_value_num',
'posts_per_page' => $ss_atts['limit']
);
}
$loop = new WP_Query( $args );
$return_prod = '';
if($loop->have_posts()) {
while ( $loop->have_posts() ) : $loop->the_post();
global $product;
$slides[] = '
<article class="swiper-slide">
<div class="slide-content">
<div class="slide-img">' . woocommerce_get_product_thumbnail() . '</div>
<h3 class="slide-title">' . get_the_title() . '</h3>
<p class="slide-text">' . wp_trim_words(get_the_content(), 15, '...' ) . '</p>
<a class="slide-btn" href="' . get_permalink() . '">Wybierz produkt</a>
</div>
</article>';
endwhile;
wp_reset_query();
return '
<div class="swiper-container">
<section class="swiper-wrapper">
'.implode('', $slides).'
</section>
</div>';
} else {
return '<h3 class="no-products-slider">' . $no_products . '</h3>';
}
}
function maxContentLength($content)
{
// Take the existing content and return a subset of it
return substr($content, 0, 100);
}
}
if(class_exists('MRSolutionsSliderWoocommerce'))
{
$mrSliderWoocommerce = new MRSolutionsSliderWoocommerce();
// Call register method
$mrSliderWoocommerce->register();
}
// activation
register_activation_hook(__FILE__, array($mrSliderWoocommerce, 'activate'));
// deactivation
register_deactivation_hook(__FILE__, array($mrSliderWoocommerce, 'deactivate'));
and this is js
jQuery(document).ready(function () {
var swiper = new Swiper('.swiper-container', {
autoplay: {
delay: 2500,
disableOnInteraction: false,
},
slidesPerView: 3,
spaceBetween: 20,
// init: false,
pagination: {
el: '.swiper-pagination',
clickable: true,
},
breakpoints: {
1024: {
slidesPerView: 2,
spaceBetween: 10,
},
560: {
slidesPerView: 1,
spaceBetween: 10,
}
}
});
});
This is result
enter image description here
Related
I have searched a lot online with no luck on searching nested values
lets say i have a users array
[
{
id: 0,
billing: {
phone: "999999"
}
},
{
id: 1,
billing: {
phone: "777777"
}
}
]
I want to use WP_Query to filter by phone number is it possible to do that ? and how ?
function get_customers(WP_REST_Request $request) {
$param = $request['phone'];
$args = array(
'post_type' => 'customer',
'posts_per_page' => 1,
'meta_query' => array(
'key' => 'billing.phone',
'value' => $param,
'compare' => '='
)
);
$customers = new WP_Query($args);
return $customers->posts;
}
add_action( 'rest_api_init', function() {
register_rest_route('rc/v1', 'customers/(?P<phone>\d+)', [
'methods' => 'GET',
'callback' => 'get_customers'
]);
});
First, your meta_query syntax is wrong it's should be inside one more array check here and If your post meta key is 'billing.phone' then the below code will work fine.
function get_customers( WP_REST_Request $request ) {
$param = $request['phone'];
$args = array(
'post_type' => 'customer',
'posts_per_page' => 1,
'meta_query' => array(
array(
'key' => 'billing.phone',
'value' => $param,
'compare' => '='
)
)
);
$customers = new WP_Query($args);
return $customers->posts;
}
add_action( 'rest_api_init', function() {
register_rest_route('rc/v1', 'customers/(?P<phone>\d+)', [
'methods' => 'GET',
'callback' => 'get_customers'
]);
});
I`ve made this action:
add_action( 'init', 'advanced_search_query' );
function advanced_search_query( $query ) {
if ( isset($_GET['make']) && isset($_GET['model']) && isset($_GET['fuel']) ) {
$all_products = [];
$inner_cats = get_terms(['taxonomy' => 'product_cat','hide_empty' => false, 'parent' => $_GET['model']]);
foreach ($inner_cats as $cat) {
if ($_GET['fuel'] == 0)
$fuel_cat = get_terms(['taxonomy' => 'product_cat','hide_empty' => false, 'parent' => $cat->term_id, 'name' => 'Diesel']);
elseif ($_GET['fuel'] == 1)
$fuel_cat = get_terms(['taxonomy' => 'product_cat','hide_empty' => false, 'parent' => $cat->term_id, 'name' => 'Benzin']);
$args = array(
'post_type' => 'product',
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'terms' => $fuel_cat[0]->term_id,
'operator' => 'IN',
)
)
);
$products = get_posts( $args );
if ($products) {
foreach ($products as $prd) {
$all_products[] = $prd;
}
}
}
}
}
Now what I am strugling with is sending the $all_products to my search page and loop them. Tried to redirect to the search page like wp_safe_recirect('domain.com?s=""') with empty s parameter and use the all_results there. Also tried to set the variable as global but again couldnt reach it.
Did you try the following?
if ($products) {
foreach ($products as $prd) {
$all_products[] = $prd;
?>
<script>window.location.href="domain.com?s=''"</script>
<?php
}
}
I am building a plugin where I have a dropdown category filter on media library.
The filter works fine on list view but on grid view mode when I select a category, rather than media files getting filtered the page refreshes and nothing is filtering.
I am not using any custom taxonomy, so I have register the taxonomy (in my case it is category to attachment post type)
function register_taxonomy_for_post_type() {
//$this->taxonomy = apply_filters( 'cool_media_taxonomy', $this->taxonomy );
if( taxonomy_exists( $this->taxonomy ) ) {
register_taxonomy_for_object_type( $this->taxonomy, $this->post_type );
} else {
$args = array(
'hierarchical' => true,
'show_admin_column' => true,
'update_count_callback' => array( $this, 'update_count' ),
);
register_taxonomy( $this->taxonomy, array( $this->post_type ), $args );
}
}
add_action( 'init', array( $this, 'register_taxonomy_for_post_type' ), 0 );
Localization:
function enqueue_media_action() {
require_once plugin_dir_path( __FILE__ ) . 'classes/class-walker-category-mediagrid-filter.php';
global $pagenow;
if( wp_script_is( 'media-editor' ) && 'upload.php' === $pagenow ) {
if( $this->taxonomy !== 'category' ) {
$options = array(
'taxonomy' => $this->taxonomy,
'hierarchical' => true,
'hide_empty' => false,
'show_count' => true,
'orderby' => 'name',
'order' => 'ASC',
'value' => 'id',
'echo' => false,
'walker' => new WalkerCategoryMediaGridFilter(),
);
} else {
$options = array(
'taxonomy' => $this->taxonomy,
'hierarchical' => true,
'hide_empty' => false,
'show_count' => true,
'orderby' => 'name',
'order' => 'ASC',
'value' => 'id',
'echo' => false,
'walker' => new WalkerCategoryMediaGridFilter(),
);
}
$attachment_terms = wp_dropdown_categories( $options );
$attachment_terms = preg_replace( array( "/<select([^>]*)>/", "/<\/select>/" ), "", $attachment_terms );
echo '<script type="text/javascript">';
echo '/* <![CDATA[ */';
echo 'var coolmediafilter_taxonomies = {"' . $this->taxonomy . '":{"list_title":"' . html_entity_decode( __( 'All categories', $this->text_domain ), ENT_QUOTES, 'UTF-8' ) . '","term_list":[' . substr( $attachment_terms, 2 ) . ']}};';
echo '/* ]]> */';
echo '</script>';
wp_enqueue_script('coolmediafilter-media-views', plugins_url( 'js/cmf-media-views.js', __FILE__ ), array( 'media-views' ), '1.0.0', true );
$cats = $this->get_accessible_categories();
$filtered_cats = array(
'hide_empty' => false,
'include' => $cats,
'orderby' => 'name',
'order' => 'ASC',
);
wp_localize_script( 'coolmediafilter-media-views', 'MediaLibraryCategoryFilterOptions',
array(
'terms' => get_terms(
$this->taxonomy,
$filtered_cats
),
)
);
}
wp_enqueue_style( 'coolmediafilter', plugins_url( 'css/coolmediafilter.css', __FILE__ ), array(), '1.0.0' );
}
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_media_action' ) );
The script:
(function(){
/**
* Create a new MediaLibraryTaxonomyFilter we later will instantiate
*/
var MediaLibraryCategoryFilter = wp.media.view.AttachmentFilters.extend({
id: 'media-view-category-filter',
createFilters: function() {
var filters = {};
// Formats the 'terms' we've included via wp_localize_script()
_.each( MediaLibraryCategoryFilterOptions.terms || {}, function( value, index ) {
filters[ value.term_id ] = {
text: value.name,
props: {
// The WP_Query var for the taxonomy
category: value.term_id,
}
};
});
filters.all = {
// Default label
text: 'All categories',
props: {
// The WP_Query var for the taxonomy
category: ''
},
priority: 10
};
this.filters = filters;
}
});
/**
* Extend and override wp.media.view.AttachmentsBrowser to include our new filter
*/
var AttachmentsBrowser = wp.media.view.AttachmentsBrowser;
wp.media.view.AttachmentsBrowser = wp.media.view.AttachmentsBrowser.extend({
createToolbar: function() {
// Make sure to load the original toolbar
AttachmentsBrowser.prototype.createToolbar.call( this );
this.toolbar.set( 'MediaLibraryCategoryFilter', new MediaLibraryCategoryFilter({
controller: this.controller,
model: this.collection.props,
priority: -75
}).render() );
}
});
})()
what I am doing wrong? Any suggestion will be very helpful.
UPDATE: Screenshot of console from FireFox
Hi I have tried everything to join the results of a common search with a sku search but nothing seems to work , it will either show the common search or the sku, not both, any help woulf be appreciated
if (!function_exists('yolo_result_search_callback')) {
function yolo_result_search_callback() {
ob_start();
$yolo_options = yolo_get_options();
$posts_per_page = 8;
if (isset($yolo_options['search_box_result_amount']) && !empty($yolo_options['search_box_result_amount'])) {
$posts_per_page = $yolo_options['search_box_result_amount'];
}
$post_type = array();
if (isset($yolo_options['search_box_post_type']) && is_array($yolo_options['search_box_post_type'])) {
foreach($yolo_options['search_box_post_type'] as $key => $value) {
if ($value == 1) {
$post_type[] = $key;
}
}
}
$keyword = $_REQUEST['keyword'];
if ( $keyword ) {
$search_query = array(
'post_type' => 'product',
'meta_query' => array(
array(
'key' => '_sku',
'value' => $keyword,
'posts_per_page' => $posts_per_page + 1,
'compare' => 'LIKE',
'relation' => 'OR',
)
)
);
$search_query = array(
's' => $keyword,
'order' => 'DESC',
'orderby' => 'date',
'post_status' => 'publish',
'post_type' => $post_type,
'posts_per_page' => $posts_per_page + 1,
);
$search = new WP_Query($search_query);
$newdata = array();
if ($search && count($search->post) > 0) {
$count = 0;
foreach ( $search->posts as $post ) {
if ($count == $posts_per_page) {
$newdata[] = array(
'id' => -2,
'title' => '<i class="wicon icon-outline-vector-icons-pack-94"></i> ' . esc_html__('View More','yolo-motor') . '',
'guid' => '',
'date' => null,
);
break;
}
$newdata[] = array(
'id' => $post->ID,
'title' => $post->post_title,
'guid' => get_permalink( $post->ID ),
'date' => mysql2date( 'M d Y', $post->post_date ),
);
$count++;
}
}
else {
$newdata[] = array(
'id' => -1,
'title' => esc_html__( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'yolo-motor' ),
'guid' => '',
'date' => null,
);
}
ob_end_clean();
echo json_encode( $newdata );
}
die(); // this is required to return a proper result
}
add_action( 'wp_ajax_nopriv_result_search', 'yolo_result_search_callback' );
add_action( 'wp_ajax_result_search', 'yolo_result_search_callback' );
}
After playing around all day, I came up with the solution, maybe it can help someone else
if (!function_exists('yolo_result_search_callback')) {
function yolo_result_search_callback() {
ob_start();
$yolo_options = yolo_get_options();
$posts_per_page = 8;
if (isset($yolo_options['search_box_result_amount']) && !empty($yolo_options['search_box_result_amount'])) {
$posts_per_page = $yolo_options['search_box_result_amount'];
}
$post_type = array();
if (isset($yolo_options['search_box_post_type']) && is_array($yolo_options['search_box_post_type'])) {
foreach($yolo_options['search_box_post_type'] as $key => $value) {
if ($value == 1) {
$post_type[] = $key;
}
}
}
$keyword = $_REQUEST['keyword'];
if ( $keyword ) {
$search_query = array(
'post_type' => $post_type,
'meta_query' => array(
array(
'key' => '_sku',
'value' => $keyword,
'posts_per_page' => $posts_per_page + 1,
'compare' => 'LIKE',
'relation' => 'OR',
)
)
);
$search_query1 = array(
's' => $keyword,
'order' => 'DESC',
'orderby' => 'date',
'post_status' => 'publish',
'post_type' => $post_type,
'posts_per_page' => $posts_per_page + 1,
);
$search = new WP_Query($search_query);
$search1 = new WP_Query($search_query1);
$newdata = array();
if ($search && count($search->post) > 0) {
$count = 0;
foreach ( $search->posts as $post ) {
if ($count == $posts_per_page) {
$newdata[] = array(
'id' => -2,
'title' => '<i class="wicon icon-outline-vector-icons-pack-94"></i> ' . esc_html__('View More','yolo-motor') . '',
'guid' => '',
'date' => null,
);
break;
}
$newdata[] = array(
'id' => $post->ID,
'title' => $post->post_title,
'guid' => get_permalink( $post->ID ),
'date' => mysql2date( 'M d Y', $post->post_date ),
);
$count++;
}
}
else {
if ($search1 && count($search1->post) > 0) {
$count = 0;
foreach ( $search1->posts as $post ) {
if ($count == $posts_per_page) {
$newdata[] = array(
'id' => -2,
'title' => '<i class="wicon icon-outline-vector-icons-pack-94"></i> ' . esc_html__('View More','yolo-motor') . '',
'guid' => '',
'date' => null,
);
break;
}
$newdata[] = array(
'id' => $post->ID,
'title' => $post->post_title,
'guid' => get_permalink( $post->ID ),
'date' => mysql2date( 'M d Y', $post->post_date ),
);
$count++;
}
}
else {
$newdata[] = array(
'id' => -1,
'title' => esc_html__( 'Sorry, but nothing matched your search criteria. Please try again with some different keywords.', 'yolo-motor' ),
'guid' => '',
'date' => null,
);
}
}
ob_end_clean();
echo json_encode( $newdata );
}
die(); // this is required to return a proper result
}
add_action( 'wp_ajax_nopriv_result_search', 'yolo_result_search_callback' );
add_action( 'wp_ajax_result_search', 'yolo_result_search_callback' );
}
I am actually trying to build up a draggable table, but for some reason after implementing the theme_function, my submit handler is no longer getting called.
This is what my Code actually looks like:
function he_ministerienschalter_theme($existing, $type, $theme, $path) {
$themes = array(
'he_ministerienschalter_admin_form' => array(
'render element' => 'element',
),
);
return $themes;
}
/**
* Implements the ministerschalter admin form
*/
function he_ministerienschalter_admin_form($form, &$form_state, $form_id, $item) {
$domainnames = he_ministerienschalter_domainselect();
$count = 0;
$form['ressortswitch'] = array(
'#prefix' => '<div id="curve-attributes">',
'#suffix' => '</div>',
'#tree' => TRUE,
'#theme' => 'he_ministerienschalter_admin_form',
);
foreach ($domainnames as $key => $domainname) {
$form['ressortswitch'][$key]['domainID-'.$key] = array(
'#type' => 'value',
'#value' => $domainname['domain_id'],
);
$form['ressortswitch'][$key]['domainname-'.$key] = array(
'#type' => 'item',
'#title' => $domainname['sitename'],
'#size' => 60,
'#maxlength' => 128,
);
$form['ressortswitch'][$key]['domainshort-'.$key] = array(
'#type' => 'textfield',
'#default_value' => $domainname['sitename'],
);
$form['ressortswitch'][$key]['weight-'.$key] = array(
'#type' => 'textfield',
'#default_value' => !empty($domainname['domain_weight']) ? $domainname['domain_weight'] : '0',
'#size' => 3,
'#attributes' => array('class' => array('item-row-weight')),
);
$count++;
}
$form['ressortswitch']['count'] = array(
'#type' => 'hidden',
'#value' => $count,
);
$form['ressortswitch']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Settings'),
);
return $form;
}
/**
* Theme callback for the he_ministerienschalter_admin_form
*/
function theme_he_ministerienschalter_admin_form($variables) {
$element = $variables['form'];
drupal_add_tabledrag('ressortswitch_table', 'order', 'sibling', 'item-row-weight');
$header = array(
t('Domainname'),
t('Domainshort'),
t('Weight')
);
$element_items = array();
foreach($element['ressortswitch'] as $id => $record) {
if(is_int($id)) {
$element_items[] = $record;
}
}
$rows = array();
foreach ($element_items as $id => $item) {
$domainNAME[$id] = $item['domainname-'.$id];
$domainSHORT[$id] = $item['domainshort-'.$id];
$domainWEIGHT[$id] = $item['weight-'.$id];
$rows[] = array(
'data' => array(
drupal_render($domainNAME[$id]),
drupal_render($domainSHORT[$id]),
drupal_render($domainWEIGHT[$id]),
),
'class' => array('draggable'),
);
}
$output = theme('table', array(
'header' => $header,
'rows' => $rows,
'attributes' => array('id' => 'ressortswitch_table'),
));
$output .= drupal_render($element['ressortswitch']['submit']);
return $output;
}
/**
* Implements hook_form_submit
*/
function he_ministerienschalter_admin_form_submit($form, &$form_state, $item) {
db_delete('he_ministerienschalter')
->execute();
$count_domains = $form_state['values']['ressortswitch']['count'];
$count = 0;
// dpm($form_state);
dpm($form_state);
while($count < $count_domains) {
db_insert('he_ministerienschalter')
->fields(array(
'domain_id' => $form_state['values']['ressortswitch'][$count]['domainID-'.$count],
'domain_short' => $form_state['values']['ressortswitch'][$count]['domainshort-'.$count],
'domain_weight' => $form_state['values']['ressortswitch'][$count]['weight-'.$count]
))
->execute();
$count++;
}
drupal_set_message('Eingabe gespeichert');
}
Any idea how to solve that?
I Updated my theme function like this and now it works like a charm :-)
$output .= drupal_render($element['form_id']);
$output .= drupal_render($element['form_build_id']);
$output .= drupal_render($element['form_token']);