Skip to content

Instantly share code, notes, and snippets.

View mrkkr's full-sized avatar

Marek mrkkr

  • Poland
View GitHub Profile
@mrkkr
mrkkr / multilevel_menu_wp_recursion.php
Last active January 17, 2020 10:19
Generate multilevel nested menu in Wordpress using recursion function
<?php
/**
* Custom menu generator
*/
function generateTreeNavwalker(array &$menuItems, $parentId = 0) {
$branch = array();
foreach ($menuItems as &$item) {
if ($item->menu_item_parent == $parentId) {
$children = generateTreeNavwalker($menuItems, $item->ID);
if ($children) {
@mrkkr
mrkkr / additional_active_item_classes.php
Created April 5, 2019 10:02
Add top level menu active menu class to child pages without adding submenu level in admin panel
<?php
function additional_active_item_classes( $classes = array(), $menu_item = false ) {
// custom taxonomy
if ( $menu_item->title == 'Custom Tax Name Page' && is_tax('custom_tax') ) {
$classes[] = 'current-menu-item';
}
// custom post type single
if ( $menu_item->title == 'Custom Post Type Page' && is_singular('products') ) {
$classes[] = 'current-menu-item';
@mrkkr
mrkkr / display_comment_from_selected_post.php
Created February 8, 2019 11:42
Wordpress - display comments form selected post
<?php
$args = array('cat' => 'home','post_type' => 'post'));
$post_obj = new WP_Query($args);
while($post_obj->have_posts() ) : $post_obj->the_post();
//display comments
$comments = get_comments(array(
'post_id' => $post->ID,
'number' => '2' ));
foreach($comments as $comment) {
//format comments
@mrkkr
mrkkr / update_wp_urls.sql
Created February 5, 2019 13:54
Wordpress - Update all old URLs in database with SQL
UPDATE wp_options SET option_value = replace(option_value, 'OLD URL', 'NEW URL') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET post_content = replace(post_content, 'OLD URL', 'NEW URL');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'OLD URL','NEW URL');
UPDATE wp_usermeta SET meta_value = replace(meta_value, 'OLD URL','NEW URL');
UPDATE wp_links SET link_url = replace(link_url, 'OLD URL','NEW URL');
UPDATE wp_comments SET comment_content = replace(comment_content , 'OLD URL','NEW URL');
UPDATE wp_posts SET post_content = replace(post_content, 'OLD URL', 'NEW URL');
UPDATE wp_links SET link_image = replace(link_image, 'OLD URL','NEW URL');
UPDATE wp_posts SET guid = replace(guid, 'OLD URL','NEW URL');
@mrkkr
mrkkr / custom_user_meta_in_users_and_edit_this_ind_user_page.php
Created September 7, 2018 15:06
Add column with custom user meta in admin panel and edit this in edit profile page
<?php
/* Add custom column */
function woo_add_user_nip_column( $columns ) { // e.g. NIP
$columns['billing_company_nip'] = __( 'NIP', 'theme' );
return $columns;
}
add_filter( 'manage_users_columns', 'woo_add_user_nip_column' );
@mrkkr
mrkkr / get_current_url_wp.php
Created May 8, 2018 09:32
Wordpress - get current url by PHP #php
<?php
/// without prefix
$current_url = "//" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
/// with prefix
global $wp;
$current_url = home_url( add_query_arg( array(), $wp->request ) );
@mrkkr
mrkkr / add_custom_items_wrap_menu_wp.php
Created April 26, 2018 14:24
Add custom items (wraps) to menu item Wordpress #php
<?php
function my_nav_wrap() {
// default value of 'items_wrap' is <ul id="%1$s" class="%2$s">%3$s</ul>'
ob_start();
the_widget( 'WC_Widget_Product_Search' ); //in this case I need WC widget
$widget = ob_get_clean();
// open the <ul>, set 'menu_class' and 'menu_id' values
$wrap = '<ul id="%1$s" class="%2$s">';
@mrkkr
mrkkr / woo_dropdown_product_tag_filter.php
Last active April 26, 2018 11:50 — forked from dustyf/gist:5862035
Woo Commerce Dropdown to filter product tag within an archive page.
<?php
function displayLocationDropdown() {
$html = '';
$html .= '<form class="location-select" method="post">';
$html .= '<select id="location-selector" name="location" class="location">';
$tag = wp_tag_cloud( array(
'format' => 'array',
@mrkkr
mrkkr / hide_default_woo_cat_from_widget.php
Created April 25, 2018 12:18
Hide default woocommerce category from product category widget #php
<?php
/* hide uncategorized product category */
add_filter( 'woocommerce_product_categories_widget_args', 'custom_woocommerce_product_subcategories_args' );
function custom_woocommerce_product_subcategories_args( $args ) {
$args['exclude'] = get_option( 'default_product_cat' );
return $args;
}
@mrkkr
mrkkr / add_custom_items_to_wp_menu.php
Created April 25, 2018 12:08
Add custom items to menu in Wordpress #php
<?php
/* add custom items to primary menu */
add_filter('wp_nav_menu_items','add_search_box_to_menu', 10, 2);
function add_search_box_to_menu( $items, $args ) {
if( $args->theme_location == 'primary' ) /* ID of menu */
return $items.'CUSTOM THINGS';
return $items;
}