0

The menu options in WP already have support for created native posts types, and custom post types, and existing taxonomies.

But in my plugin I provide a couple custom URLs that are not associated with any post object. E.g. domain.com/open-entries will list all the entries for a particular CPT with certain values set in postmeta; and other staff like that.

I can tell my users to add links to this urls using the "Links" part of the menu administration, but would require for them to remember and enter the links manually, which is not ideal.

Ideally I would like to have something like this:enter image description here

Where "Custom Links" is another foldable panel where to insert my custom links, so it's easier to insert them into the selected menu.

Where should I begin? What filter or action is there to hook into?

Thanks and regards.

  • From my point of view, it could be better if you provide other method to get that listing, so the users can choose the URL structure they want in their webistes. For example, the user could select a page (page post type) for the listing and you hook into pre_get_posts in that page to show the listing; this is similar to what bbpress does. Using a shortcode could be another possibility. The point is that users are not forced to use a fixed URL. – cybmeta Mar 12 '15 at 9:46
  • I'll take it into account. Still, would like to know how to do what I'm asking. Thanks! – yivi Mar 12 '15 at 9:48
  • Of course, it was just a suggestion of what I think is really better from the final user perspective. – cybmeta Mar 12 '15 at 10:50
  • I think it is a good idea, and I'll probably use a variation of this for future projects. It pointed me in the right direction. And thanks for your answer below, that helps as well! – yivi Mar 12 '15 at 12:54
1

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 function nav_menu_link() {?>
        <div id="posttype-custom-link" class="posttypediv">
            <div id="tabs-panel-custom-link" class="tabs-panel tabs-panel-active">
                <ul id ="custom-link-checklist" class="categorychecklist form-no-clear">
                    <li>
                        <label class="menu-item-title">
                            <input type="checkbox" class="menu-item-checkbox" name="menu-item[-1][menu-item-object-id]" value="-1"> <?php _e('Open entries', 'text-doamin'); ?>
                        </label>
                        <input type="hidden" class="menu-item-type" name="menu-item[-1][menu-item-type]" value="custom">
                        <input type="hidden" class="menu-item-title" name="menu-item[-1][menu-item-title]" value="<?php esc_attr_e('Open entries', 'text-doamin'); ?>">
                        <input type="hidden" class="menu-item-url" name="menu-item[-1][menu-item-url]" value="<?php echo esc_url( home_url('/open-entries') ); ?>">
                        <input type="hidden" class="menu-item-classes" name="menu-item[-1][menu-item-classes]" value="custom-link">
                    </li>
                </ul>
            </div>
            <p class="button-controls">
                <span class="add-to-menu">
                    <input type="submit" class="button-secondary submit-add-to-menu right" value="Add to Menu" name="add-post-type-menu-item" id="submit-posttype-custom-link">
                    <span class="spinner"></span>
                </span>
            </p>
        </div>
    <?php }
}

$custom_nav = new Custom_Nav;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.