current_menu_parent for custom post type and custom url

Ended up hooking the nav_menu_css_class filter, and adding the appropriate class when needed. Like this: add_filter( ‘nav_menu_css_class’, ‘add_parent_menu_class’, 10, 2 ); function add_parent_menu_class( $classes, $item ) { $post = get_post(); if ( ! $post ) { return $classes; } if ( $item->url === get_bloginfo( ‘url’ ) . ‘/custom_url’ && get_post_type() == $this->post_type ) { $classes[] … Read more

add custom entries to menu options

Here a very quick example. The idea is to add a new meta box in the menu configuration. add_meta_box is used in admin_head-nav-menus.php page: class Custom_Nav { function __construct() { add_action( ‘admin_head-nav-menus.php’, array( $this, ‘add_nav_menu_meta_boxes’ ) ); } public function add_nav_menu_meta_boxes() { add_meta_box( ‘custom_links’, __(‘Custom links’), array( $this, ‘nav_menu_link’), ‘nav-menus’, ‘side’, ‘low’ ); } public … Read more

WordPress Rewrite Issue

The rewrite rules are wrong. For example, you want to rewrite from community/tag/easter to ‘index.php?pagename=community&stencil-tag=easter’, so the regex should contain community/tag/ and not only tag/. Also, \d match only digits but the tag value is a string. Same apply to the rewrite for stencil-tag. You could use . to match any character, both numeric and … Read more

Finding and removing unnecessary redirects

While in the process of implementing a CSP and defining the allowed sources, I was shocked to find the total number of resources ShareThis calls. At the time of writing this, I counted a total of 39 domains and subdomains it uses. Not a big surprise anymore why ShareThis was causing my slow page load … Read more

Rewrite Url to a SEO-friendly format

The easiest way to do this is with the built-in $wp_rewrite global. Something like this should give you a good start: function new_rewrite_rules(){ global $wp_rewrite; $keytag = ‘%user_id%’; $wp_rewrite->add_rewrite_tag($keytag, ‘(.+?)’, ‘user=”); $structure = $wp_rewrite->root . “username-$keytag/”; $rewrite = $wp_rewrite->generate_rewrite_rules($structure); if(!is_array($wp_rewrite->rules)) $wp_rewrite->rules = array(); $wp_rewrite->rules = $rewrite + $wp_rewrite->rules; return $wp_rewrite->rules; } You would then access … Read more