Sub CPT taxonomy 404 page - url-rewriting

I have a problem with my sub taxonomies.
my-cpt/chapitres archive is working. I made a redirection to a page
from the tax-template.
my-cpt/chapitres/sub-term --> 404
my-cpt/chapitres/sub-term/post --> working
This is my code :
cpt
function custom_cpt() {
$labels = array(
'name' => __( 'Mon cpt' ),
'singular_name' => __( 'Mon cpt'),
'menu_name' => __( 'Mon cpt'),
'parent_item_colon' => __( 'Parent Mon cpt'),
// Paramétrages
'all_items' => __( 'Mon cpt'),
'view_item' => __( 'Voir'),
'add_new_item' => __( 'Ajouter du contenu'),
'add_new' => __( 'Ajout contenu'),
'edit_item' => __( 'Modifier'),
'update_item' => __( 'Mettre à jour'),
'search_items' => __( 'Rechercher'),
'not_found' => __( 'Non trouvé'),
'not_found_in_trash' => __( 'Non trouvé dans la corbeille'),
);
$args = array(
'label' => __( 'my-cpt'), // label
'description' => __( 'Mon cpt'),
'labels' => $labels,
'menu_icon' => 'dashicons-book-alt',
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail'),
'public' => true,
'hierarchical' => false,
'rewrite' => array('slug' => 'my-cpt/%chapitres%', 'with_front' => false),
'show_in_rest' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'has_archive' => false,
'can_export' => true,
'query_var' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'menu_position' => 5,
);
register_post_type( 'my-cpt', $args );
}
add_action( 'init', 'custom_cpt', 0 );
Taxonomy:
add_action( 'init', 'custom_cpt_taxonomies', 0 );
function custom_cpt_taxonomies() {
$labels = array(
'name' => _x( 'Chapitres', 'taxonomy general name' ),
'singular_name' => _x( 'Chapitre', 'taxonomy singular name' ),
'search_items' => __( 'Rechercher un chapitre' ),
'all_items' => __( 'Tous les chapitres' ),
'parent_item' => __( 'Chapitres Parents' ),
'parent_item_colon' => __( 'Chapitre Parent:' ),
'edit_item' => __( 'Modifier' ),
'update_item' => __( 'Mettre à jour' ),
'add_new_item' => __( 'Ajouter un chapitre' ),
'new_item_name' => __( 'Nouveau chapitre' ),
'menu_name' => __( 'Chapitres' ),
'has_archive' => false,
);
register_taxonomy('chapitres',array('my-cpt'), array(
"labels" => $labels,
"hierarchical" => true,
"public" => true,
"show_ui" => true,
"show_admin_column" => true,
"show_in_nav_menus" => true,
"show_tagcloud" => true,
"show_in_rest" => true,
'rewrite' => array('hierarchical' => true, 'with_front' => false ),
));
}
I found this for the rewrite rules
function rewrite_permalink_cpt($link, $post){
if ($post->post_type != 'my-cpt')
return $link;
if ($cats = get_the_terms($post->ID, 'chapitres')){
$link = str_replace('%chapitres%', get_taxonomy_parents(array_pop($cats)->term_id, 'chapitres', false, '/', true), $link);
}
return $link;
}
add_filter('post_type_link', 'rewrite_permalink_cpt', 10, 2);
add_filter('rewrite_rules_array', 'rewrite_rules_permalink_cpt');
function rewrite_rules_permalink_cpt($rules){
$cpt = array();
$cpt['my-cpt/(.+)/(.+)/(.+)/(.+)/?$'] = 'index.php?my-cpt=$matches[4]';
$newRules['my-cpt/(.+)/(.+)/?$'] = 'index.php?my-cpt=$matches[1]';
return array_merge($cpt, $rules);
}
function get_taxonomy_parents($id, $taxonomy, $link = false, $separator = '/', $nicename = false, $visited = array()) {
$chain = '';
$parentref = get_term($id, $taxonomy);
$parent = &$parentref;
if (is_wp_error($parent)) {
return $parent;
}
if ($nicename)
$name = $parent->slug;
else
$name = $parent->name;
if($parent!=0){
if ($parent->parent && ($parent->parent != $parent->term_id) && !in_array($parent->parent, $visited)) {
$visited[] = $parent->parent;
$chain .= get_taxonomy_parents($parent -> parent, $taxonomy, $link, $nicename, $visited);
$chain .= "/";
}
if ($link) {
// nothing, can't get this working :(
} else
$chain .= $name;
return $chain;
}
}
I think the problem is come from the rewrite rules, but I don't know what is wrong...
It would be appreciate if someone has an idea

Related

Modify Custom Taxonomy slug with CPT slug

Hi please check my requirement bellow.
First of all, I have registered a CPT "directory" with a slug name "directory". Also, I have registered a custom taxonomy "business_category". I want to below permalink structure.
CPT Archive link: www.domain.com/directory/
Taxonomy link: www.domain.com/directory/category/TAXONOMY_NAME/
CPT Single Page: www.domain.com/directory/POST_NAME
So, I have used the Below code.
public function __construct( $plugin_name, $version ) {
$this->plugin_name = $plugin_name;
$this->version = $version;
$this->PT = 'cc-directory';
$this->name = 'Directory';
$this->singular_name = 'Directory';
$this->slug = 'directory';
}
public function register_post_type() {
// Get supported features for Directory post type
$supports = apply_filters('cc_directory_supports', array('editor', 'title','thumbnail'));
$labels = array(
'name' => $this->name,
'singular_name' => $this->singular_name,
'add_new' => 'Add New',
'add_new_item' => 'Add New ' . $this->singular_name,
'edit_item' => 'Edit ' . $this->singular_name,
'new_item' => 'New ' . $this->singular_name,
'all_items' => 'All ' . $this->name,
'view_item' => 'View ' . $this->name,
'search_items' => 'Search ' . $this->name,
'not_found' => 'No ' . strtolower($this->name) . ' found',
'not_found_in_trash' => 'No ' . strtolower($this->name) . ' found in Trash',
'parent_item_colon' => '',
'menu_name' => $this->name
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'show_in_menu' => true,
'query_var' => true,
'rewrite' => array( 'slug' => $this->slug ),
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => true,
'menu_position' => 11,
'menu_icon' => 'dashicons-book',
'supports' => $supports,
'yarpp_support' => true
);
register_post_type( $this->PT, $args );
$plural_label = 'Categories';
$singular_label = 'Category';
register_taxonomy(
'business_category',
$this->PT,
array(
'label' => $plural_label,
'labels' => array(
'name' => $plural_label,
'singular_name' => $singular_label,
'all_items' => sprintf(__('All %s', 'claritycloud-directory'), $plural_label),
'edit_item' => sprintf(__('Edit %s', 'claritycloud-directory'), $singular_label),
'view_item' => sprintf(__('View %s', 'claritycloud-directory'), $singular_label),
'update_item' => sprintf(__('Update %s', 'claritycloud-directory'), $singular_label),
'add_new_item' => sprintf(__('Add New %s', 'claritycloud-directory'), $singular_label),
'new_item_name' => sprintf(__('New %s Name', 'claritycloud-directory'), $singular_label),
'popular_items' => sprintf(__('Popular %s', 'claritycloud-directory'), $plural_label),
'search_items' => sprintf(__('Search %s', 'claritycloud-directory'), $plural_label),
),
'public' => true,
'show_ui' => true,
'show_in_rest' => true,
'hierarchical' => true,
'rewrite' => array('slug' => 'business-category'),
)
);
}
Right now my URLs are :
http://localhost/demo-project/directory/
http://localhost/demo-project/business-category/antiques/
http://localhost/demo-project/directory/a-lil-bit-of-sas/
It will be like below:
http://localhost/demo-project/directory/
http://localhost/demo-project/directory/category/antiques/
http://localhost/demo-project/directory/a-lil-bit-of-sas/
Just need to modify the above 2nd URL.
Can anyone please advise me?
Thanks,
Subhankar
Finally, I got the solution here:
add_action('init', 'cpt_resources');
function cpt_resources() {
$labels = array(
'name' => _x('Resources', 'snt'),
'singular_name' => _x('Resource', 'snt'),
'add_new' => _x('Add Resource', 'snt'),
'add_new_item' => __('Add Resource'),
'edit_item' => __('Edit Resource'),
'new_item' => __('New Resource'),
'view_item' => __('View Resource'),
'search_items' => __('Search Resources'),
'not_found' => __('Nothing found'),
'not_found_in_trash' => __('Nothing found in Trash'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'taxonomies' => array('resource_type'),
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'has_archive' => true,
'rewrite' => array( 'slug' => 'resources' ),
'capability_type' => 'post',
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title','thumbnail', 'editor' ),
);
register_post_type( 'resources_post_type' , $args );
}
function resource_type() {
$labels = array(
'name' => _x( 'Resource Types', 'Taxonomy General Name', 'snt' ),
'singular_name' => _x( 'Resource Type', 'Taxonomy Singular Name', 'snt' ),
'menu_name' => __( 'Resource Types', 'snt' ),
'all_items' => __( 'All Items', 'snt' ),
'parent_item' => __( 'Parent Item', 'snt' ),
'parent_item_colon' => __( 'Parent Item:', 'snt' ),
'new_item_name' => __( 'New Item Name', 'snt' ),
'add_new_item' => __( 'Add New Item', 'snt' ),
'edit_item' => __( 'Edit Item', 'snt' ),
'update_item' => __( 'Update Item', 'snt' ),
'view_item' => __( 'View Item', 'snt' ),
'separate_items_with_commas' => __( 'Separate items with commas', 'snt' ),
'add_or_remove_items' => __( 'Add or remove items', 'snt' ),
'choose_from_most_used' => __( 'Choose from the most used', 'snt' ),
'popular_items' => __( 'Popular Items', 'snt' ),
'search_items' => __( 'Search Items', 'snt' ),
'not_found' => __( 'Not Found', 'snt' ),
'no_terms' => __( 'No items', 'snt' ),
'items_list' => __( 'Items list', 'snt' ),
'items_list_navigation' => __( 'Items list navigation', 'snt' ),
);
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => array('slug' => 'resources/category')
);
register_taxonomy( 'resource_type', array( 'resources_post_type' ), $args );
}
add_action( 'init', 'resource_type', 0 );
function resources_cpt_generating_rule($wp_rewrite) {
$rules = array();
$terms = get_terms( array(
'taxonomy' => 'resource_type',
'hide_empty' => false,
) );
$post_type = 'resources_post_type';
foreach ($terms as $term) {
$rules['resources/category/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&resources_post_type=$matches[1]&name=$matches[1]';
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'resources_cpt_generating_rule');
Result :
http://localhost/demo-wp/resources/
http://localhost/demo-wp/resources/category/test-resources/
http://localhost/demo-wp/resources/final-check/

Divi image gallery sorting

Hope someone could help me with this one! I've already asked for their support but "it goes beyond their support level" and they even adviced me to ask it here..
So I'm using the Divi image gallery for my wordpress catalog, which contains lots of images.
Now I'm looking for a way that the images in the gallery are standard sorted by filename.
Any guidelines how to do change this in their code?
Thanks a lot!!
File names for attachments are stored in the 'post_name' column of the posts table. Therefore, this simple change should do the trick.
The simple answer to your question is:
Locate the file divi/includes/builder/main-modules.php
Search for function get_galleryin the file
From there, scroll down slightly to find $attachments_args
Change 'orderby' => 'post__in' to 'orderby' => 'post_name'
The bad news...
Making a change to main-modules.php means either you can no longer do updates or when you do update you'll have to apply the hack again.
Michael is right BUT:
The good news
You you can insert it into your Child Theme and everything is save
The better answer to your question is:
Locate the file divi/includes/builder/main-modules.php
Copy the ET_Builder_Module_Gallery
Insert into your functions.php, wrap and modify like Michael said
Complete code for this
function divi_child_theme_setup() {
if ( class_exists('ET_Builder_Module')) {
class CHILD_ET_Builder_Module_Gallery extends ET_Builder_Module {
function init() {
$this->name = esc_html__( 'Gallery', 'et_builder' );
$this->slug = 'et_pb_gallery';
$this->fb_support = true;
$this->whitelisted_fields = array(
'src',
'gallery_ids',
'gallery_orderby',
'fullwidth',
'posts_number',
'show_title_and_caption',
'show_pagination',
'background_layout',
'auto',
'auto_speed',
'admin_label',
'module_id',
'module_class',
'zoom_icon_color',
'hover_overlay_color',
'hover_icon',
'orientation',
);
$this->fields_defaults = array(
'fullwidth' => array( 'off' ),
'posts_number' => array( 4, 'add_default_setting' ),
'show_title_and_caption' => array( 'on' ),
'show_pagination' => array( 'on' ),
'background_layout' => array( 'light' ),
'auto' => array( 'off' ),
'auto_speed' => array( '7000' ),
'orientation' => array( 'landscape' ),
);
$this->options_toggles = array(
'general' => array(
'toggles' => array(
'main_content' => esc_html__( 'Images', 'et_builder' ),
'elements' => esc_html__( 'Elements', 'et_builder' ),
),
),
'advanced' => array(
'toggles' => array(
'layout' => esc_html__( 'Layout', 'et_builder' ),
'overlay' => esc_html__( 'Overlay', 'et_builder' ),
'text' => array(
'title' => esc_html__( 'Text', 'et_builder' ),
'priority' => 49,
),
),
),
'custom_css' => array(
'toggles' => array(
'animation' => array(
'title' => esc_html__( 'Animation', 'et_builder' ),
'priority' => 90,
),
),
),
);
$this->main_css_element = '%%order_class%%.et_pb_gallery';
$this->advanced_options = array(
'fonts' => array(
'title' => array(
'label' => esc_html__( 'Title', 'et_builder' ),
'css' => array(
'main' => "{$this->main_css_element} .et_pb_gallery_title",
),
),
'caption' => array(
'label' => esc_html__( 'Caption', 'et_builder' ),
'use_all_caps' => true,
'css' => array(
'main' => "{$this->main_css_element} .mfp-title, {$this->main_css_element} .et_pb_gallery_caption",
),
'line_height' => array(
'range_settings' => array(
'min' => '1',
'max' => '100',
'step' => '1',
),
),
'depends_show_if' => 'off'
),
),
'border' => array(
'css' => array(
'main' => "{$this->main_css_element} .et_pb_gallery_item",
),
),
);
$this->custom_css_options = array(
'gallery_item' => array(
'label' => esc_html__( 'Gallery Item', 'et_builder' ),
'selector' => '.et_pb_gallery_item',
),
'overlay' => array(
'label' => esc_html__( 'Overlay', 'et_builder' ),
'selector' => '.et_overlay',
),
'overlay_icon' => array(
'label' => esc_html__( 'Overlay Icon', 'et_builder' ),
'selector' => '.et_overlay:before',
),
'gallery_item_title' => array(
'label' => esc_html__( 'Gallery Item Title', 'et_builder' ),
'selector' => '.et_pb_gallery_title',
),
'gallery_item_caption' => array(
'label' => esc_html__( 'Gallery Item Caption', 'et_builder' ),
'selector' => '.et_pb_gallery_caption',
),
'gallery_pagination' => array(
'label' => esc_html__( 'Gallery Pagination', 'et_builder' ),
'selector' => '.et_pb_gallery_pagination',
),
'gallery_pagination_active' => array(
'label' => esc_html__( 'Pagination Active Page', 'et_builder' ),
'selector' => '.et_pb_gallery_pagination a.active',
),
);
}
function get_fields() {
$fields = array(
'src' => array(
'label' => esc_html__( 'Gallery Images', 'et_builder' ),
'renderer' => 'et_builder_get_gallery_settings',
'option_category' => 'basic_option',
'overwrite' => array(
'ids' => 'gallery_ids',
'orderby' => 'gallery_orderby',
),
'toggle_slug' => 'main_content',
),
'gallery_ids' => array(
'type' => 'hidden',
'class' => array( 'et-pb-gallery-ids-field' ),
'computed_affects' => array(
'__gallery',
),
),
'gallery_orderby' => array(
'label' => esc_html__( 'Gallery Images', 'et_builder' ),
'type' => 'hidden',
'class' => array( 'et-pb-gallery-ids-field' ),
'computed_affects' => array(
'__gallery',
),
'toggle_slug' => 'main_content',
),
'fullwidth' => array(
'label' => esc_html__( 'Layout', 'et_builder' ),
'type' => 'select',
'option_category' => 'layout',
'options' => array(
'off' => esc_html__( 'Grid', 'et_builder' ),
'on' => esc_html__( 'Slider', 'et_builder' ),
),
'description' => esc_html__( 'Toggle between the various blog layout types.', 'et_builder' ),
'affects' => array(
'zoom_icon_color',
'caption_font',
'caption_text_color',
'caption_line_height',
'caption_font_size',
'caption_all_caps',
'caption_letter_spacing',
'hover_overlay_color',
'auto',
'posts_number',
'show_title_and_caption',
'orientation'
),
'computed_affects' => array(
'__gallery',
),
'tab_slug' => 'advanced',
'toggle_slug' => 'layout',
),
'posts_number' => array(
'label' => esc_html__( 'Images Number', 'et_builder' ),
'type' => 'text',
'option_category' => 'configuration',
'description' => esc_html__( 'Define the number of images that should be displayed per page.', 'et_builder' ),
'depends_show_if' => 'off',
'toggle_slug' => 'main_content',
),
'orientation' => array(
'label' => esc_html__( 'Thumbnail Orientation', 'et_builder' ),
'type' => 'select',
'options_category' => 'configuration',
'options' => array(
'landscape' => esc_html__( 'Landscape', 'et_builder' ),
'portrait' => esc_html__( 'Portrait', 'et_builder' )
),
'description' => sprintf(
'%1$s<br><small><em><strong>%2$s:</strong> %3$s %4$s.</em></small>',
esc_html__( 'Choose the orientation of the gallery thumbnails.', 'et_builder' ),
esc_html__( 'Note', 'et_builder' ),
esc_html__( 'If this option appears to have no effect, you might need to', 'et_builder' ),
esc_html__( 'regenerate your thumbnails', 'et_builder')
),
'depends_show_if' => 'off',
'computed_affects' => array(
'__gallery',
),
'tab_slug' => 'advanced',
'toggle_slug' => 'layout',
),
'show_title_and_caption' => array(
'label' => esc_html__( 'Show Title and Caption', 'et_builder' ),
'type' => 'yes_no_button',
'option_category' => 'configuration',
'options' => array(
'on' => esc_html__( 'Yes', 'et_builder' ),
'off' => esc_html__( 'No', 'et_builder' ),
),
'description' => esc_html__( 'Whether or not to show the title and caption for images (if available).', 'et_builder' ),
'depends_show_if' => 'off',
'toggle_slug' => 'elements',
),
'show_pagination' => array(
'label' => esc_html__( 'Show Pagination', 'et_builder' ),
'type' => 'yes_no_button',
'option_category' => 'configuration',
'options' => array(
'on' => esc_html__( 'Yes', 'et_builder' ),
'off' => esc_html__( 'No', 'et_builder' ),
),
'toggle_slug' => 'elements',
'description' => esc_html__( 'Enable or disable pagination for this feed.', 'et_builder' ),
),
'background_layout' => array(
'label' => esc_html__( 'Text Color', 'et_builder' ),
'type' => 'select',
'option_category' => 'color_option',
'options' => array(
'light' => esc_html__( 'Dark', 'et_builder' ),
'dark' => esc_html__( 'Light', 'et_builder' ),
),
'tab_slug' => 'advanced',
'toggle_slug' => 'text',
'description' => esc_html__( 'Here you can choose whether your text should be light or dark. If you are working with a dark background, then your text should be light. If your background is light, then your text should be set to dark.', 'et_builder' ),
),
'auto' => array(
'label' => esc_html__( 'Automatic Animation', 'et_builder' ),
'type' => 'yes_no_button',
'option_category' => 'configuration',
'options' => array(
'off' => esc_html__( 'Off', 'et_builder' ),
'on' => esc_html__( 'On', 'et_builder' ),
),
'affects' => array(
'auto_speed',
),
'depends_show_if' => 'on',
'depends_to' => array(
'fullwidth',
),
'tab_slug' => 'custom_css',
'toggle_slug' => 'animation',
'description' => esc_html__( 'If you would like the slider to slide automatically, without the visitor having to click the next button, enable this option and then adjust the rotation speed below if desired.', 'et_builder' ),
),
'auto_speed' => array(
'label' => esc_html__( 'Automatic Animation Speed (in ms)', 'et_builder' ),
'type' => 'text',
'option_category' => 'configuration',
'depends_default' => true,
'tab_slug' => 'custom_css',
'toggle_slug' => 'animation',
'description' => esc_html__( "Here you can designate how fast the slider fades between each slide, if 'Automatic Animation' option is enabled above. The higher the number the longer the pause between each rotation.", 'et_builder' ),
),
'zoom_icon_color' => array(
'label' => esc_html__( 'Zoom Icon Color', 'et_builder' ),
'type' => 'color-alpha',
'custom_color' => true,
'depends_show_if' => 'off',
'tab_slug' => 'advanced',
'toggle_slug' => 'overlay',
),
'hover_overlay_color' => array(
'label' => esc_html__( 'Hover Overlay Color', 'et_builder' ),
'type' => 'color-alpha',
'custom_color' => true,
'depends_show_if' => 'off',
'tab_slug' => 'advanced',
'toggle_slug' => 'overlay',
),
'hover_icon' => array(
'label' => esc_html__( 'Hover Icon Picker', 'et_builder' ),
'type' => 'text',
'option_category' => 'configuration',
'class' => array( 'et-pb-font-icon' ),
'renderer' => 'et_pb_get_font_icon_list',
'renderer_with_field' => true,
'tab_slug' => 'advanced',
'toggle_slug' => 'overlay',
),
'disabled_on' => array(
'label' => esc_html__( 'Disable on', 'et_builder' ),
'type' => 'multiple_checkboxes',
'options' => array(
'phone' => esc_html__( 'Phone', 'et_builder' ),
'tablet' => esc_html__( 'Tablet', 'et_builder' ),
'desktop' => esc_html__( 'Desktop', 'et_builder' ),
),
'additional_att' => 'disable_on',
'option_category' => 'configuration',
'description' => esc_html__( 'This will disable the module on selected devices', 'et_builder' ),
'tab_slug' => 'custom_css',
'toggle_slug' => 'visibility',
),
'admin_label' => array(
'label' => esc_html__( 'Admin Label', 'et_builder' ),
'type' => 'text',
'description' => esc_html__( 'This will change the label of the module in the builder for easy identification.', 'et_builder' ),
'toggle_slug' => 'admin_label',
),
'module_id' => array(
'label' => esc_html__( 'CSS ID', 'et_builder' ),
'type' => 'text',
'option_category' => 'configuration',
'tab_slug' => 'custom_css',
'toggle_slug' => 'classes',
'option_class' => 'et_pb_custom_css_regular',
),
'module_class' => array(
'label' => esc_html__( 'CSS Class', 'et_builder' ),
'type' => 'text',
'option_category' => 'configuration',
'tab_slug' => 'custom_css',
'toggle_slug' => 'classes',
'option_class' => 'et_pb_custom_css_regular',
),
'__gallery' => array(
'type' => 'computed',
'computed_callback' => array( 'ET_Builder_Module_Gallery', 'get_gallery' ),
'computed_depends_on' => array(
'gallery_ids',
'gallery_orderby',
'fullwidth',
'orientation',
),
),
);
return $fields;
}
/**
* Get attachment data for gallery module
*
* #param array $args {
* Gallery Options
*
* #type array $gallery_ids Attachment Ids of images to be included in gallery.
* #type string $gallery_orderby `orderby` arg for query. Optional.
* #type string $fullwidth on|off to determine grid / slider layout
* #type string $orientation Orientation of thumbnails (landscape|portrait).
* }
* #param array $conditional_tags
* #param array $current_page
*
* #return array Attachments data
*/
static function get_gallery( $args = array(), $conditional_tags = array(), $current_page = array() ) {
$attachments = array();
$defaults = array(
'gallery_ids' => array(),
'gallery_orderby' => '',
'fullwidth' => 'off',
'orientation' => 'landscape',
);
$args = wp_parse_args( $args, $defaults );
$attachments_args = array(
'include' => $args['gallery_ids'],
'post_status' => 'inherit',
'post_type' => 'attachment',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'post_name',
);
if ( 'rand' === $args['gallery_orderby'] ) {
$attachments_args['orderby'] = 'rand';
}
if ( 'on' === $args['fullwidth'] ) {
$width = 1080;
$height = 9999;
} else {
$width = 400;
$height = ( 'landscape' === $args['orientation'] ) ? 284 : 516;
}
$width = (int) apply_filters( 'et_pb_gallery_image_width', $width );
$height = (int) apply_filters( 'et_pb_gallery_image_height', $height );
$_attachments = get_posts( $attachments_args );
foreach ( $_attachments as $key => $val ) {
$attachments[$key] = $_attachments[$key];
$attachments[$key]->image_src_full = wp_get_attachment_image_src( $val->ID, 'full' );
$attachments[$key]->image_src_thumb = wp_get_attachment_image_src( $val->ID, array( $width, $height ) );
}
return $attachments;
}
function shortcode_callback( $atts, $content = null, $function_name ) {
$module_id = $this->shortcode_atts['module_id'];
$module_class = $this->shortcode_atts['module_class'];
$gallery_ids = $this->shortcode_atts['gallery_ids'];
$fullwidth = $this->shortcode_atts['fullwidth'];
$show_title_and_caption = $this->shortcode_atts['show_title_and_caption'];
$background_layout = $this->shortcode_atts['background_layout'];
$posts_number = $this->shortcode_atts['posts_number'];
$show_pagination = $this->shortcode_atts['show_pagination'];
$gallery_orderby = $this->shortcode_atts['gallery_orderby'];
$zoom_icon_color = $this->shortcode_atts['zoom_icon_color'];
$hover_overlay_color = $this->shortcode_atts['hover_overlay_color'];
$hover_icon = $this->shortcode_atts['hover_icon'];
$auto = $this->shortcode_atts['auto'];
$auto_speed = $this->shortcode_atts['auto_speed'];
$orientation = $this->shortcode_atts['orientation'];
$module_class = ET_Builder_Element::add_module_order_class( $module_class, $function_name );
if ( '' !== $zoom_icon_color ) {
ET_Builder_Element::set_style( $function_name, array(
'selector' => '%%order_class%% .et_overlay:before',
'declaration' => sprintf(
'color: %1$s !important;',
esc_html( $zoom_icon_color )
),
) );
}
if ( '' !== $hover_overlay_color ) {
ET_Builder_Element::set_style( $function_name, array(
'selector' => '%%order_class%% .et_overlay',
'declaration' => sprintf(
'background-color: %1$s;
border-color: %1$s;',
esc_html( $hover_overlay_color )
),
) );
}
// Get gallery item data
$attachments = self::get_gallery( array(
'gallery_ids' => $gallery_ids,
'gallery_orderby' => $gallery_orderby,
'fullwidth' => $fullwidth,
'orientation' => $orientation,
) );
if ( empty( $attachments ) ) {
return '';
}
wp_enqueue_script( 'hashchange' );
$fullwidth_class = 'on' === $fullwidth ? ' et_pb_slider et_pb_gallery_fullwidth' : ' et_pb_gallery_grid';
$background_class = " et_pb_bg_layout_{$background_layout}";
$module_class .= 'on' === $auto && 'on' === $fullwidth ? ' et_slider_auto et_slider_speed_' . esc_attr( $auto_speed ) : '';
$posts_number = 0 === intval( $posts_number ) ? 4 : intval( $posts_number );
$output = sprintf(
'<div%1$s class="et_pb_module et_pb_gallery%2$s%3$s%4$s clearfix">
<div class="et_pb_gallery_items et_post_gallery" data-per_page="%5$d">',
( '' !== $module_id ? sprintf( ' id="%1$s"', esc_attr( $module_id ) ) : '' ),
( '' !== $module_class ? sprintf( ' %1$s', esc_attr( ltrim( $module_class ) ) ) : '' ),
esc_attr( $fullwidth_class ),
esc_attr( $background_class ),
esc_attr( $posts_number )
);
foreach ( $attachments as $id => $attachment ) {
$data_icon = '' !== $hover_icon
? sprintf(
' data-icon="%1$s"',
esc_attr( et_pb_process_font_icon( $hover_icon ) )
)
: '';
$image_output = sprintf(
'<a href="%1$s" title="%2$s">
<img src="%3$s" alt="%2$s" />
<span class="et_overlay%4$s"%5$s></span>
</a>',
esc_url( $attachment->image_src_full[0] ),
esc_attr( $attachment->post_title ),
esc_url( $attachment->image_src_thumb[0] ),
( '' !== $hover_icon ? ' et_pb_inline_icon' : '' ),
$data_icon
);
$output .= sprintf(
'<div class="et_pb_gallery_item%2$s%1$s">',
esc_attr( $background_class ),
( 'on' !== $fullwidth ? ' et_pb_grid_item' : '' )
);
$output .= "
<div class='et_pb_gallery_image {$orientation}'>
$image_output
</div>";
if ( 'on' !== $fullwidth && 'on' === $show_title_and_caption ) {
if ( trim($attachment->post_title) ) {
$output .= "
<h3 class='et_pb_gallery_title'>
" . wptexturize($attachment->post_title) . "
</h3>";
}
if ( trim($attachment->post_excerpt) ) {
$output .= "
<p class='et_pb_gallery_caption'>
" . wptexturize($attachment->post_excerpt) . "
</p>";
}
}
$output .= "</div>";
}
$output .= "</div><!-- .et_pb_gallery_items -->";
if ( 'on' !== $fullwidth && 'on' === $show_pagination ) {
$output .= "<div class='et_pb_gallery_pagination'></div>";
}
$output .= "</div><!-- .et_pb_gallery -->";
return $output;
}
}
$cgm = new CHILD_ET_Builder_Module_Gallery();
remove_shortcode( 'et_pb_gallery' );
add_shortcode( 'et_pb_gallery', array($cgm, '_shortcode_callback') );
}
}
add_action('wp', 'divi_child_theme_setup', 9999);
I used code from Divi 3.0.51. have fun

Cake PHP 2.8.0 not validating

I have the folowing validate inside my Workshops class
public $validate = array(
'programa'=> array(
'allowEmpty' => false,
'required' => true
),
'lugar'=> array(
'allowEmpty' => false,
'required' => true
),
'fecha_inicio'=> array(
'allowEmpty' => false,
'required' => true
),
'fecha_fin'=> array(
'allowEmpty' => false,
'required' => true
),
'objetivo'=> array(
'allowEmpty' => false,
'required' => true
),
'instructor'=> array(
'allowEmpty' => false,
'required' => true
)
);
however when I save with empty fields a record is created in the database, the validation is not being done.
This is the code for my add form
<h1>Agregar Taller</h1>
<?php
echo $this->Form->create('Workshop');
echo $this->Form->input('nombre');
echo $this->Form->input('fecha_inicio',
array(
'label' => 'Fecha Inicio',
'dateFormat' => 'DMYhm',
'minYear' => date('Y') - 0,
'maxYear' => date('Y') + 2,
));
echo $this->Form->input('fecha_fin',
array(
'label' => 'Fecha fin',
'dateFormat' => 'DMYhm',
'minYear' => date('Y') - 0,
'maxYear' => date('Y') + 2,
));
echo $this->Form->input('caracteristica_tec21');
echo $this->Form->input('programa');
echo $this->Form->input('lugar');
echo $this->Form->input('instructor');
echo $this->Form->input('objetivo', array('rows' => '3'));
echo $this->Form->end('Guardar Taller');
?>
And here is my controller
<?php
class WorkshopsController extends AppController {
public $helpers = array('Html', 'Form');
public function index() {
$this->set('workshops',$this->Workshop->find('all'));
}
public function view($id = null){
if(!$id){
throw new NotFoundException(__('Taller inválido'));
}
$workshop = $this->Workshop->findById($id);
if (!$workshop) {
throw new NotFoundException(__('Taller inválido'));
}
$this->set('workshop', $workshop);
}
public function add() {
if ($this->request->is('post')) {
$this->Workshop->create();
if ($this->Workshop->save($this->request->data)) {
$this->Flash->success(__('El taller se ha agregado'));
return $this->redirect(array('action' => 'index'));
}else{
$this->Flash->error(__('No se ha podido agregar el taller'));
}
}
}
}
?>
You suppose to add a message field in every form field's validation, this is the message which shown in view file. Please see the below:
public $validate = array(
'programa' => array(
'allowEmpty' => false,
'required' => true,
'message' => "Please Enter the programa."
),
'lugar' => array(
'allowEmpty' => false,
'required' => true,
'message' => "Please Enter the lugar."
),
'fecha_inicio' => array(
'allowEmpty' => false,
'required' => true,
'message' => "Please Enter the fecha_inicio."
),
'fecha_fin' => array(
'allowEmpty' => false,
'required' => true,
'message' => "Please Enter the fecha_fin."
),
'objetivo' => array(
'allowEmpty' => false,
'required' => true,
'message' => "Please Enter the objetivo."
),
'instructor' => array(
'allowEmpty' => false,
'required' => true,
'message' => "Please Enter the instructor."
)
);
If the answer is correct, Please don't forget to mark it as a answer.

Ajax replace method does not working error

Ajax replace method doesn't working in these code.But append method is working.
<?php
$currentDate = date("Y-m-d");
$currentTime = date("Y-m-d");
global $user;
function form_test_permission()
{
return array(
'submit_form_test' =>array(
'title' =>t('Submit_form_test'),
'description' => t('Submit the form_test form'),
),
);
}
function form_test_menu() {
$items = array();
$items['production'] = array(
'title' =>'Production',
'type' => MENU_NORMAL_ITEM,
'access arguments' => array('submit_form_test'),
'page callback' => 'drupal_get_form',
'page arguments' => array('form_test_form'),
);
return $items;
}
function form_test_form($form,&$form_state) {
$form['production'] = array(
'#title' => t('production'),
'#type' => 'hidden',
'#value'=> '1',
);
$form['production1'] = array(
'#title' => t('production1'),
'#type' => 'hidden',
);
$form['production_date'] = array(
'#title' => t('production_date'),
'#type' => 'hidden',
'#value' => date('Y-m-d'),
);
$form['production_time'] = array(
'#title' => t('production_time'),
'#type' => 'hidden',
'#value' => date('H:i:s'),
);
$form['production23'] = array(
'#title' => t('production23'),
'#type' => 'hidden',
'#value'=> '1',
);
$form['button2'] = array(
'#value' => 'UNDO',
'#type' => 'submit',
'#prefix' => '<div class="test">',
'#suffix' => '</div>',
'#submit' => array('form_test_form_button2'),
);
$form['button1'] = array(
'#value' => ' ADD ',
'#type' => 'submit',
'#prefix' => '<div class="test1">',
'#suffix' => '</div>',
'#submit' => array('form_test_form_button1'),
'#ajax' => array(
'callback' => 'ajax_example_submit_driven_callback',
'method' => 'replace',
'effect' => 'fade',
'event' =>'click',
'wrapper' => 'countvalue',
),
);
$form['start_counter'] = array(
'#type' => 'select',
'#options' => array (
'Start' => 'Start',
'2501 ' => '2501 ',
'302 ' => '302 ',
'1107 ' => '1107',
'1104' => '1104',
'0106' => '0106',
'0305' => '0305',
'0103' => '0103',
),
'#prefix' => ' <div class="box chart gradient" style="width:30%;float:left;margin-top: -186px;">
<div class="title">
<h4>
<span style="text-align:center;">Downtime</span>
</h4>
</div>
<div class="content" style="padding-bottom:0;">
<p class="dowtime-show" id="counter"></p>',
);
$form['start_counter1'] = array(
'#type' => 'textfield',
);
$form['stop_counter'] = array(
'#type' => 'select',
'#options' => array (
'Stop'=>'Stop',
'Mechanical' => 'Mechanical',
'Electrical' => 'Electrical',
'Programming' => 'Programming',
'Setup' => 'Setup',
'PlannedMaintenance' => 'PlannedMaintenance'
),
'#suffix' => '</div> </div>',
);
$form['counter'] = array(
'#type' => 'textfield',
);
$form['button3'] = array(
'#value' => 'submit',
'#type' => 'submit',
'#submit' => array('form_test_form_button3'),
);
return $form;
}
function ajax_example_submit_driven_callback($form,$form_state){
$result = db_query("SELECT * FROM form_test WHERE production_date='2014-02-10' and production_time between '01:00:00' and '02:00:00'");
$prod_1= $result->rowcount();
$form['#tree'] = TRUE;
$form_state['rebuild'] = TRUE;
return 'count value is:'.$prod_1.'';
}
function form_test_form_button1($form,&$form_state){
$ft_id = db_insert('form_test')
->fields(array(
'production' => $form_state['values']['production'],
'production_date' => $form_state['values']['production_date'],
'production_time' => $form_state['values']['production_time'],
'production1' => $form_state['values']['production1'],
))
->execute();
$form_state['rebuild'] = true;
}
function form_test_form_button2($form,&$form_state){
$ft_id = db_insert('form_test1')
->fields(array(
'production23' => $form_state['values']['production23'],
'production_date' => $form_state['values']['production_date'],
'production_time' => $form_state['values']['production_time'],
))
->execute();
}
function form_test_form_button3($form,&$form_state){
$ft_id = db_insert('downtime1')
->fields(array(
'start_counter1' => $form_state['values']['start_counter1'],
'stop_counter' => $form_state['values']['stop_counter'],
'counter' => $form_state['values']['counter'],
'production_date' => $form_state['values']['production_date'],
'production_time' => $form_state['values']['production_time'],
))
->execute();
}
I spend a lot of hours to get the result but cannot able to get it.Anybody know how to solve these kind of Errors.The main problem is that it doesn't change the value while using replace method.

ajax function in drupal 6 not working

I need to filter the territory field upon the value of the region field im using ajax function like it's mentioned in the below code but it's not working. THE AJAX CALL on the fonction is not getting into the function 'territory_filter_callback' anyway would know where is the error?
function form_search_menu() {
$items['form/search'] = array(
'title' => t('Search'),
'page callback' => 'drupal_get_form',
'page arguments' => array('_search'),
'access callback' => TRUE,
'description' => t('search'),
);
return $items;
}
function _search(&$form_state) {
drupal_add_js(drupal_get_path('module', 'form_search') .'/script.js');
$form['description'] = array(
'#type' => 'item',
'#title' => t('Search page'),
);
//FILL THE LIST OF REGIONS
$conn = oci_connect('webuser', 'website', '172.16.1.1');
//regions
$stid = oci_parse($conn, "SELECt code,descr1 FROM table1.region");
oci_execute($stid);
$cidades = array(); while (($row = oci_fetch_array($stid, OCI_ASSOC))) {
$cidades[$row['CODE']]= $row['DESCR1'];
} $cidades ['']='Select';
//city
$stid1 = oci_parse($conn, "SELECT code,descr1 from table1.city ORDER BY DESCR1"); oci_execute($stid1); $types = array(); while (($row = oci_fetch_array($stid1, OCI_ASSOC))) {
$types [$row['CODE']]= $row['DESCR1'];
} $types['']='Select';
//territory
$stid2 = oci_parse($conn, "SELECT code,descr1 FROM table1.territory"); oci_execute($stid2);
$territory = array();
while (($row = oci_fetch_array($stid2, OCI_ASSOC))) {
$territory [$row['CODE']]= $row['DESCR1'];
}
$territory ['']='Select';
oci_free_statement($stid);
oci_close($conn);
$form['name'] = array(
'#type' => 'fieldset',
//'#title' => t('Name'),
// Make the fieldset collapsible.
'#collapsible' => false, // Added
'#collapsed' => FALSE, // Added
);
$form['name']['Region'] = array(
'#type' => 'select',
'#title' => t('Region'),
'#options' => $cidades,
'#required' => FALSE, '#default_value' => isset($form_state['values']['name']['Region']) ? $form_state ['values']['name']['Region'] : '',
'#ajax' => array(
'event' => 'change',
'callback' => 'territory_filter_callback',
'wrapper' => 'dropdown_second_replace'
),
// Added
);
$form['name']['Territory'] = array(
'#type' => 'select',
'#title' => t('Territory'), '#prefix' => '<div id="dropdown_second_replace">', '#suffix' => '</div>',
'#options' => $territory,
'#required' => FALSE, '#default_value' => isset($form_state['values']['name']['Territory']) ? $form_state['values']['name']['Territory'] : '',
// Added );
$form['name']['City'] = array(
'#type' => 'select',
'#title' => t('City'),
'#options' => $types,
'#required' => FALSE,
// Added
);
$form['link'] = array(
'#type' => 'markup',
'#value' => '<a href="#" onclick="navigate()" ><input type="button" value="Search" style="background-color:#2A64A9;color:#FFFFFF;width:80px; height:25px; border:1;CURSOR:POINTER;float:right;border-color:#FFFFFF;" ></a>', //
return $form;
}
function territory_filter_callback(&$form,&$form_state)
{
$territory_options=array();
if(isset($form['name']['Region']['#default_value']['0'])
{ $Region=$form['name']['Region']['#default_value']['0'];
}
else
{ $Region=0;
}
$territory_options=selected_territory($Region);
$form['name']['Region']['#ajax']=
array('event' => 'change',
'wrapper' => 'territory_wrapper',
'callback' => 'filter_territory_callback',
'method' =>replace,
);
$form ['name']['territory']['prefix']='<div id="territory_wrapper">';
$form ['name']['territory']['prefix']='</div>';
$form ['territory']['#options']=$territory_options;
}
function filter_territory_callback($form,$form_state)
{
$Region=$form['name']['Region']['#value'];
$form['name']['territory']['#options']=selected_territory($Region);
return $form['territory'];
}
drupal 6 not support '#ajax'...'#ajax' is working with drupal 7...in drupal 6 you can use '#ahah'
http://drupal.org/node/331941
good luck

Resources