Skip to content

Instantly share code, notes, and snippets.

View kirandash's full-sized avatar

Kiran kirandash

View GitHub Profile
@kirandash
kirandash / WooCommerce - get category for product page
Last active August 19, 2016 04:59
A WC product may belong to none, one or more WC categories. Supposing you just want to get one WC category id.
global $post;
$terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($terms as $term) {
$product_cat_id = $term->term_id;
break;
}
@kirandash
kirandash / Add remove action via functions.php
Created August 17, 2016 05:47
Add remove action via functions.php for child theme
function primrose_child_add_remove(){
remove_action( 'woocommerce_before_shop_loop', 'primrose_woocommerce_subcategories', 10 );
add_action( 'woocommerce_before_shop_loop', 'primrose_child_woocommerce_subcategories', 10 );
}
add_action('init','primrose_child_add_remove');
/**
* Extend WordPress search to include custom fields
*
* http://adambalee.com
*/
/**
* Join posts and postmeta tables
*
* http://codex.wordpress.org/Plugin_API/Filter_Reference/posts_join
@kirandash
kirandash / CUSTOM LANGUAGE SWITCHER
Last active July 16, 2023 05:59
Custom language switcher for the wpml plugin that shows the language code
<?php
// IN FUNCTIONS.PHP FILE CUSTOM LANGUAGE SWITCHER
function language_selector_flags(){
$languages = icl_get_languages('skip_missing=0&orderby=code');
if(!empty($languages)){
echo '<ul class="menu language-menu">';
foreach($languages as $l){
if($l['active'] == 1){ $class = 'lang_sel_sel'; }
elseif ($l['active'] == 0) {
<?php
//functions.php file function
function get_menu_parent_ID($menu_name){
if(!isset($menu_name)){
return "No menu name provided in arguments";
}
$menu_slug = $menu_name;
$locations = get_nav_menu_locations();
$menu_id = $locations[$menu_slug];
@kirandash
kirandash / Filter p tags with images - WordPress
Created July 29, 2016 07:42
In case you want to have <img> in your content but not have them get "Auto P'd" like WordPress likes to do.
<?php
/* Filter p tags with images */
function kd_filter_ptags_on_images($content) {
$content = preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
return preg_replace('/<p>\s*(<iframe .*>*.<\/iframe>)\s*<\/p>/iU', '\1', $content);
}
add_filter('acf_the_content', 'kd_filter_ptags_on_images');
add_filter('the_content', 'kd_filter_ptags_on_images');
?>
@kirandash
kirandash / Mark parent navigation active when on custom post type single page
Created July 22, 2016 06:58
Mark (highlight) custom post type parent as active item in Wordpress Navigation. When you visit a custom post type's single page, the parent menu item (the post type archive) isn't marked as active. This code solves it by comparing the slug of the current post type with the navigation items, and adds a class accordingly.
add_action('nav_menu_css_class', 'add_current_nav_class', 10, 2 );
function add_current_nav_class($classes, $item) {
// Getting the current post details
global $post;
// Getting the post type of the current post
$current_post_type = get_post_type_object(get_post_type($post->ID));
$current_post_type_slug = $current_post_type->rewrite['slug'];
function add_marker( $marker, map ) {
// var
var latlng = new google.maps.LatLng( $marker.attr('data-lat'), $marker.attr('data-lng') );
// create marker
var marker = new google.maps.Marker({
position : latlng,
map : map,
icon: "http://de-vos.nl//wp-content/themes/devos/img/mapMarker.png"
//Get page title with ID
$pageID = 26;
$page = get_post($pageID);
echo $page->post_title;
echo $page->post_content;
//Get page thumbnail with ID
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id('26'), 'post');
echo $thumb[0];
@kirandash
kirandash / Gravity Forms + Bootstrap
Created July 14, 2016 10:00
Helpful function for adding Bootstrap classes to Gravity Forms fields.
<?php
/**
* Gravity Forms Bootstrap Styles
*
* Applies bootstrap classes to various common field types.
* Requires Bootstrap to be in use by the theme.
*
* Using this function allows use of Gravity Forms default CSS
* in conjuction with Bootstrap (benefit for fields types such as Address).
*