Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mlbd/4944e3a15ee0770e47c96f694deee0b5 to your computer and use it in GitHub Desktop.
Save mlbd/4944e3a15ee0770e47c96f694deee0b5 to your computer and use it in GitHub Desktop.
/**
* Create Programmatically WP nav menu from API
*
* 1. First retrieve data from url
* 2. Then Clean nav menu items if already have any
* 3. Add nav menu items dynamically by using wp_update_nav_menu_item()
*
* @package binAmmer
* @author mlimon
*/
class binAmeerNavMenu {
/**
* The API url
*
* @var string
*/
public $data_url = 'https://api.binameer.com/v1/visa/freevisacountry?reqtype=menu&fbclid=IwAR3IcMqhVKj1Ng3694CqmvlnHvMQJYN6hQMV-ZtsvDFjL7gInwn6smVT68g';
/**
* The nav menu title
*
* @var string
*/
public $menu_name = 'Menu';
/**
* The nav menu id
*
* @var int
*/
public $menu_id = null;
/**
* The nav menu item term_id
* The nav menu item where you want to fetch all item from API
* ( Required )
*
* @var string
*/
public $menu_item_id = 11;
/**
* The page id
*
* @var int
*/
public $page_id = 2;
/**
* Constructor
*/
public function __construct()
{
$main_menu = get_term_by( 'name', $this->menu_name, 'nav_menu' );
$this->menu_id = $main_menu->term_id;
add_action('init', array($this, 'cleanItems'));
add_action('wp_loaded', array($this, 'createItems'));
add_filter('the_content', array($this, 'modifyContent'));
}
/**
* returns retrieve data
*
* @link https://developer.wordpress.org/reference/functions/wp_remote_get/
* @link https://codex.wordpress.org/Function_API/wp_remote_retrieve_body
*
* @param string return data type
* @access private
*/
private function retrieveData($type = "")
{
$request = wp_remote_get( $this->data_url );
if( is_wp_error( $request ) )
return false;
$body = wp_remote_retrieve_body( $request );
$get_data = json_decode( $body, true );
$get_arr = array();
foreach( $get_data['data']['attributes'] as $id ) {
$get_arr[] = $id['cntr_name'];
}
if( $type == "sanitize" )
return $get_arr;
else
return $get_data;
}
/**
* Clean the nav menu item childs if exists
*
* @link https://developer.wordpress.org/reference/functions/wp_get_nav_menu_items/
* @link https://codex.wordpress.org/Function_Reference/wp_delete_post
*
* @access public
*/
public function cleanItems()
{
$get_arr = $this->retrieveData("sanitize");
if( empty( $get_arr ) )
return false;
$menu_obj = wp_get_nav_menu_items( $this->menu_id );
$get_menu_obj = array();
foreach( $menu_obj as $item ) {
if( $item->menu_item_parent == $this->menu_item_id ) {
$get_menu_obj[] = $item->ID;
}
}
if( !is_array( $get_menu_obj ) || empty($get_menu_obj) )
return false;
foreach( (array)$get_menu_obj as $ob_item ) {
wp_delete_post( $ob_item, true );
}
}
/**
* Create nav item into specific nav menu item
*
* @link https://developer.wordpress.org/reference/functions/wp_update_nav_menu_item/
*
* @access public
*/
public function createItems()
{
$fetch_data = $this->retrieveData();
if(
!isset($fetch_data['data']['attributes'])
|| empty($fetch_data['data']['attributes'])
)
{
return false;
}
$item_url = home_url( '/' );
//then add item into newly-created menu
foreach( (array)$fetch_data['data']['attributes'] as $item ) {
if( isset( $this->page_id ) && !empty( $this->page_id ) ) {
$item_url = get_permalink( $this->page_id ) . "?cntr_code=".$item['cntr_code'];
}
wp_update_nav_menu_item($this->menu_id, 0, array(
'menu-item-title' => __($item['cntr_name']),
'menu-item-classes' => 'cm-item-' . $item['cntr_code'],
'menu-item-url' => $item_url,
'menu-item-parent-id' => $this->menu_item_id,
'menu-item-status' => 'publish'));
}
}
/**
* the_content filtering
*
* @link https://codex.wordpress.org/Plugin_API/Filter_Reference/the_content
*
* @access public
*/
public function modifyContent($content)
{
if(
is_page() &&
is_page( $this->page_id ) &&
isset( $_GET['cntr_code'] ) &&
!empty( $_GET['cntr_code'] )
)
{
// run your condition here then store data
$data = "holla Man how are you?";
if( !empty( $data ) && $data !== FALSE ) {
$content = $data;
}
}
return $content;
}
}
$the_nav = new binAmeerNavMenu();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment