Skip to content

Instantly share code, notes, and snippets.

@mica-sirup
Created July 14, 2023 06:55
Show Gist options
  • Select an option

  • Save mica-sirup/892fe94992daf7546edc9b96bffa3ecf to your computer and use it in GitHub Desktop.

Select an option

Save mica-sirup/892fe94992daf7546edc9b96bffa3ecf to your computer and use it in GitHub Desktop.
Hook and change burger svg icon in the wp navigation block
<?php
function custom_burger_svg_icon_render_block_core_navigation (string $block_content, array $block){
if ($block['blockName'] === 'core/navigation' && !is_admin() && !wp_is_json_request()) {
// replace $svg content with your burger icon > this one was made by ngamlerdlek.design https://thenounproject.com/icon/burger-1126774/
$svg='<svg id="burger" xmlns="http://www.w3.org/2000/svg" with="64" height="64" viewBox="0 0 128 128"><path d="M11.81 88.57c0 8.08 5.68 14.62 12.56 14.62h80.15c6.88 0 12.56-6.54 12.56-14.62v-2.92H11.81v2.92Zm52.63-64.15c-29.06 0-52.63 19.44-52.63 43.17h105.25c-.17-23.91-23.56-43.17-52.62-43.17Zm55.03 47.64H9.23c-2.4 0-4.47 2.06-4.47 4.47S6.82 81 9.23 81h110.24c2.4 0 4.47-2.06 4.47-4.47 0-2.41-1.89-4.47-4.47-4.47Z"/></svg>';
// Replace the existing burger icons 3 lines burger (you have to change the 2 lines to 3 lines icon in wp-nav block) in the block content with the custom SVG
return preg_replace('/\<svg width="24" height="24" xmlns="http:\/\/www.w3.org\/2000\/svg" viewBox="0 0 24 24"\>\<path d="M5 5v1.5h14V5H5zm0 7.8h14v-1.5H5v1.5zM5 19h14v-1.5H5V19z" \/\>\<\/svg\>/', $svg, $block_content);
}
return $block_content;
}
// Hook the custom function to the 'render_block' filter
add_filter('render_block', 'custom_burger_svg_icon_render_block_core_navigation', null, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment