GithubHelp home page GithubHelp logo

eduardoleao / wp-custom-menu-items Goto Github PK

View Code? Open in Web Editor NEW

This project forked from daggerhart/wp-custom-menu-items

0.0 0.0 0.0 3 KB

Class for creating and managing custom (and dynamic) menu items in WordPress

PHP 100.00%

wp-custom-menu-items's Introduction

Custom WordPress Menu Items

This is a utility class for WordPress developers that allow for adding and modifying menu links dynamically within a plugin or theme.

Originally this was hosted as a Gist.

Example Use

<?php
add_action( 'wp', function(){
    // Single item
    /**
     * @param $menu_slug
     * @param $title
     * @param $url
     * @param $order
     * @param $parent
     * @param null $ID
     */
    custom_menu_items::add_item('menu-1', 'My Profile', get_author_posts_url( get_current_user_id() ), 3  );

    // Item with children
    // note: the ID is manually set for the top level item
    custom_menu_items::add_item('menu-1', 'Top Level', '/some-url', 0, 0, 9876 ); 
    // note: this and other children know the parent ID
    custom_menu_items::add_item('menu-1', 'Child 1', '/some-url/child-1', 0, 9876 ); 
    custom_menu_items::add_item('menu-1', 'Child 2', '/some-url/child-2', 0, 9876 );
    custom_menu_items::add_item('menu-1', 'Child 3', '/some-url/child-3', 0, 9876 );

    /**
     * Add object by ID
     *
     * @param $menu_slug
     * @param $object_ID
     * @param string $object_type
     * @param $order
     * @param $parent
     * @param null $ID
     */
    // Add the post w/ ID 1 to the menu
    custom_menu_items::add_object('menu-1', 1, 'post');
    // Add the taxonomy term with ID "3" to the menu as a top-level item with the ID of 9876
    custom_menu_items::add_object('menu-1', 3, 'term', 0, 0, 9876);
    // Add the taxonomy term with ID "4" to the menu as a child of item 9876 
    custom_menu_items::add_object('menu-1', 4, 'term', 0, 9876);
} );

References:

Thanks:

Here are some developers who helped make this class better when it was a Gist.

  • @webdados
  • @codepuncher
  • @vandelio
  • @damienbuchs

wp-custom-menu-items's People

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.