• Hi.
    Im building a site where the users will be adding levels in the menu that should not be links, just a way of viewing the pages that are under it. Is there a plugin way of removing the anchor from all Custom links and hiding the URL option on the admin page, or is it some kind of core hacking ive got to do? I dont want them to have to put a # on every link, since there will be a lot of them. Please tell me how to do this. Thanks in advance!

Viewing 12 replies - 1 through 12 (of 12 total)
  • Maybe something like this? Untested code:

    function remove_title_attr( $nav )
    {
    return $nav = preg_replace('/href=\"(.*?)\"/', 'href=\"#\"', $nav );
    }
    
    add_filter( 'wp_nav_menu', 'remove_title_attre' );
    add_filter( 'wp_page_menu', 'remove_title_attr' );
    add_filter( 'wp_list_categories', 'remove_title_attr' );

    Source: https://www.socreative.tv/blog/remove-title-attributes-from-wp_nav_menu/

    Thread Starter mYrAn

    (@myran)

    That removes the title on the page itself ?? I want to remove fields from the adminmenu and possibly anchors (not necessery) from the site..

    No, if you look the code I posted, I edited the code there.

    Thread Starter mYrAn

    (@myran)

    Doesnt do anything in the adminmenu but this appears where the menu was

    Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'remove_title_attre' not found or invalid function name in E:\www\*\wp-includes\plugin.php on line 170

    Sorry, I got that wrong. Where in admin panel exactly?

    Thread Starter mYrAn

    (@myran)

    on wp-admin/nav-menus.php, custom links. I want to remove the url field and have it always just containing #.

    Can’t you set it to # with jquery and just hide the element with display:none;?

    Thread Starter mYrAn

    (@myran)

    I dont know that much jQuery. How do i change the value of #custom-menu-item-url to #? Hiding it isnt a problem, change the value is now.

    Thread Starter mYrAn

    (@myran)

    Ive managed to hide it the first time, but since a new box is added when ever i add a menubutton it just appears again..

    <script>
    jQuery(document).ready(function($) {
      $('input#custom-menu-item-url').attr('value', '#');
    });
    </script>
    Thread Starter mYrAn

    (@myran)

    Bump..

    Thread Starter mYrAn

    (@myran)

    Anyone? :<

    In the first function mentioned, there is a small typo in the code. For wp_nav_menu there is ‘remove_title_attre‘ but it should be like the rest ‘remove_title_attr’.

    Also, I am not sure that regular expression is correct. It took my url ‘https://my-website/some-page&#8217; and made it ‘https://my-website/\#\’. If that’s the intent that’s fine, but I thought it would need to be href=”#”. I am not a master of regex so I can only provide a link to Smashing Mag that describes them in detail: https://coding.smashingmagazine.com/2009/06/01/essential-guide-to-regular-expressions-tools-tutorials-and-resources/.

    But I believe it should be like this:

    preg_replace('/href=\"(.*?)\"/', 'href="#"', $nav );

    TIP: I prefer to use ‘javascript:void(0);’ to prevent any respond from a click.

    So the code I used is:

    function remove_title_attr( $nav )
    {
    return $nav = preg_replace('/href=\"http:\/\/nolink\"/', 'href="javascript:void(0);"', $nav );
    }
    
    add_filter( 'wp_nav_menu', 'remove_title_attr' );
    add_filter( 'wp_page_menu', 'remove_title_attr' );

    Where I simply put ‘nolink’ into a Custom URL in WordPress and it shows as href=”https://nolink&#8221; which is replaced by regular expression and transformed into href=”javascript:void(0);”.
    WILL NOT WORK FOR CATEGORY LINKS THOUGH.

    mYrAn – I know you’re looking to remove the link completely in your Admin side, but I cannot help you with that at the moment.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Nav-menus, "Hide" URL option for Custom links?’ is closed to new replies.