I add the following code in in functions.php (wordpress), but it doesn't work correctly:
function change_city_to_dropdown( $fields ) {
$city_args = wp_parse_args( array(
'type' => 'select',
'options' => array(
'city1' => 'City1',
'city2' => 'City2',
'city3' => 'City3',
'city4' => 'City4',
'city5' => 'City5',
'city6' => 'City6',
),
'input_class' => array(
'wc-enhanced-select',
)
), $fields['shipping']['shipping_city'] );
$fields['shipping']['shipping_city'] = $city_args;
$fields['billing']['billing_city'] = $city_args; // Also change for billing field
wc_enqueue_js( "
jQuery( ':input.wc-enhanced-select' ).filter( ':not(.enhanced)' ).each( function() {
var select2_args = { minimumResultsForSearch: 5 };
jQuery( this ).select2( select2_args ).addClass( 'enhanced' );
});" );
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'change_city_to_dropdown' );
function change_address_1_to_dropdown( $fields ) {
$address_1_args = wp_parse_args( array(
'type' => 'select',
'options' => array(
'street1' => 'Street1',
'street2' => 'Street2',
'street3' => 'Street3',
'street4' => 'Street4',
'street5' => 'Street5',
'street6' => 'Street6',
),
'input_class' => array(
'wc-enhanced-select',
)
), $fields['shipping']['shipping_address_1'] );
$fields['shipping']['shipping_address_1'] = $address_1_args;
$fields['billing']['billing_address_1'] = $address_1_args; // Also change for billing field
wc_enqueue_js( "
jQuery( ':input.wc-enhanced-select' ).filter( ':not(.enhanced)' ).each( function() {
var select2_args = { minimumResultsForSearch: 5 };
jQuery( this ).select2( select2_args ).addClass( 'enhanced' );
});" );
return $fields;
}
add_filter( 'woocommerce_checkout_fields', 'change_address_1_to_dropdown' );
I would like "street1" and "street2" to be assigned only to "city1", not to all cities. Likewise, "street3" and "street4" should be assigned to the city "city2". And, "street5" and "street6" should be assigned to the citys "city3", "city4", "city5" and "city6". Can you help me with these changes?
Thank you very much!
Related
My theme was tailor-made by a developper.
All my (single-post) articles are custom posts.
By default the url of a custom post is : https://www.example.com/custom-post-name/slug
For very good reasons this prefix /custom-post-name/ had to be removed.
The developper of my theme achieved that by editing some code in the file declaring the custom post which is this one : wp-content/themes/mytheme/inc/custom-post-types.php
The custom-post-name as registered in this file is "article"
register_post_type( "article", $args );
I believe the part of code removing the prefix /custom-post-name/ from the urls is :
function gp_remove_cpt_slug( $post_link, $post, $leavename ) {
if ( 'article' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 3 );
It has worked fine until now that I want to translate my website.
The translated articles get this url :
https://www.example.com/en/slug
The result is a 404 error
If I add (by hand) in the url the prefix custom-post-name that way :
https://www.example.com/en/custom-post-name/slug
Then it works.
What I need is to get completely rid of this prefix for my custom-post-type registered with the name "article". In the default language AND IN THE TRANSLATED versions made with the plugin Polylang
The complete code of my file wp-content/themes/mytheme/inc/custom-post-types.php is this :
<?php
function cptui_register_my_cpts_module() {
$labels = array(
"name" => __( "Modules", "" ),
"singular_name" => __( "Module", "" ),
);
$args = array(
"label" => __( "Modules", "" ),
"labels" => $labels,
"description" => "",
"public" => false,
"publicly_queryable" => false,
"show_ui" => true,
"show_in_rest" => false,
"rest_base" => "",
"has_archive" => false,
"show_in_menu" => true,
"exclude_from_search" => true,
"capability_type" => "page",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "module", "with_front" => true ),
"query_var" => true,
"menu_icon" => "dashicons-schedule",
"supports" => array( "title", "page-attributes" ),
);
register_post_type( "module", $args );
$labels = array(
"name" => __( "Articles", "" ),
"singular_name" => __( "Article", "" ),
);
$args = array(
"label" => __( "Articles", "" ),
"labels" => $labels,
"description" => "",
"public" => true,
"publicly_queryable" => true,
"show_ui" => true,
"show_in_rest" => false,
"rest_base" => "",
"has_archive" => false,
"show_in_menu" => true,
"exclude_from_search" => false,
"capability_type" => "page",
"map_meta_cap" => true,
"hierarchical" => false,
"rewrite" => array( "slug" => "article", "with_front" => true ),
"query_var" => true,
//"menu_icon" => "dashicons-schedule",
"menu_position" => 5,
"supports" => array( "title", "page-attributes", "comments", "editor" ),
"taxonomies" => array( "categorie" ),
);
register_post_type( "article", $args );
}
add_action( 'init', 'cptui_register_my_cpts_module' );
function cptui_register_my_taxes_categorie() {
/**
* Taxonomy: Catégories.
*/
$labels = array(
"name" => __( "Catégories", "" ),
"singular_name" => __( "Catégorie", "" ),
);
$args = array(
"label" => __( "Catégories", "" ),
"labels" => $labels,
"public" => true,
"hierarchical" => true,
"label" => "Catégories",
"show_ui" => true,
"show_in_menu" => true,
"show_in_nav_menus" => true,
"query_var" => true,
"rewrite" => array( 'slug' => 'categorie', 'with_front' => true, ),
"show_admin_column" => true,
"show_in_rest" => false,
"rest_base" => "",
"show_in_quick_edit" => true,
);
register_taxonomy( "categorie", array( "article" ), $args );
}
add_action( 'init', 'cptui_register_my_taxes_categorie' );
function gp_remove_cpt_slug( $post_link, $post, $leavename ) {
if ( 'article' != $post->post_type || 'publish' != $post->post_status ) {
return $post_link;
}
$post_link = str_replace( '/' . $post->post_type . '/', '/', $post_link );
return $post_link;
}
add_filter( 'post_type_link', 'gp_remove_cpt_slug', 10, 3 );
function gp_add_cpt_post_names_to_main_query( $query ) {
// Bail if this is not the main query.
if ( ! $query->is_main_query() ) {
return;
}
// Bail if this query doesn't match our very specific rewrite rule.
if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
return;
}
// Bail if we're not querying based on the post name.
if ( empty( $query->query['name'] ) ) {
return;
}
// Add CPT to the list of post types WP will include when it queries based on the post name.
$query->set( 'post_type', array( 'post', 'page', 'article' ) );
}
add_action( 'pre_get_posts', 'gp_add_cpt_post_names_to_main_query' );
add_filter('request', 'rudr_change_term_request', 1, 1 );
function rudr_change_term_request($query){
$tax_name = 'categorie'; // specify you taxonomy name here, it can be also 'category' or 'post_tag'
// Request for child terms differs, we should make an additional check
if( $query['attachment'] ) :
$include_children = true;
$name = $query['attachment'];
else:
$include_children = false;
$name = $query['name'];
endif;
$term = get_term_by('slug', $name, $tax_name); // get the current term to make sure it exists
if (isset($name) && $term && !is_wp_error($term)): // check it here
if( $include_children ) {
unset($query['attachment']);
$parent = $term->parent;
while( $parent ) {
$parent_term = get_term( $parent, $tax_name);
$name = $parent_term->slug . '/' . $name;
$parent = $parent_term->parent;
}
} else {
unset($query['name']);
}
switch( $tax_name ):
/* case 'category':{
$query['category_name'] = $name; // for categories
break;
}
case 'post_tag':{
$query['tag'] = $name; // for post tags
break;
}*/
default:{
$query[$tax_name] = $name; // for another taxonomies
break;
}
endswitch;
endif;
return $query;
}
add_filter( 'term_link', 'rudr_term_permalink', 10, 3 );
function rudr_term_permalink( $url, $term, $taxonomy ){
$taxonomy_name = 'categorie'; // your taxonomy name here
$taxonomy_slug = 'categorie'; // the taxonomy slug can be different with the taxonomy name (like 'post_tag' and 'tag' )
// exit the function if taxonomy slug is not in URL
if ( strpos($url, $taxonomy_slug) === FALSE || $taxonomy != $taxonomy_name ) return $url;
$url = str_replace('/' . $taxonomy_slug, '', $url);
return $url;
}
?>
Thanks a lot for reading and any help,
François
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
I am challenging Ajax implementation when replying by "BBPress".
However, reply posted in this manner is displayed in the dashboard, but it is not displayed in the loop of the replies list.
This will be displayed in the loop of the replies list only when you click "Update" on the dashboard.
I want to know the code that displays correctly in the loop.
This is my code:
my-reply.js
"use strict";
(function($){
$(document).on("click","#bbp_reply_submit", function() {
// get
var inputForum = '56'; //There is only one forum.
var inputTopic = $('#bbp_topic_id').val();
var inputTitle = $('#bbp-reply-form-info').text();
var inputContent = $('#bbp_reply_input').val();
var inputParent = $('#bbp_reply_to').val();
var inputIp = $('#my_reply_ip').val();
// Ajax
$.ajax({
url: MY_AJAX.api,
type: 'POST',
data: {
// action name
action: MY_AJAX.action,
// nonce
nonce: MY_AJAX.nonce,
// submit data
submitForum: inputForum,
submitTopic: inputTopic,
submitTitle: inputTitle,
submitContent: inputContent,
submitParent: inputParent,
submitIp: inputIp,
}
})
// scusess
.done(function( response ) {
alert('success'); // Actually, I will display reply instead of alerts.
})
// error
.fail(function( jqXHR, textStatus, errorThrown ) {
alert('error');
});
});
})(jQuery);
functions.php
// nonce
function my_enqueue_scripts() {
$handle = 'my-script';
$jsFile = 'path/to/myscript.js';
wp_register_script($handle, $jsFile, ['jquery']);
$acrion = 'my-ajax-action';
wp_localize_script($handle, 'MY_AJAX', [
'api' => admin_url( 'admin-ajax.php' ),
'action' => $acrion,
'nonce' => wp_create_nonce( $acrion ),
]);
wp_enqueue_script($handle);
}
add_action( 'wp_enqueue_scripts', 'my_enqueue_scripts' );
// post reply
function my_ajax_event() {
$action = 'my-ajax-action';
if( check_ajax_referer($action, 'nonce', false) ) {
$submitForum = esc_html( $_POST['submitForum'] );
$submitTopic = esc_html( $_POST['submitTopic'] );
$submitTitle = esc_html( $_POST['submitTitle'] );
$submitContent = esc_html( $_POST['submitContent'] );
$submitParent = esc_html( $_POST['submitParent'] );
$submitIp = esc_html( $_POST['submitIp'] );
$submitTopicslug = esc_html( $_POST['submitTopicslug'] );
$page = get_post( $submitTopic );
$slug = $page->post_name;
$date = 'Y-m-d H:i:s';
$my_post = array(
'post_title' => (string)wp_strip_all_tags($_POST['submitTitle']),
'post_content' => (string)$_POST['submitContent'],
'post_status' => 'publish',
'post_author' => get_current_user_id(),
'post_type' => 'reply',
'meta_input' => array(
'_bbp_forum_id' => $_POST['submitForum'],
'_bbp_topic_id' => $_POST['submitTopic'],
'_bbp_reply_to' => $_POST['submitParent'],
'_bbp_author_ip' => $_POST['submitIp'],
'_edit_last' => get_current_user_id(),
'_bbp_topic_sort_desc' => 'dft',
'_bbp_sort_desc' => 'dft',
'_bbp_topic_sort_show_lead_topic' => 'dft',
'_bbp_topic_sort_show_lead_topic_forum' => 'dft',
'_wp_old_slug' => $slug,
'_bbp_last_active_time' => $date,
'_bbp_reply_count' => '',
'_bbp_engagement' => '',
'_bbp_reply_count_hidden' => '',
'_bbp_voice_count' => '',
)
);
wp_insert_post( $my_post );
} else {
status_header( '403' );
}
die();
}
add_action( 'wp_ajax_my-ajax-action', 'my_ajax_event' );
add_action( 'wp_ajax_nopriv_my-ajax-action', 'my_ajax_event' );
My woocommerce are not listing orders with status 'wc-expired'. I tried adding the 'wc_order_statuses' filter, but it still does not work.
add_filter('wc_order_statuses', function ($order_statuses){
$order_statuses['wc-expired'] = _x( 'Expired', 'Order status', 'woocommerce' );
return $order_statuses;
});
Row in Table wp_posts
The Orders List in Admin
Create new order status. Sample:
function register_expired_order_status() {
register_post_status( 'wc-expired', array(
'label' => 'Expired',
'public' => true,
'exclude_from_search' => false,
'show_in_admin_all_list' => true,
'show_in_admin_status_list' => true,
'label_count' => _n_noop( 'Expired <span class="count">(%s)</span>', 'Expired <span class="count">(%s)</span>' )
) );
}
add_action( 'init', 'register_expired_order_status' );
function add_expired_to_order_statuses( $order_statuses ) {
$new_order_statuses = array();
// add new order status after processing
foreach ( $order_statuses as $key => $status ) {
$new_order_statuses[ $key ] = $status;
if ( 'wc-processing' === $key ) {
$new_order_statuses['wc-expired'] = 'Expired';
}
}
return $new_order_statuses;
}
add_filter( 'wc_order_statuses', 'add_expired_to_order_statuses' );
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